Batch File to rename file for pervious month

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
SQLSearcherAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

FloraCommented:
please find attached the excel tool that does the job
Batch-File-Renamer.xls
0
FloraCommented:
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
ste5anSenior DeveloperCommented:
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
Cloud Class® Course: Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

Steve KnightIT ConsultancyCommented:
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
SQLSearcherAuthor Commented:
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
Steve KnightIT ConsultancyCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Bill PrewCommented:
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
SQLSearcherAuthor Commented:
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
Steve KnightIT ConsultancyCommented:
Np, oh yes I missed of an .xls on the if exist line, well spotted.

Steve
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Windows Batch

From novice to tech pro — start learning today.