Link to home
Start Free TrialLog in
Avatar of scottrwalker
scottrwalkerFlag for United States of America

asked on

Batch file to move and rename

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
ASKER CERTIFIED SOLUTION
Avatar of Bill Prew
Bill Prew

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

@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

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

Avatar of Bill Prew
Bill Prew

@scottrwalker

Any feedback on this?

~bp
Avatar of scottrwalker

ASKER

I got the basics from this comment and found some extra information too.