Solved

batch file to rename file with todays date.

Posted on 2013-12-16
2
316 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 84

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

Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How to run a .bat file from a no name folder 5 53
bash script to unzip a file 5 60
Import filename in batch 2 36
batch file problem 5 67
The following is a collection of cases for strange behaviour when using advanced techniques in DOS batch files. You should have some basic experience in batch "programming", as I'm assuming some knowledge and not further explain the basics. For some…
How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

809 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