Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Rename file if exist to file.old.%date%

Posted on 2013-06-04
12
Medium Priority
?
3,837 Views
Last Modified: 2013-06-04
Hey Experts.  I am running a script that is dumping out data from our AD environment and the data goes into a log file.  I run this script a couple times a week and I have to remember to rename the file .date before running the script.  Because my memory is not the best, I would like the batch script to run and if the log file already exists that the existing file will be renamed to .old.%date%.  Example:

data.log should be renamed to data.old.060413

The format of the date doesn't matter.  It could be julian date or month/date.  I just need it to be renamed.  So far all I have is:

if exist c:\tools\file.log rename c:\tools\file.old.%date%

Open in new window


I appreciate your help!
0
Comment
Question by:samiam41
[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
  • 3
  • 2
  • +1
12 Comments
 
LVL 96

Expert Comment

by:Lee W, MVP
ID: 39219842
Why not change your script so that every time it runs, the date stamp is part of the file name.

For example:
file.date.log
0
 
LVL 96

Expert Comment

by:Lee W, MVP
ID: 39219849
There are a couple of ways of incorporating the date into a file name - if you are based in the USA with a US style date format, you can reference the date in a batch file using the following format:

%date:~-4%-%date:4,2%-%date:7,2%
That should output a date in the format:
2013-06-04
YYYY-MM-DD (best for sorting)

Other methods exist for retrieving the date values and assigning them to a variable for use in a batch file, but this is the quickest if your date variables match mine.
0
 
LVL 96

Expert Comment

by:Lee W, MVP
ID: 39219850
To be clear:
if exist c:\tools\file.log rename c:\tools\file.old.%date:~-4%-%date:4,2%-%date:7,2%
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 9

Author Comment

by:samiam41
ID: 39219852
Hey leew!!  Great to see you again.

That works for me.  So, how do I do it?  :)

rename file.log file.date.log
0
 
LVL 9

Author Comment

by:samiam41
ID: 39219856
Oops.  Looks like you already posted it.  Thanks Leew.  Testing it now.
0
 
LVL 43

Assisted Solution

by:Steve Knight
Steve Knight earned 400 total points
ID: 39219861
For a quick and dirty rename you can also use %date:/=-% to replace any / with - in the date.  Be careful if you schedule it though or run as a different user etc. as it could easily have a different date format and need different %date:...% string editing bits.  For this reason I always use technique like this unless very specific pc/user to run on:

http://www.experts-exchange.com/OS/Microsoft_Operating_Systems/MS_DOS/A_1153-Using-dates-in-batch-files-scripts.html

Steve
0
 
LVL 85

Accepted Solution

by:
oBdA earned 1600 total points
ID: 39219878
Personally, I'd rename it not to the current date, but rather to the date (and time, if required) that the file was created.
This is currently in test mode and will only display the ren command; remove the uppercase ECHO in line 9 to run it for real. If the time format doesn't fit, please post the result (the file time will be echoed in line 10, you can of course remove this once it works):
@echo off
setlocal enabledelayedexpansion
set LogFile=C:\Tools\file.log
if not exist "%LogFile%" goto SkipRename
for %%f in ("%LogFile%") do (
	for /f "tokens=1-3 delims=/ " %%a in ("%%~tf") do (set TimeStamp=%%c%%a%%b)
	set NewName=%%~nf.old.!TimeStamp!%%~xf
	echo Renaming "%LogFile%" to "!NewName!" ...
	ECHO ren "%LogFile%" "!NewName!"
	ECHO File date: %%~tf
)
:SkipRename

Open in new window

0
 
LVL 9

Author Comment

by:samiam41
ID: 39220041
leew, when I run your command from a batch script, this is what I get:
C:\Tools>if exist c:\tools\file.log rename c:\tools\file.old.2013-date:4,2date:7,2

Nothing shows up in the directory.
0
 
LVL 9

Author Comment

by:samiam41
ID: 39220047
oBdA, just entered in your code, removed the safety cap (echo on line 9) and rolled it out.  Worked like I expected!  Thanks.
0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 39220073
For reference Leew just missed two ~ out of the date commands.  You can see what the bits used mean in set /? etc.

if exist c:\tools\file.log rename c:\tools\file.old.%date:~-4%-%date:~4,2%-%date:~7,2%
0
 
LVL 9

Author Comment

by:samiam41
ID: 39220089
Thanks dragon-it but I'm not sure what I'm doing wrong.  Now I'm getting
"The syntax of the command is incorrect."

I got a solution.  I'll leave it as an Unsolved Mystery.
0
 
LVL 9

Author Closing Comment

by:samiam41
ID: 39220092
Thanks experts!  Rolling with my solution and on to the next challenge.  Take care guys.
0

Featured Post

Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

Question has a verified solution.

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

The recent Microsoft changes on update philosophy for Windows pre-10 and their impact on existing WSUS implementations.
Assume you have an outside contractor who comes in seasonally or once a week to do some work in your office, but you only want to give him access to the programs and files he needs and keep all other documents and programs private. Can you do this o…
This Micro Tutorial will give you a introduction in two parts how to utilize Windows Live Movie Maker to its maximum capability. This will be demonstrated using Windows Live Movie Maker on Windows 7 operating system.
The viewer will learn how to successfully download and install the SARDU utility on Windows 7, without downloading adware.
Suggested Courses

618 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