Avatar of ewang1205
ewang1205

asked on 

copy the max date file using dos command not working

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
Microsoft DOS

Avatar of undefined
Last Comment
ewang1205
Avatar of Ben Personick (Previously QCubed)
Ben Personick (Previously QCubed)
Flag of United States of America image

Please clarify your question..
SOLUTION
Avatar of oBdA
oBdA

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
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

ASKER CERTIFIED SOLUTION
Avatar of Steve Knight
Steve Knight
Flag of United Kingdom of Great Britain and Northern Ireland image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
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

Avatar of Qlemo
Qlemo
Flag of Germany image

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.
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
Avatar of Qlemo
Qlemo
Flag of Germany image

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.
Avatar of Steve Knight
Steve Knight
Flag of United Kingdom of Great Britain and Northern Ireland image

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
Avatar of ewang1205
ewang1205

ASKER

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.  

Avatar of Steve Knight
Steve Knight
Flag of United Kingdom of Great Britain and Northern Ireland image

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.
Avatar of ewang1205
ewang1205

ASKER

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.
Avatar of Steve Knight
Steve Knight
Flag of United Kingdom of Great Britain and Northern Ireland image

thats what exit /b or goto :eof to break out should do...
Avatar of ewang1205
ewang1205

ASKER

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
Avatar of ewang1205
ewang1205

ASKER

I will credit and close the question.  I will make a new post for the EXIT question.  Thanks.
Microsoft DOS
Microsoft DOS

Microsoft Disk Operating System (MS-DOS) was an operating system for x86-based personal computers, and traces of it are still found in the Windows operating system. DOS is still used in some embedded systems and for certain legacy 16-bit networks.

14K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo