s2nrbald Posted August 1, 2012 Posted August 1, 2012 Hello,I have a batch file which copies a file and appends today's date. I would like to do the same, but append the last modified date. Can someone help?Here is my existing code to append today's date:@echo offset today=%date:~6,4%-%date:~3,2%-%date:~0,2%:loop copy %1 "%~dpn1_%today%%~x1" if .%2==. goto end shift goto loop:end
jaclaz Posted August 1, 2012 Posted August 1, 2012 (edited) Can someone help?Yes. http://homepage.ntlworld.com./jonathan.deboynepollard/FGA/questions-with-yes-or-no-answers.htmlEasier to get the "last modified date" is to actually DIR the file.Say that you have a file c:\test.txtDir C:\test.txt will show you something like (this is my Italian interface):C:\>dir test.txt Il volume nell'unità C non ha etichetta. Numero di serie del volume: C08C-CFD9 Directory di C:\19/12/2011 12:05 62 Test.txt 1 File 62 byte 0 Directory 147.178.754.048 byte disponibiliThen try:C:\>dir /TC test.txtto get the Creation date)C:\>dir /TA test.txt This is the (last) Access date.Same goes for /TW for (last) Write dateParsing any of these three commands results amounts more or less to:@ECHO OFFFOR /F "tokens=1,2,3 delims=/ " %%A IN ('DIR /TA C:\test.txt ^| FIND /I "test.txt"') DO (SET A_Day=%%ASET A_Month=%%BSET A_Year=%%C)SET A_But see this thread also :jaclaz Edited August 1, 2012 by jaclaz
s2nrbald Posted August 1, 2012 Author Posted August 1, 2012 Thank you for your support.Now a new question. How do I get your input into the form required? For my previous code mentioned above, I saved this file as backup.bat. I then have a shortcut to this on my desktop. When I drag a file to the shortcut, it automatically adds a copy of the file on the desktop with today's date added. In this new example I would like to do something similar (add a shortcut to this new .bat file, drag some file to it, and then let it create a copy of it with the last modified date appended.) Taking it one step further, it would be nice if it also could just rename the file, without even making a copy.
jaclaz Posted August 1, 2012 Posted August 1, 2012 Thank you for your support.You are welcome Now a new question. How do I get your input into the form required? An interesting one.Possibly adapting the example snippet to what you want to do .I took the original request as:I want to write a new batch file, similar to the one I already wrote, but I don't know how to get the Last Modified date/time of file; how can I do this?And not as:I had someone write for me a batch file.I need someone to write a new one for me with these other features: ......Had you actually READ the given thread, you might have read also this post :Since "your" batch already uses variable expansion:http://www.robvanderwoude.com/ntfor.php and the t element of an expanded variable represents date/time (last modified one)."Your" batch becomes nicely:@echo offset LM_date=%~t1set today=%LM_date:~6,4%-%LM_date:~3,2%-%LM_date:~0,2%:loopcopy %1 "%~dpn1_%today%%~x1"if .%2==. goto endshiftgoto loop:endThe general idea was to give you some missing pieces (of various puzzles ) but let you find the way to re-compose the puzzle.... jaclaz
Yzöwl Posted August 1, 2012 Posted August 1, 2012 Please also bear in mind that in may be prudent to incorporate a check that the 'file' does not already have that date incorporated within its file name. It may then also be necessary to ensure that conflicts of files modified more than once in any day are catered for adequately in your copy procedure.
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now