Batch file copy newest file

June 18, 2010 at 00:20:04
Specs: XP / SBS 2003
I'm using this batch file to take the newest file and copy it to another location.

FOR /F %%I IN ('DIR *.* /B /O:-D') DO COPY %%I <<NewDir>> & EXIT


My exact syntax is this:
FOR /F %%I IN ('DIR H:\*.* /B /O:-D') DO COPY %%I F:\

But I'm getting an error saying the system cannot find the specified file. I had it working once about an hour ago, edited my live bat file to add this command and it stopped working. I don't know what I did as I accidentally closed the unsaved editor. Currently there are 3 files in H, I want the newest to go to F. Any help appreciated.


See More: Batch file copy newest file

Report •


#1
June 18, 2010 at 04:26:43

FOR /F "delins=" %%I IN ('DIR H:\*.* /B /O:-D') DO COPY "%%~fI" "F:\"

Report •

#2
June 18, 2010 at 04:37:11
Thanks,
Still no go. Says "delins" not expected at this time. Then still says cannot find the file specified. Took delins out, same thing.

Report •

#3
June 18, 2010 at 04:38:09
I should add, I'm running this from C, make a difference?

Report •

Related Solutions

#4
June 18, 2010 at 04:42:32
Ah! It's trying.
Sees the files, but in 8.3 but the file names have spaces so it's only seeing "H:\File Name Number one.txt" as H:\File" and erroring out. Any way around this?

Report •

#5
June 18, 2010 at 05:39:59
FOR /F "delims=" %%I IN ('DIR H:\*.* /B /O:-D') DO COPY "%%~fI" "F:\"


Report •

#6
June 18, 2010 at 05:46:14
IVO,
Thanks. I didn't catch the typo in "delims" first either. Working great!

Report •

Ask Question