title: SQL Server Restore Database with new file names
body: <p>Is it possible to restore SQL Server database from a backup and at the same time create <code>*.mdf</code> and <code>*.ldf</code> files with new names? </p>
  
  <p>Assumed that I know that there are only two files per backup (one <code>mdf</code> and one <code>ldf</code>), but I don't know the exact names of these files and unfortunately I can't derive them from database names. </p>
  
  <p>The purpose of all this is to create batch file which would restore databases from a set of backup files. At the same time I must solve filename conflicting issue.</p>
  
  <p>Currently I have this command, which doesn't work for every backup because some database files are named differently from variable %DATABASENAME%:</p>
  
  <pre><code>...
  RESTORE DATABASE [%NewDB%] 
  FROM DISK = N'%BACKUPFILENAME%' 
  WITH RECOVERY,  
  MOVE N'%DATABASENAME%' TO N'C:\%NewDB%.mdf',  
  MOVE N'%DATABASENAME%_Log' TO N'C:\%NewDB%_Log.ldf'
  ...
  </code></pre>