Jump to content

Recommended Posts

Posted

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 off

set today=%date:~6,4%-%date:~3,2%-%date:~0,2%

:loop

copy %1 "%~dpn1_%today%%~x1"

if .%2==. goto end

shift

goto loop

:end


Posted (edited)

Can someone help?

Yes. :)

http://homepage.ntlworld.com./jonathan.deboynepollard/FGA/questions-with-yes-or-no-answers.html

Easier to get the "last modified date" is to actually DIR the file.

Say that you have a file c:\test.txt

Dir 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 disponibili

Then try:

C:\>dir /TC test.txt

to get the Creation date)

C:\>dir /TA test.txt

This is the (last) Access date.

Same goes for /TW for (last) Write date

Parsing any of these three commands results amounts more or less to:


@ECHO OFF
FOR /F "tokens=1,2,3 delims=/ " %%A IN ('DIR /TA C:\test.txt ^| FIND /I "test.txt"') DO (
SET A_Day=%%A
SET A_Month=%%B
SET A_Year=%%C
)
SET A_

But see this thread also :rolleyes: :

jaclaz

Edited by jaclaz
Posted

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.

Posted

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

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 :realmad: the given thread, you might have read also this post :yes: :

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 off

set LM_date=%~t1

set today=%LM_date:~6,4%-%LM_date:~3,2%-%LM_date:~0,2%

:loop

copy %1 "%~dpn1_%today%%~x1"

if .%2==. goto end

shift

goto loop

:end

The general idea was to give you some missing pieces (of various puzzles :w00t:) but let you find the way to re-compose the puzzle.... ;)

jaclaz

Posted

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.

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...