Solved

batch file to rename file with todays date.

Posted on 2013-12-16
2
315 Views
Last Modified: 2013-12-16
Hi,

I would like help to create a batch file that will move the file e.g

c:\before.txt

to a new location and change the name with today date.

So the new location would look like this

c:\after_16_12_2013

Thanks
0
Comment
Question by:luketr
2 Comments
 
LVL 83

Accepted Solution

by:
oBdA earned 500 total points
ID: 39721093
Try this; the script will retrieve the date and time using WMI (which seems complicated, but prevents problems with date/time format settings when using %date% or %time%).
I'd recommend to set the time stamp to year_month_date, though (you can do that in line 14); the sorting will be better if you keep a history of these files.
Just set the source and target file paths and names, for the target file, the time stamp will be inserted between name and target extension:
@echo off
setlocal enabledelayedexpansion
set SourceFile=C:\Temp\before.txt
set TargetFile=D:\Temp\after.txt
set /a Line=0
for /f "tokens=1-9" %%a in ('wmic Path Win32_LocalTime Get Day^,DayOfWeek^,Hour^,Minute^,Month^,Quarter^,Second^,WeekInMonth^,Year ^| find /v ""') do (
  set /a Line += 1
  if "!Line!"=="1" (set VarA=%%a&set VarB=%%b&set VarC=%%c&set VarD=%%d&set VarE=%%e&set VarF=%%f&set VarG=%%g&set VarH=%%h&set VarI=%%i)
  if "!Line!"=="2" (set !VarA!=%%a&set !VarB!=%%b&set !VarC!=%%c&set !VarD!=%%d&set !VarE!=%%e&set !VarF!=%%f&set !VarG!=%%g&set !VarH!=%%h&set !VarI!=%%i)
)
for %%a in (Month Day Hour Minute Second) do (if !%%a! LSS 10 set %%a=0!%%a!)
REM *** At this point, the variables Day, DayOfWeek, Hour, Minute, Month, Quarter, Second, WeekInMonth, and Year are set.
REM *** Month, Day, Hour, Minute, Second have leading zeroes if less than 10.
set TimeStamp=%Day%_%Month%_%Year%
echo My time stamp: %TimeStamp%
for %%a in ("%TargetFile%") do set TargetFile=%%~dpna_%TimeStamp%%%~xa
move "%SourceFile%" "%TargetFile%"

Open in new window

0
 

Author Comment

by:luketr
ID: 39721131
worked brilliantly thank you very much.
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

This article was inspired by a question here at Experts Exchange (http://www.experts-exchange.com/Software/Photos_Graphics/Images_and_Photos/Q_28629170.html). The requirements stated in that question are (1) reduce the file size of a large number of…
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…
I designed this idea while studying technology in the classroom.  This is a semester long project.  Students are asked to take photographs on a specific topic which they find meaningful, it can be a place or situation such as travel or homelessness.…
Need to grow your business through quality cloud solutions? With everything required to build a cloud platform and solution, you may feel like the distance between you and the cloud is quite long. Help is here. Spend some time learning about the Con…

948 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

22 Experts available now in Live!

Get 1:1 Help Now