21

I have a list of zip files with date and time appended like yyyymmdd_hhmmss_Demos.zip. Now how to get the most recently added zip file in the source dir. I need to copy this file in the target using copy command.

I found some info about forfiles, but do not have an idea on how to get it done for seconds.

double-beep
  • 5,031
  • 17
  • 33
  • 41
azzaxp
  • 697
  • 5
  • 11
  • 26
  • 2
    possible duplicate of [How do I write a Windows batch script to copy the newest file from a directory?](http://stackoverflow.com/questions/97371/how-do-i-write-a-windows-batch-script-to-copy-the-newest-file-from-a-directory) It's not clear whether you want the most recently modified file, the file most recently added to the directory, or the file with the highest yyyymmdd_hhmmss. – Raymond Chen Jul 18 '12 at 13:27

4 Answers4

37

You can use

pushd D:\a
for /f "tokens=*" %%a in ('dir /b /od') do set newest=%%a
copy "%newest%" D:\b
popd
Bali C
  • 30,582
  • 35
  • 123
  • 152
  • for the sake of simplicity i just put a set of text files in a folder a and made target as folder b and hence wrote this command for /f "tokens=*" %%a in ('dir D:\a /b /od') do set newest=%%a copy "%newest%" D:\b But this does not work for me.. what is wrong – azzaxp Jul 18 '12 at 11:42
  • 1
    @azzaxp Try my updated answer. It seems the directory was the problem for some reason, I'm not sure why. – Bali C Jul 18 '12 at 12:02
  • `dir /b /od` will list also subdirs, if any. I think you should exclude them using `/a-d` switch (unless of course OP wants to copy subdirs also) – wmz Jul 18 '12 at 12:05
  • 1
    @wmz No, it doesn't. The `/s` switch lists subfolders, try it and see :) – Bali C Jul 18 '12 at 12:09
  • sorry wrong wording. I meant that it will include names of any subdirs, not their contents (for this as you correctly stated you would need /s). So it will find newest **file** or **directory** – wmz Jul 18 '12 at 12:28
  • @azzaxp sorry to bring this back up years later haha. Im trying this solution though unlike op i need to copy the latest folder not sure if that makes a difference, but when i run this i just get a list of it setting the last created directory being set to newest, for some reason the copy doesn't seem to work though, is there a switch or something i should be using to make it work with folders? – Sirk Jul 04 '14 at 07:32
2
set Path="D:\hello\abc\old"
for /f "tokens=*" %%a in ('dir /A:-D /B /O:-D /S %Path%') do set NEW=%%a&& goto:n 
:n
echo %NEW%
4b0
  • 21,981
  • 30
  • 95
  • 142
vishal
  • 21
  • 1
1
pushd \\ryap\CONTROL_DATOS
for /f "tokens=*" %%a in ('dir \\ryap\CONTROL_DATOS /b /od') do set newest=%%a
Xcopy/Y "\\ryap\CONTROL_DATOS\%newest%" "D:\TXT_SOURCES\"
popd
Stephan
  • 53,940
  • 10
  • 58
  • 91
Mauro
  • 21
  • 1
  • 4
    Pasting some code without explaining what it does is not so useful. Can you add a description? – SaeX Jan 05 '16 at 19:32
0

Below snippet will extract the date and customize as per your needs

for /f "tokens=1-4 delims=/ " %%i in ("%date%") do (
     set dow=%%i
     set month=%%j
     set day=%%k
     set year=%%l
)
:: Pad digits with leading zeros e.g Sample_01-01-21.csv
    set yy=%year:~-2%
    set datestr=%day%-%month%-%yy%

Alternate way:

set datestr=%date:~0,2%-%date:~3,2%-%date:~6,2%
Bharathiraja
  • 714
  • 1
  • 12
  • 20