copy the max date file using dos command not working

ewang1205
ewang1205 used Ask the Experts™
on
I have the following command to copy test0514.pgp to test.pgp.  But, test0512.pgp
has the latest timestamp and in fact it copy test0512.pgp to test.pgp.  Can I check the name of the file and copy the max date based on the name?  Thanks.

FOR /F "tokens=*" %%I IN ('DIR c:\test*.gpg /B /O:-D') DO COPY/y "c:\%%I" c:\test.gpg & EXIT

test0512.pgp
test0513.pgp
test0514.pgp
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Ben Personick (Previously QCubed)Lead SaaS Infrastructure Engineer

Commented:
Please clarify your question..
Most Valuable Expert 2018
Distinguished Expert 2018
Commented:
The command you've posted does NOT copy a single file; it copies ALL files matching the pattern to "test.gpg"; of course only the last copy will survive, but it's not very fast if a lot of gpg files happen to be around.
And the last file to be copied will be the oldest one, not the latest one; "/O:-D" orders ("/O") reverse ("-") by date ("D"), meaning the oldest file will be listed last and "win".
The /y is unnecessary; in a batch script, copy will overwrite an existing file without verifying.
The "exit" is very probably unnecessary, too; if the script is started with a double-click from Explorer, the command window will close automatically; if you run it manually from an open command prompt, it will close the prompt (which is usually not wanted in this case).
To copy the oldest file matching the pattern:
FOR /F "tokens=*" %%I IN ('DIR C:\test*.gpg /B /O:-D') DO set OldestFile=C:\%%a
COPY /y "%OldestFile%" "C:\test.gpg"

Open in new window

To copy the latest file matching the pattern:
FOR /F "tokens=*" %%I IN ('DIR C:\test*.gpg /B /O:D') DO set LatestFile=C:\%%a
COPY /y "%LatestFile%" "C:\test.gpg"

Open in new window

To copy the first file by name matching the pattern:
FOR /F "tokens=*" %%I IN ('DIR C:\test*.gpg /B /O:-N') DO set FirstFile=C:\%%a
COPY /y "%FirstFile%" "C:\test.gpg"

Open in new window

To copy the last file by name matching the pattern:
FOR /F "tokens=*" %%I IN ('DIR C:\test*.gpg /B /O:N') DO set LastFile=C:\%%a
COPY /y "%LastFile%" "C:\test.gpg"

Open in new window

Ben Personick (Previously QCubed)Lead SaaS Infrastructure Engineer

Commented:
Thanks for pointing that out oDBA, I got so caught up in the request's wording I didn't look at the code!

Here, I think we should just tweak his original line as follows, it's the simplest way, he was on the right track but he actually wants a GOTO :EOF to exit the loop on the first item encountered.

Also I added in the switch to only output FILE names in case he has any directories there.
FOR /F "tokens=*" %%I IN ('DIR "c:\test*.gpg" /B /O:-D /A:-D') DO COPY/y "c:\%%I" c:\test.gpg&GOTO :EOF

Open in new window

CompTIA Security+

Learn the essential functions of CompTIA Security+, which establishes the core knowledge required of any cybersecurity role and leads professionals into intermediate-level cybersecurity jobs.

Commented:
Actually surely he already has what he needs with the EXIT to stop the loop, it is because he just needs to sort by name instead of date, i.e. use /O-N and then the latest by order of name will be the first as in oBdA's suggestion.

Sometimes I use an exit or goto out of a loop in batch (though in most languages that is bad practice) and sometimes keep setting a variable like oBdA did, whichever makes more sense to you when you read it back.

Steve
Ben Personick (Previously QCubed)Lead SaaS Infrastructure Engineer

Commented:
Steve,

  Initially I was under the impression that the file was actually dated oldest, as you are asserting.

  However now I'm more on board with oDBA's assessment that the exit is not kicking out of the loop, and instead it copies every file over the original until reaching the OLDEST.

Don't actually have time to test the exit but if you say it's working then I will go back to wanting the author to more properly clarify what he is looking for in his script.

-Q

Qlemo"Batchelor", Developer and EE Topic Advisor
Top Expert 2015

Commented:
Tested, and works as expected:
   for /L %%F in (1 1 10) do echo %%F & exit /b
results in 1. So this part is working, and I expected it to from experience.

The issue is indeed that date in the file name and its (creation? modification?) timestamp do not correspond. As Steve suggested. the correct way is to use /o:-N in dir, if you can trust the name. You can't on year change, as the newest is test0101.pgp, but test1231.pgp would be found first, because it has the "higher" file name.
Maybe using the creation timestamp instead of modification timestamp is the best solution. dir /o:-d /t:c would do that.
Ben Personick (Previously QCubed)Lead SaaS Infrastructure Engineer

Commented:
qlemo he wasn't using exit /b, and there is a dif between the two, I believe you're right the /b is needed in this case.  I usually use the EOF to avoid the exit versus exit/b
Qlemo"Batchelor", Developer and EE Topic Advisor
Top Expert 2015

Commented:
exit vs. exit /b are the same if we talk about exiting the batch file (!). The FOR loop is left (but there isn't any command performed after it).
Goto is useful because you can continue in the batch. Goto :EOF vs. exit /b makes no difference.

Commented:
Which all brings back to... if the date can't be relied on then all that is left is the filename.

Depending upon WHY the files go out of order though (are they updated afterwards?) have you tried looking at the results with

dir /o-d /tc
dir /o-d /tw

just manually for instance at the cmd.exe prompt, what you are wanting is a manual command that consistently always gives you the newest numbered file at the top.  If an earlier file is created but then edited it MIGHT be shown by /TC, but then equally the file could be saved as a new file by whatever app. is amending it.

Steve

Author

Commented:
If I use the /B /O:-N, it actually copy to max file name ( test0514.pgp  ) first then min file name ( test0512.pgp ) last.  I need the other way.  By the way, I have to take EXIT because I have one more dos command after the copy.  

Commented:
if you use dir /b and /o-n then it should use the first filename being your highest number then exit.... The other way being no exit and keep using set for all entries so it ends up with the last value in the list.

Author

Commented:
Yes, use the  EXIT will work as expected.  

But, using EXIT will ext the batch and the remaining code will be ignored.  How to continue the remaining code?  Thanks.

Commented:
thats what exit /b or goto :eof to break out should do...

Author

Commented:
Here is the code.  First line exit will exit the batch,  the second line cp command will not execute.  

FOR /F "tokens=*" %%I IN ('DIR c:\*.gpg /B /O:-N') DO COPY/y "c:\%%I" c:\sdcr.gpg & exit /b

cp c:\test.csv c:\test2.csv

Author

Commented:
I will credit and close the question.  I will make a new post for the EXIT question.  Thanks.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial