2

I have a Windows directory which contains 4 files in total. The file names are as follows:

  • Address_1502.csv
  • Address_1602.csv
  • Site_1502.csv
  • Site_1602.csv

The prefix of the file name never changes so the files will always begin with Address_ or site_. I am trying to find a command which can copy the most recent of the two files from their current directory in to a different directory. I currently have the following script however this only finds the newest of all 4 files and copies it over however I would like to find the newest of the two Address_ files AND the newest of the two Site_ files.

Can anyone help at all?

set "source=D:\TEMP_ONLY\SOURCE"
set "dest=D:\TEMP_ONLY\DESTINATION"
pushd "%source%" ||(
   echo.Source does not exist&pause&goto EOF)

for /f "tokens=*" %%f in (
   'dir /A-D /OD /B') Do set "file=%%f"
popd
xcopy /d /i "%source%\%file%" "%dest%\"
1

1 Answer 1

0

I would like to find the newest of the two Address_ files AND the newest of the two Site_ files.

Use the following batch file:

@echo off
Setlocal EnableDelayedExpansion
set "source=D:\TEMP_ONLY\SOURCE"
set "dest=D:\TEMP_ONLY\DESTINATION"
pushd "%source%" || ( echo.Source does not exist & pause & goto done2)
for /f "delims=" %%a in ('dir Address_*.csv /a-d /b /O:d') do (
  xcopy /d /i "%source%\%%a" "%dest%\"
  goto :done1
  )
:done1   
for /f "delims=" %%a in ('dir Site_*.csv /a-d /b /O:d') do (
  xcopy /d /i "%source%\%%a" "%dest%\"
  goto :done2
  )
:done2
popd

Further Reading

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .