0

Ive looked here Batch file to copy directory if it has a later created date than the target directory

for /f "tokens=*" %%a in ('dir/b /od \\machine\c$\location') do set newest=%%a
xcopy /e /k "%newest%" "\\machine\c$\otherlocation"
popd
pause

doesn't work for me:(explained below and above is the code im using)

Dos shows "xcopy /e /k "file.bat" "location" fie not found - file.bat 0 File(s) copied" thing is this file is not the newest created

I've tried various bits of code which I can not get working.

What Im trying to achieve:

Id like a batch file which will search a directory by creation date pick the latest CREATION date then copy to a different direction e.g. file created at 08:16am will get copied rather than the file created at 08:15am on the day of creation.

It'd be amazing if this batch could search by creation date, file type and a string of text from the file name E.g

Files: (in format of Day/Month/Time)

Test01010815.bat   created 01/01 08:15
Test01020817.bat   created 01/02 08:17
Test01020818.csv   created 01/02 08:18
Test01020819.csv   created 01/02 08:19

then copies the CSV as I want CSV only and the bottom file as it was the latest created CSV

In the mean time have tried this:

for /f "delims=" %%i in ('dir /b /a-d /od "\\location\STARTTofFILEname*.*"') do set "LatestModifiedFile=%%~i"
xcopy /F /Y "\\el-1311932\c$\batch\export\exe\%LatestModifiedFile%" "\\el-1311932\c$\batch\exe\test\StockExport.csv"
Community
  • 1
  • 1
  • "Doesn't work for me" does not explain what it did that you didn't want it to do, or what it didn't do that you wanted it to do. From your description, the code you've posted *should* work. Is the filename alone sufficient for your `last-created` criterion? That is, is the name reliable, or do you really want to process by create-date? – Magoo Feb 03 '16 at 08:35
  • Edit done to the subject. I'd really like to process by created date (possible speed increase?) I will have hundreds of files in this directory. I could pull using the file name however I didn't really want the batch to scan every filename as I don't want this batch to slow anything down or take too long. As you can see my goal is to check when the file was created then copy the latest to a different directory, if you think scanning the filename is fine then Id go with that. – Thomas Jones Feb 03 '16 at 09:06
  • Ok the file is the latest created it overwrites itself when ran – Thomas Jones Feb 03 '16 at 09:13

1 Answers1

0

You've started out with a mapped drive z:, then changed the code to leave an orphaned popd and use a URL, now you're using two...

This should work:

set "sourcedir=\\machine\c$\location"
set "destdir=\\el-1311932\c$\batch\export\exe"
for /f "delims=" %%i in ('dir/b /a-d /od "%sourcedir%\*.csv"') do set "LatestModifiedFile=%%~nxi"
xcopy /F /Y "%sourcedir%\%LatestModifiedFile%" "%destdir%\StockExport.csv"
Magoo
  • 77,302
  • 8
  • 62
  • 84
  • Had to add these switches for creation date (/b /a-d /od /t:) Your version put me done the right path thankyou just your version finds by modified date – Thomas Jones Feb 04 '16 at 08:52