2

I am trying to get all the newest file from subdirectories into one network directory with a command without getting the subdirectory structures. They are the SQL Server log files with extension of *.trn. I have the following but it doesn't work.

Trying to get only the newest *.trn files from ...........Backup and it's subdirectories.

for /R E:\SQLSERVER\PRODINSTANCE1\Backup %%f in (*.trn) do xcopy %%f "\\198.152.71.14\NetBackups$\MSSQL\Logs" /B /O:D /d /Y
1
  • What doesn't work about it?
    – zdan
    Commented Jun 27, 2013 at 17:29

2 Answers 2

1

You can use the dir command with the /od switch and a for loop:

@echo off &setlocal enabledelayedexpansion
for /d /r "E:\SQLSERVER\PRODINSTANCE1\Backup" %%a in (*) do (
    for /f "delims=" %%i in ('dir /b /a-d /od "%%~a"') do set "newest=%%~fi"
    xcopy "!newest!" "\\198.152.71.14\NetBackups$\MSSQL\Logs" /B /O:D /d /Y
)

For more help enter help dir in the command prompt.

1
  • I wasn't sure @Calvin only wanted the one newest file or all the newest/updated files.
    – mojo
    Commented Jun 27, 2013 at 19:22
0

I'm not sure if you're using xcopy correctly. /O doesn't take arguments. It only copies ownership/ACL info. (Is that really what you want for log files?)

Since you didn't describe what "doesn't work" means, my only suggestion is to hedge against file names with spaces in them.

FOR /R "%SRC_DIR%" %%f in (*.trn) do xcopy "%%~f" "%DEST_DIR%" /B /O /D /Y

This worked for me (I tested with .pdf's).

Not the answer you're looking for? Browse other questions tagged or ask your own question.