Solved

Batch File to rename file for pervious month

Posted on 2014-10-24
9
310 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
  • 3
  • 2
  • 2
  • +2
9 Comments
 
LVL 5

Expert Comment

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

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 32

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
 
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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 

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 500 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 51

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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

One of my most closely kept secrets is revealed in this discussion How to output text on the same line This question was recently posted in EE by Simon336697 (http://www.experts-exchange.com/Programming/Languages/Scripting/Shell/Batch/Q_2459…
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…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

757 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

19 Experts available now in Live!

Get 1:1 Help Now