Solved

Batch file to move and rename

Posted on 2011-09-23
6
186 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 54

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
On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

 
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 54

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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Being a system administrator some time we require to do things remotely, one of them is installing software. Here I am going to tell you how to install software through wmic (Windows management instrument console). I am not at all saying that this i…
Introduction: Recently, I got a requirement to zip all files individually with batch file script in Windows OS. I don't know much about scripting, but I searched Google and found a lot of examples and websites to complete my task. Finally, I was ab…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

756 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