batch file to change folder name with time stamp

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
mikeyz
Posts: 2
Joined: 20 Mar 2013 17:42

batch file to change folder name with time stamp

#1 Post by mikeyz » 20 Mar 2013 17:52

Hello

I would like to have a batch file that adds a time stamp to a folder name if finds it in the directory.
For example. I have a folder that has folder names like this:
03-11-2013
03-12-2013
03-12-2013

if the script finds a folder with todays date . it will add a time stamp to it.
please see example below

03-11-2013
03-12-2013_20-52-43 <-- todays date
03-12-2013_22-45-30 < -- next time the batch file ran and found new folder created with todays date.

So when the script ran it found a folder with 03-12-2013 (todays date) it add the time stamp after it. and then later the script found another folder that was created and add the time stamp. etc....etc...

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: batch file to change folder name with time stamp

#2 Post by foxidrive » 20 Mar 2013 23:05

Your date format is ambiguous. Is it MM-DD-YYYY?

Where does the timestamp come from?

mikeyz
Posts: 2
Joined: 20 Mar 2013 17:42

Re: batch file to change folder name with time stamp

#3 Post by mikeyz » 21 Mar 2013 11:14

Yes

date format is MM-DD-YYYY . the dated folder is place there by another program that does a different process.

Timestamp should be current system time. any format of the time layout is acceptable.

thanks Mike

mfm4aa
Posts: 70
Joined: 13 Feb 2013 14:02
Location: Europe

Re: batch file to change folder name with time stamp

#4 Post by mfm4aa » 21 Mar 2013 12:22

Here we go:

Code: Select all


@echo off &setlocal
set "startfolder=X:\PathTo\start"

pushd "%startfolder%"
for /d %%i in (*.*) do call:process "%%~i"
popd
goto:eof

:process
set "fname=%~nx1"
set "fname=%fname:~0,10%"
if "%fname%" neq "%date%" goto:eof
set "fname=%fname%_%time%"
set "fname=%fname::=-%"
ren "%~1" "%fname%"
goto:eof

endlocal
Last edited by mfm4aa on 22 Mar 2013 04:45, edited 1 time in total.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: batch file to change folder name with time stamp

#5 Post by foxidrive » 22 Mar 2013 02:39

The %date% may not be what is needed as regional settings affect it. Using WMIC is a good option.

It shouldn't walk the directory tree, according to the request...

mfm4aa
Posts: 70
Joined: 13 Feb 2013 14:02
Location: Europe

Re: batch file to change folder name with time stamp

#6 Post by mfm4aa » 22 Mar 2013 04:44

My code doesn't make any assumptions about the date format (except the length of 10).
I removed the /r option from the for loop & added pushd/popd, thanks.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: batch file to change folder name with time stamp

#7 Post by foxidrive » 22 Mar 2013 05:57

mfm4aa wrote:My code doesn't make any assumptions about the date format (except the length of 10).


%date% can have a day name in the string, and may not be in MM-DD-YYYY format.

using Wmic to get the date is region insensitive. Using VBS and Richie Lawrence's code are also options for get the date in a predictable manner.

Post Reply