Solved

batch file to rename file with todays date.

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

When you receive another warning that your shared drive is almost full and you have asked your users to clean out old files again and again, here is a single command that may help. This command will place all the files that have not been used rec…
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…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

746 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

12 Experts available now in Live!

Get 1:1 Help Now