I want to make a batch or cmd file to automatically select the latest file in the directory D:\Romexis_Bilder\romexis_SQL_Backup. These are ZIP SQL backup files that are generated two times daily in the format yymmddhhmm.zip, e.g Romexis_db201805271200.zip on a server running Windows 2016 Server. A copy o the Data is transfered over VPN to a NAS, and should be restored auomatically to a server 2012R2 that is our back up server running the same database.
The latest added file to the directory (result of FOR /F) should then be used in SQL RESTORE (backup and ftp program Windows).
Thanks to MOFI from stack overflow and some modification the following batch was created:
@echo off
set “BackupFolder=D:\Romexis_Bilder\romexis_SQL_Backup”
for /F “eol=| delims=” %I in (‘dir “%BackupFolder%\Romexis_db*.zip” /A-D-H /B /O:D 2^>nul’) do set “NewestFile=%I” & goto DatabaseRestore
echo ERROR: Could not find any *.zip backup file in folder:
echo “%BackupFolder%”
echo/
pause
goto :EOF
:DatabaseRestore
cd /D “%ProgramFiles(x86)%\Pranas.NET\SQLBackupAndFTP”
SqlRestore.exe “%BackupFolder%%NewestFile%” -db Romexis_db -srv .\ROMEXIS -pwd password
echo/
pause
This Works!
Now the GUI of One-Click SQL Restore opens with the newest *zip . The only thing that I still need to get out, is the syntax in command prompt for the restore, now i still need to click on the restore button of the GUI. Another Problem what is the command to disconnect the database in One-click SQL restore?
An if the ZIP is password protected how is the syntax in One click if supported?
Original post : for loop - How to find and select newest file in a directory and restore with SQLRestore? - Stack Overflow