scottrwalker
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.tx t moves to and becomes c:\testfolder2\testfile201 10923.txt
Example:
c:\testfolder1\testfile.tx
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
@Scott: Are you still on this one?
Here's a version that will work, whatever your date format.
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
And while at it, you may want to move all files in the same folder...
Cheers,
Rene
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
@scottrwalker
Any feedback on this?
~bp
Any feedback on this?
~bp
ASKER
I got the basics from this comment and found some extra information too.
Open in new window