1.

Solve : copying last modified file?

Answer»

Hi all,

I'm trying to copy the last modified file in dos.

I can USE 'dir /OD' to search a directory for the last modified file, but how do I capture the top result as a variable???

any ideas??

Cheers in advance This will almost do it:
Code: [Select]dir /OD /B>dirlist.txt
FOR /F "tokens=1,2" %%i IN (dirlist.txt) DO set lastfile= %%i

@echo %lastfile%
But I didn't have time to finish it. I have to run out to a client, when I come back I'll refine it the rest of the way for you.Try this

Code: [Select]dir /-OD /B>dirlist.txt
set /P lastfile=<dirlist.txt
@echo %lastfile%
This does a directory listing, sorting the newest file to the top of the list. The Set /P takes the first line from the supplied file (the newest file) and stores it in the variable %lastfile%

GrahamNice, that /P is the flag I was looking for and couldn't find before I had to leave.Easy to grab the first or last file or folder name in a list of any of the properties DIR can sort using the /O switch - size, name, extension, ETC

Quote


@echo off
REM earliest file in directory
FOR /F %%i IN ('dir /b /a-d /o-d') DO (
set firstfile=%%i
set firstdate=%%~ti
)

REM latest file in directory
FOR /F %%i IN ('dir /b /a-d /od') DO (
set lastfile=%%i
set lastdate=%%~ti
)

echo first file %firstdate% %firstfile%
echo last file %lastdate% %lastfile%

Quote from: gpl on February 19, 2008, 09:26:57 AM
Try this

Code: [Select]dir /-OD /B>dirlist.txt
set /P lastfile=<dirlist.txt
@echo %lastfile%
This does a directory listing, sorting the newest file to the top of the list. The Set /P takes the first line from the supplied file (the newest file) and stores it in the variable %lastfile%

Graham

Nice, that did the job.

I had to move the - to before the D, other wise it's all good.

Cheers guys!!Since we're on the subject of setting a variable with the outcome of the Dir command...


I'd really like to have ANOTHER variable with the date that it was last modified.

then I could display the date that it was last modified at my menu and the user could decided weather to use or not.

cheers for all your help guys. Quote from: blastman on February 20, 2008, 04:00:44 AM
I'd really like to have another variable with the date that it was last modified.

See my post of 19 FebQuote from: Dias DE verano on February 19, 2008, 09:53:57 AM
Easy to grab the first or last file or folder name in a list of any of the properties DIR can sort using the /O switch - size, name, extension, etc

Quote

@echo off
REM earliest file in directory
FOR /F %%i IN ('dir /b /a-d /o-d') DO (
set firstfile=%%i
set firstdate=%%~ti
)

REM latest file in directory
FOR /F %%i IN ('dir /b /a-d /od') DO (
set lastfile=%%i
set lastdate=%%~ti
)

echo first file %firstdate% %firstfile%
echo last file %lastdate% %lastfile%


Hey thats cool.

Thanks.

One problem, if i want to run this for a different directory then it seems to not pick up the time and dates, only the file names??

EXAMPLE,

REM latest file in directory
FOR /F %%i IN ('dir c:\stuff\*.* /b /a-d /od') DO (
set lastfile=%%i
set lastdate=%%~ti
)

echo last file %lastdate% %lastfile%

Would only display;

last file "what-ever-the-file-name-is"

any ideas??

Cheers for all your help. Hmmm... I'm at work right now, so I'll try to solve this later, but for now...

try e.g.

set directory="C:\mystuff"
cd /d %directory%
FOR /F %%i IN ('dir /b /a-d /od') DO (

(etc)
Hi,

Yep that worked a treat


Setting the working directory before running the loop was the ticket.

cheers for all your help.

Thanking you!!!


Discussion

No Comment Found