Link to home
Start Free TrialLog in
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
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

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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

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
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.
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.  

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.
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.
thats what exit /b or goto :eof to break out should do...
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
I will credit and close the question.  I will make a new post for the EXIT question.  Thanks.