Solved

Batch file to move and rename

Posted on 2011-09-23
6
183 Views
Last Modified: 2012-05-12
I am looking for a batch file or command line set to move a file from one folder to another, yet in teh process, add the date as part of hte moved filename.

Example:
c:\testfolder1\testfile.txt moves to and becomes c:\testfolder2\testfile20110923.txt
0
Comment
Question by:scottrwalker
6 Comments
 
LVL 52

Accepted Solution

by:
Bill Prew earned 250 total points
ID: 36589944
Here's a simple approach assuming your date format is MM/DD/YYYY.

set Stamp=%DATE:~-4%%DATE:~-10,2%%DATE:~-7,2%
move "c:\testfolder1\testfile.txt" "c:\testfolder2\testfile%Stamp%.txt"

Open in new window

~bp
0
 
LVL 14

Expert Comment

by:athomsfere
ID: 36589962
This should do it

@Echo Off

	For /F "tokens=2,3,4 delims=/ " %%A in ('Date /t') do @( 
	Set Day=%%A
	Set Month=%%B
	Set Year=%%C
	Set All=%%C%%B%%A
	)
	
	Copy c:\testfolder1\testfile.txt c:\testfolder2\
	cd C:\testfolder2\
	ren testfile.txt testfile%all%.txt

Open in new window

0
 
LVL 10

Expert Comment

by:ReneGe
ID: 36899431
@Scott: Are you still on this one?

Here's a version that will work, whatever your date format.

@ECHO OFF
CALL :GetDateTime
MOVE /Y "c:\testfolder1\testfile.txt" "c:\testfolder2\testfile%TheDate%.txt"
EXIT

:GetDateTime
REM READ DATE
FOR /F "skip=1 tokens=1-6" %%A IN ('WMIC Path Win32_LocalTime Get Day^,Hour^,Minute^,Month^,Second^,Year /Format:table') DO (
	IF %%A GTR 0 (
	SET DayVal=%%A
	SET HourVal=%%B
	SET MinVal=%%C
	SET MonthVal=%%D
	SET SecVal=%%E
	SET YearVal=%%F
	)
)

REM ADDING LEADING "0"
	IF %DayVal% LSS 10   (SET Day=0%DayVal%) 		ELSE (SET Day=%DayVal%)
	IF %HourVal% LSS 10  (SET Hour=0%HourVal%) 		ELSE (SET Hour=%HourVal%)
	IF %MinVal% LSS 10   (SET Min=0%MinVal%) 		ELSE (SET Min=%MinVal%)
	IF %MonthVal% LSS 10 (SET Month=0%MonthVal%) 	ELSE (SET Month=%MonthVal%)
	IF %SecVal% LSS 10   (SET Sec=0%SecVal%) 		ELSE (SET Sec=%SecVal%)
	SET Year=%YearVal%

	SET TheDate=%Year%-%Month%-%Day%
	SET TheTime=%Hour%.%Min%.%Sec%


EXIT /b

Open in new window

0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 10

Expert Comment

by:ReneGe
ID: 36901448
And while at it, you may want to move all files in the same folder...

Cheers,
Rene
@ECHO OFF
CALL :GetDateTime

SET SourceFolder=C:\Source
SET DestinationFolder=C:\Dest

FOR /F "delims=" %%A in('DIR /B "%SourceFolder%"') DO MOVE /Y "%%~fA" "%DestinationFolder%\%%~nA%TheDate%.%%~xA

EXIT

:GetDateTime
REM READ DATE
FOR /F "skip=1 tokens=1-6" %%A IN ('WMIC Path Win32_LocalTime Get Day^,Hour^,Minute^,Month^,Second^,Year /Format:table') DO (
	IF %%A GTR 0 (
	SET DayVal=%%A
	SET HourVal=%%B
	SET MinVal=%%C
	SET MonthVal=%%D
	SET SecVal=%%E
	SET YearVal=%%F
	)
)

REM ADDING LEADING "0"
	IF %DayVal% LSS 10   (SET Day=0%DayVal%) 		ELSE (SET Day=%DayVal%)
	IF %HourVal% LSS 10  (SET Hour=0%HourVal%) 		ELSE (SET Hour=%HourVal%)
	IF %MinVal% LSS 10   (SET Min=0%MinVal%) 		ELSE (SET Min=%MinVal%)
	IF %MonthVal% LSS 10 (SET Month=0%MonthVal%) 	ELSE (SET Month=%MonthVal%)
	IF %SecVal% LSS 10   (SET Sec=0%SecVal%) 		ELSE (SET Sec=%SecVal%)
	SET Year=%YearVal%

	SET TheDate=%Year%-%Month%-%Day%
	SET TheTime=%Hour%.%Min%.%Sec%


EXIT /b

Open in new window

0
 
LVL 52

Expert Comment

by:Bill Prew
ID: 37123485
@scottrwalker

Any feedback on this?

~bp
0
 

Author Closing Comment

by:scottrwalker
ID: 37321523
I got the basics from this comment and found some extra information too.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

The following is a collection of cases for strange behaviour when using advanced techniques in DOS batch files. You should have some basic experience in batch "programming", as I'm assuming some knowledge and not further explain the basics. For some…
VALIDATING DATES One method of validating dates is to jam the date into the DATE command and see if it accepts it by examining the system's errorlevel value. A non-zero result indicates failure. A typical example might look something like the fol…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

929 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now