简体   繁体   中英

Get the last modified file

I'm doing a batch file and I want to get the last file copied to the directory. In this case, a .cmd file which is always generating a new one.

The first tentative I have made was using the /T:W thing and it's was working very nice; the problem is happening when the file name use two (or more) numbers.

for /f %%x in ('dir C:\foldercmd\*.cmd /B /T:W ') do set "cmdFile=%%x"

So the output i'm getting is:

C:\Users\foldercmd>set "cmdFile=cmdWork_1.cmd"
C:\Users\foldercmd>set "cmdFile=cmdWork_10.cmd"
C:\Users\foldercmd>set "cmdFile=cmdWork_11.cmd"
C:\Users\foldercmd>set "cmdFile=cmdWork_12.cmd"
C:\Users\foldercmd>set "cmdFile=cmdWork_2.cmd"
C:\Users\foldercmd>set "cmdFile=cmdWork_3.cmd"
C:\Users\foldercmd>set "cmdFile=cmdWork_4.cmd"
C:\Users\foldercmd>set "cmdFile=cmdWork_5.cmd"
C:\Users\foldercmd>set "cmdFile=cmdWork_6.cmd"
C:\Users\foldercmd>set "cmdFile=cmdWork_7.cmd"
C:\Users\foldercmd>set "cmdFile=cmdWork_8.cmd"
C:\Users\foldercmd>set "cmdFile=cmdWork_9.cmd"

And i want to get:

C:\Users\foldercmd>set "cmdFile=cmdWork_1.cmd"
C:\Users\foldercmd>set "cmdFile=cmdWork_2.cmd"
C:\Users\foldercmd>set "cmdFile=cmdWork_3.cmd"
[...]
C:\Users\foldercmd>set "cmdFile=cmdWork_11.cmd"
C:\Users\foldercmd>set "cmdFile=cmdWork_12.cmd"

I am going to answer this question purely by the title. Get last modified file

@echo off
for /f "delims=" %%i in ('dir "C:\foldercmd\*.cmd" /b /a-d /o-d') do set "cmdfile=%%i" & goto :end
:end
echo %cmdfile%

This will order the files *.cmd in order, latest files first, then only set the first file as latest and echo it. Once you have a new .cmd file added to that directory, it will then select the new one.. easy as that.

According to Dir /? the /O switch allows you to sort. In case you want to do this, based on last modification date, you can use D , so you get:

Dir /OD

Is this what you are looking for?

If you were unable to modify the process which is naming the files, and you wanted to still sort using the names, you may be able to leverage from your :

For /F "Delims=" %%A In ('PowerShell -NoP^
 "GCI "C:\foldercmd" -Fi "cmdWork_*.cmd"|Sort{If($_ -Match '(\d+)')"^
 "{[int]$Matches[1]}}|Select -Exp Name"')Do Set "cmdFile=%%A"

The above example loops through each file, set ting each in turn, until the last, but you could ask to return only the last if you only wanted to:

For /F "Delims=" %%A In ('PowerShell -NoP^
 "GCI "C:\foldercmd" -Fi "cmdWork_*.cmd"|Sort{If($_ -Match '(\d+)')"^
 "{[int]$Matches[1]}}|Select -Exp Name -L 1"')Do Set "cmdFile=%%A"

Two pure batch solutions searching the highest number used.

  1. using a counting loop

     :: Q:\\Test\\2019\\04\\29\\SO_55899758.cmd @Echo off for /l %%L in (1,1,100) do if not exist "C:\\foldercmd\\cmdwork_%%L.cmd" ( set /a "No=%%L-1" goto :Done ) Echo Highest number is greater 100 Goto :Eof :Done echo Highest used number is %No% pause 
  2. splitting the name at the underscore and comparing with the previous

     :: Q:\\Test\\2019\\04\\29\\SO_55899758_2.cmd @Echo off & Setlocal EnableDelayedExpansion set "No=0" for /f "tokens=2 delims=_." %%L in (' dir /B "C:\\foldercmd\\cmdwork_*.cmd" ') do if %%L gtr !No! set "No=%%L" echo Highest used number is %No% pause 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM