Solved

Batch file to move and rename

Posted on 2011-09-23
6
182 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 51

Accepted Solution

by:
Bill Prew earned 250 total points
Comment Utility
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
Comment Utility
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
Comment Utility
@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
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
LVL 10

Expert Comment

by:ReneGe
Comment Utility
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 51

Expert Comment

by:Bill Prew
Comment Utility
@scottrwalker

Any feedback on this?

~bp
0
 

Author Closing Comment

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

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Suggested Solutions

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…
YESTERDAY YESTERDAY.BAT is inspired by a previous article I wrote entitled: TOMORROW.BAT (http://www.experts-exchange.com/OS/Microsoft_Operating_Systems/MS_DOS/A_4196-Advanced-Batch-File-Programming-TOMORROW-BAT.html). The crux of this batch f…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

763 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

13 Experts available now in Live!

Get 1:1 Help Now