Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Batch File to rename file for pervious month

Posted on 2014-10-24
9
Medium Priority
?
377 Views
Last Modified: 2014-10-28
Hello Experts Exchange
I have a folder that has 12 files, 1 file for every month in the year.

The file names are as follows;
Lost Time.xls
Lost Time_01.xls
Lost Time_02.xls
Lost Time_03.xls
Lost Time_04.xls
Lost Time_05.xls
Lost Time_06.xls
Lost Time_07.xls
Lost Time_08.xls
Lost Time_09.xls
Lost Time_11.xls
Lost Time_12.xls

Currently the Lost Time.xls file holds this months data in it.

At the start of next month I want a batch file to rename Lost Time.xls to Lost Time_10.xls, and to rename Lost Time_11.xls to Lost Time.xls.

I want the batch file to be dynamic so it can do the renaming of the files for every month of the year.

Is this possible?

Regards

SQLSearcher
0
Comment
Question by:SQLSearcher
[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
  • 3
  • 2
  • 2
  • +2
9 Comments
 
LVL 6

Expert Comment

by:Flora
ID: 40401291
please find attached the excel tool that does the job
Batch-File-Renamer.xls
0
 
LVL 6

Expert Comment

by:Flora
ID: 40401292
if you do not want it by excel then you can use freeware Bulk Rename Utility  portable version

http://www.portablefreeware.com/?id=1008
0
 
LVL 35

Expert Comment

by:ste5an
ID: 40401296
I would strongly recommend that you name your files after the month of the year, like lost-time-2014-10.xls, when possible. And handle the moving window in your application.
0
Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

 
LVL 43

Expert Comment

by:Steve Knight
ID: 40401719
Would say the same too re: keeping names... BUT if you want what you asked for try this.. on a copy of your directory, or having kept a copy...

Steve

@echo off

REM ================================================================================================
REM Define filename to look for
REM ================================================================================================
Set Filename=Lost_Time

REM ================================================================================================
REM Move to correct directory for the files
REM ================================================================================================
CD /D "c:\dir\with\files\in"

REM ================================================================================================
REM Make temporary VBScript file to get date in correct format regardless of local date format
REM ================================================================================================
ECHO wscript.echo right(100+month(date),2),right(100+month(date)-1,2) >"%temp%\dateparts.vbs"
FOR /F "TOKENS=1,2 delims=," %%a IN ('cscript //nologo "%temp%\dateparts.vbs"') DO (SET mm=%%a) & (set mlast=%%b)
ECHO The month is %mm% and last month is %mlast%

REM ================================================================================================
REM If month is January then change last month to December
REM ================================================================================================
If "%mlast%"=="00" set mlast=12

REM ================================================================================================
REM Check if last months file already exists and if not rename it to that
REM ================================================================================================
IF exist "%filename%_%mlast%.xls" (
  echo There is already a file for last month so can't rename file
  pause
  exit /b
) ELSE (
  RENAME "%filename%.xls" "%filename%_%mlast%.xls"
)

REM ================================================================================================
REM Check if this months file exists and rename it if so.
REM ================================================================================================
If exist "%filename%_%mm%" (
  RENAME "%filename%_%mm%.xls" "%filename%.xls"
) ELSE (
  echo There is no file for this month to rename
  pause
  exit /b
)

Open in new window

0
 

Author Comment

by:SQLSearcher
ID: 40402083
Hello Steve
Your code is what I looking for, but I have tried it and it does not work.

When I run it, it says the following;
The month is 10 09 and last month is
The system cannot find the file specified.
There is no file for this month to rename
Press any key to continue...


I think its a problem with the last month variable, I had a play but could not work it out.  Would you take a look again for me please?

Regards

SQLSearcher
0
 
LVL 43

Accepted Solution

by:
Steve Knight earned 2000 total points
ID: 40402095
Apologies, try this, had missed the delimeter out of the vbscript line so it wasn't adding a comma inbetween the results.

@echo off

REM ================================================================================================
REM Define filename to look for
REM ================================================================================================
Set Filename=Lost_Time

REM ================================================================================================
REM Move to correct directory for the files
REM ================================================================================================
CD /D "c:\dir\with\files\in"

REM ================================================================================================
REM Make temporary VBScript file to get date in correct format regardless of local date format
REM ================================================================================================
ECHO wscript.echo right(100+month(date),2) ^& "," ^& right(100+month(date)-1,2) >"%temp%\dateparts.vbs"
FOR /F "TOKENS=1,2 delims=," %%a IN ('cscript //nologo "%temp%\dateparts.vbs"') DO (SET mm=%%a) & (set mlast=%%b)
ECHO The month is %mm% and last month is %mlast%

REM ================================================================================================
REM If month is January then change last month to December
REM ================================================================================================
If "%mlast%"=="00" set mlast=12

REM ================================================================================================
REM Check if last months file already exists and if not rename it to that
REM ================================================================================================
IF exist "%filename%_%mlast%.xls" (
  echo There is already a file for last month so can't rename file
  pause
  exit /b
) ELSE (
  RENAME "%filename%.xls" "%filename%_%mlast%.xls"
)

REM ================================================================================================
REM Check if this months file exists and rename it if so.
REM ================================================================================================
If exist "%filename%_%mm%" (
  RENAME "%filename%_%mm%.xls" "%filename%.xls"
) ELSE (
  echo There is no file for this month to rename
  pause
  exit /b
)
                                          

Open in new window

0
 
LVL 57

Expert Comment

by:Bill Prew
ID: 40402325
Just curious, am I reading this right, that the following are true:

- you always want exactly 12 files in the folder
- you have a process that writes the current month data to the file name "lost_time.xls"
- you want to have multiple years data in each of these file, but just the data for that month

Trying to understand better what the context is in case there are any suggestions I could make.

~bp
0
 

Author Comment

by:SQLSearcher
ID: 40407952
Hello Steve
I had to make two changes and now it is working great, thank you for your help.

Here is the working code;

REM @echo off

REM ================================================================================================
REM Define filename to look for
REM ================================================================================================
Set Filename=Lost Time

REM ================================================================================================
REM Move to correct directory for the files
REM ================================================================================================
CD /D "F:\Graham Rock\T and A\Losses\Summary"

REM ================================================================================================
REM Make temporary VBScript file to get date in correct format regardless of local date format
REM ================================================================================================
ECHO wscript.echo right(100+month(date),2) ^& "," ^& right(100+month(date)-1,2) >"%temp%\dateparts.vbs"
FOR /F "TOKENS=1,2 delims=," %%a IN ('cscript //nologo "%temp%\dateparts.vbs"') DO (SET mm=%%a) & (set mlast=%%b)
ECHO The month is %mm% and last month is %mlast%

REM ================================================================================================
REM If month is January then change last month to December
REM ================================================================================================
If "%mlast%"=="00" set mlast=12

REM ================================================================================================
REM Check if last months file already exists and if not rename it to that
REM ================================================================================================
IF exist "%filename%_%mlast%.xls" (
  echo There is already a file for last month so can't rename file
  pause
  exit /b
) ELSE (
  RENAME "%filename%.xls" "%filename%_%mlast%.xls"
)

REM ================================================================================================
REM Check if this months file exists and rename it if so.
REM ================================================================================================
If exist "%filename%_%mm%.xls" (
  RENAME "%filename%_%mm%.xls" "%filename%.xls"
) ELSE (
  echo There is no file for this month to rename
  pause
  exit /b
)

Open in new window



Regards

SQLSearcher
0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 40408104
Np, oh yes I missed of an .xls on the if exist line, well spotted.

Steve
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

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…
AutoHotkey is an excellent, free, open source programming/scripting language for Windows. It started out as a keyboard/mouse macros product, but has expanded into a robust language. This article provides an introduction to it, with links to addition…
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

715 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