Hi here is what I need to get accomplished and not sure how. I have a SQL sever that does daily backup of the transaction logs to a local E drive on the sql server. What I need to do is copy the most recent log file from that server to a remote server. Once I get the script to work I will then setup as a schedule task. Can anyone give me advice on how to copy the most recent file to a different server. This will run daily as well so the file name date will change that is why I need the most recent file in the directory where the backup are going.
Asked
Active
Viewed 4,956 times
1 Answers
1
Use in the batch file these command lines:
for /F "delims=" %%I in ('dir "C:\Path To\Source Folder\*" /A-D-H /B /O-D /TW 2^>nul') do copy /B /Y "C:\Path To\Source Folder\%%I" "X:\Path To\Destination Folder\" >nul & goto FileCopied
:FileCopied
rem More commands after copying file with newest last modification date.
This code is explained for example at Get newest file in directory with specific extension. Command DIR excludes here also files with hidden attribute set as command COPY does not copy hidden files. The destination folder must already exist.
Enhanced version with real folder paths:
@echo off
setlocal EnableExtensions DisableDelayedExpansion
set "SourceFolder=E:\MSSQL\Backup\AaaImaging"
set "TargetFolder=\\servername\F\MSSQL\Backup\Pablo"
for /F "delims=" %%I in ('dir "%SourceFolder%\*" /A-D-H /B /O-D /TW 2^>nul') do copy /B /Y "%SourceFolder%\%%I" "%TargetFolder%\" >nul & goto FileCopied
:FileCopied
rem More commands after copying file with newest last modification date.
endlocal
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.
copy /?
dir /?
echo /?
endlocal /?
for /?
goto /?
rem /?
set /?
setlocal /?
Read also the Microsoft documentation page about Using command redirection operators and Single line with multiple commands using Windows batch file.

Mofi
- 46,139
- 17
- 80
- 143