2

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.

The latest added file to the directory (result of FOR /F) should then be used in SQL RESTORE (backup and ftp program Windows).

The idea was to use the FOR command

My draft:

  • Go into the directory:

    pushd "D:\Romexis_Bilder\romexis_SQL_Backup"
    
  • Find the latest file. (I don't really know how to set the parameters here.)

    for /f "tokens=*" %% in ('dir /D:\Romexis_Bilder\romexis_SQL_Backup /od') do set newest=%%D:\Romexis_Bilder\romexis_SQL_Backup
    
  • The result of FOR should be used in *.zip

    cd C:\Program Files (x86)\Pranas.NET\SQLBackupAndFTP\
    SqlRestore D:\Romexis_Bilder\romexis_SQL_Backup\*.zip -db Romexis_db -srv .\ROMEXIS -pwd password disconnect Romexis_db
    

I stuck with FOR, but don't know if there would also be another possibility.

Mofi
  • 46,139
  • 17
  • 80
  • 143
vento
  • 25
  • 6

3 Answers3

0

I don't know if the last command line in question is really correct. I have some doubts output this line.

But this code can be used to get the name of the newest *.zip file according to last modification date without path.

@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 /TW 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 disconnect Romexis_db
echo/
pause

FOR executes in a separate command process started with cmd.exe /C in background the command line:

dir "D:\Romexis_Bilder\romexis_SQL_Backup\*.zip" /A-D-H /B /O-D /TW 2>nul

DIR outputs to handle STDOUT of background command process

  • only names of non hidden files because of /A-D-H (attribute not directory and not hidden)
  • in bare format because of /B just the file name with file extension, but without file path
  • sorted reverse (newest first) by date because of /O-D
  • using write time (last modification time) because of /TW
  • in directory D:\Romexis_Bilder\romexis_SQL_Backup matching the pattern Romexis_db*.zip.

I recommend running this command line in a command prompt window to see at least once what DIR outputs.

DIR would output an error message to handle STDERR in case of no *.zip file found or the directory does not exist at all. This error message is suppressed by redirecting it to device NUL.

Read also the Microsoft article about Using Command Redirection Operators for an explanation of 2>nul. The redirection operator > must be escaped with caret character ^ on FOR command line to be interpreted as literal character when Windows command interpreter processes this command line before executing command FOR which executes the embedded dir command line with using a separate command process started in background.

FOR captures the output written to STDOUT and processes the output line by line with ignoring empty lines which do not occur here because of DIR with option /B does not output empty lines.

FOR with option /F would ignore lines starting with a semicolon by default. For that reason end of line character is redefined with eol=| from ; to a vertical bar which file names can't contain. eol=| would not be required in this case because of file name pattern Romexis_dbYYYMMDDhhmm.zip making it unlikely that a file name starts with a semicolon.

FOR with option /F would split up the lines into substrings using space/tab as delimiter and would assign for each line only first space/tab delimited string to specified loop variable I. This line splitting behavior is disabled by specifying an empty list of delimiters with delims=. delims= would not be required in this case because of file name pattern Romexis_dbYYYMMDDhhmm.zip making it unlikely that a file name contains a space character.

The name of the file output first by DIR which is the newest ZIP file in specified directory is assigned to environment variable NewestFile. And next the FOR loop is exited with a jump to label DatabaseRestore as all other file names output by DIR are of no interest for this task.

The command lines below the FOR command line are executed only if there is no *.zip file in specified directory which report this unexpected error case.

It would be also possible to use the DIR command line below in batch file because of file name pattern Romexis_dbYYYMMDDhhmm.zip:

dir "%BackupFolder%\Romexis_db*.zip" /A-D-H /B /O-N 2^>nul

The same command line for execution from within a command prompt window:

dir "D:\Romexis_Bilder\romexis_SQL_Backup\*.zip" /A-D-H /B /O-N 2>nul

The order of the file names in output is here reverse by name which results in printing first the Romexis_db*.zip with newest date/time in file name thanks to date/time format YYYMMDDhhmm.

For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.

  • cd /?
  • dir /?
  • echo /?
  • for /?
  • goto /?
  • pause /?
  • set /?

See also:

Mofi
  • 46,139
  • 17
  • 80
  • 143
0

@Mofi Thanks you for all your work it helped a lot! As you advised I used each command in command prompt first to see the outputs (adapted batch %%I to cmd %I and vice versa)
I'm now able to find the newest file in D:\Romexis_Bilder\romexis_SQL_Backup the result is processed an taken as variable into the restore of the database which is done with One-Click SQL Restore https://sqlbackupandftp.com/restore I did some modification in syntax of your commands O:D since „- „ excludes, removed attribute /TW because it was only listing backups from 2017.

@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

Maybe the ^ in 'dir "%BackupFolder%\Romexis_db*.zip" /A-D-H /B /O:D 2^>nul' is not correct in CMD but didn‘t seem affect the result.

It was really advance! 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. Or try it over Microsoft Visual Studio SQL or command line tool.

vento
  • 25
  • 6
  • `/TW` is not really needed because of listing is done by default using last modification (write) date on using `/O` with `D`. But I do not understand why you have changed `/O-D` - list by date with newest first - to `/O:D` - list by date with oldest first. Why do you reverse the list order and use now the oldest backup file from 2017 instead of the newest backup file from 2018? – Mofi Jun 03 '18 at 15:22
  • Escaping redirection operator `>` with `^` is definitely necessary in the batch file and I explained also why. If you remove `^` from batch file and run it from within a command prompt window, you will see an error message output by `cmd` because of `2>nul` at wrong position on __FOR__ command line and `cmd` exits batch file execution after first two lines because of this syntax error. – Mofi Jun 03 '18 at 15:30
  • @MOFI If I use the your command 'DIR /O-D or /O:-D ' the newest file is set to the D:\Romexis_Bilder\romexis_SQL_Backup\Romexis_db201707221830.zip iF I use O:D ( See https://www.computerhope.com/dirhlp.htm :For instance, an option of "/O:D" displays files oldest-to-newest) I get the right variable last file from 2018 and not the oldest file as you written. But until now I can’t explain why. – vento Jun 09 '18 at 15:36
  • This means that `Romexis_db201707221830.zip` has a last modification date (write date) which is not `2017-07-22 18:30`. This file has a last modification date newer than the last modification date of all other `Romexis_db*.zip` files in the directory. So it is most likely better to sort the files by file name using `/O-N` as suggested by me in my answer and ignore unreliable last modification dates of the ZIP files. – Mofi Jun 09 '18 at 16:21
0

@MOFI no modifications are made to files from 2017 or other files at all, files are not overwritten or modified later, a new file is always created by the back up program 2 times a day with the naming romexis_dbYYYMMDDhhmm.ziptwo times a day. Will try /O-N THANKS a lot fo you input

vento
  • 25
  • 6