# Renaming multiple files using batch file



## fibre_on (Feb 1, 2012)

Hi guys,
I'm working on a batch file for quite a while now but I can't solve a problem.
I'm working on a Windows Vista system and I created a *.cmd file using the Windows Editor.

Description of my task:
Within the script I ask the user to input several variables, i.e. "VORJAHR" (i.e. 2011), "VORMONAT" (i.e. 12), "JAHR" (i.e. (2012). "MONAT" (i.e. 01).
I do then take those variables to search for files in several folders and subfolders (which is the variable "VORLAGENORDNER") having used "VORJAHR_VORMONAT" within their filenames. The filenames are structured either ABCDEFG_2011_12.doc or ABCDEFG_2011_12_en.doc, while "ABCDEFG" is not defined in length or structure.
Now, I want to replace the variables "VORJAHR_VORMONAT" (i.e. 2011_12) with "JAHR_MONAT" (i.e. 2012_01).
In other words the target files should be renamed from ABCDEFG_2011_12.doc to ABCDEFG_2012_01.doc. 

My command line so far:
forfiles /p %VORLAGENORDNER% /m *%VORJAHR_VORMONAT%* /s /C "cmd /c ren @file *%JAHR_MONAT%*"

What do I do wrong?
I hope that someone can help me on that account.

Thanks in advance!
fibre


----------



## Squashman (Apr 4, 2003)

Going to assume english is not your first language. Can you translate your variable names into English or give me a better description of what each variable is.

You definitely cannot do this with the FORFILES command. But you will be using the FOR command and if I am understanding you correctly this will be just a simple string substitution using the SET command.


----------



## fibre_on (Feb 1, 2012)

Hi Squashman,
yeah you're right. English is not my mother tongue... 

Those are the translations:
VORJAHR = previous year
VORMONAT = previous month
JAHR = actual year
MONAT = actual month
VORLAGENORDNER = folder in which the files are located

I already tried the SET command but it did not lead to success...


----------



## Squashman (Apr 4, 2003)

Well lets give you a little tutorial on using the SET command. Just going to hard code all the variables.

```
@echo off
setlocal enabledelayedexpansion
set filename=ABCDE_2011_12.txt
set pyear=2011
set pmonth=12
set ayear=2012
set amonth=01
set nfilename=!filename:%pyear%_%pmonth%=%ayear%_%amonth%!
echo Original file: %filename%
echo New File: %nfilename%
```
Now when you run this batch file it outputs the following.

```
E:\batch files\SET_example>set_example.bat
Original file: ABCDE_2011_12.txt
New File: ABCDE_2012_01.txt
```


----------



## fibre_on (Feb 1, 2012)

If I just run the code as shown above the output on screen is correct. Thanks for that.

How can I look up the filenames in a folder containing subfolders which have the variable "2011_12" string? In my original command I tried to add a mask, i.e. "/m *%VORJAHR_VORMONAT%*".


----------



## Squashman (Apr 4, 2003)

You can't use the FORFILES command in this instance because you need to do SET statements to do the string replacement. Nor were you using the Variables correctly with your /M switch.

So to get a directory listing I would do this.

```
REM setting the source directory
PUSHD "%VORLAGENORDNER%" 

REM this gives you a list of all the files in the directory that match your previous month and year
FOR /F "tokens=* delims=" %%G in ('dir /a-d /b /s *%VORJAHR%_%VORMONAT%*') do (
     echo %%G
     PUT THE REST OF YOUR CODE HERE TO RENAME THE FILE NAMES
)
```


----------



## fibre_on (Feb 1, 2012)

Hm, still got some probs:
Taking into account your statements above the modified code will be:


PUSHD "%VORLAGENORDNER%" 
REM 
FOR /F "tokens=* delims=" %%G in ('dir /a-d /b /s *%VORJAHR%_%VORMONAT%*') do (
echo %%G
set tokens=!tokens:%VORJAHR%_%VORMONAT%=%JAHR%_%MONAT%!
)


Is that correct?


----------



## Squashman (Apr 4, 2003)

If you can please use Bulletin Board CODE tags around your code like I did.

Should be something like this.

```
@echo off
setlocal enabledelayedexpansion

PUSHD "%VORLAGENORDNER%" 

FOR /F "tokens=* delims=" %%G in ('dir /a-d /b /s *%VORJAHR%_%VORMONAT%*') do (
	SET FILENAME=%%nxG
	set NEWNAME=!FILENAME:%VORJAHR%_%VORMONAT%=%JAHR%_%MONAT%!
)
```


----------



## fibre_on (Feb 1, 2012)

I ran the script with the above code 

```
@echo off
setlocal enabledelayedexpansion
 
PUSHD "%VORLAGENORDNER%" 
 
FOR /F "tokens=* delims=" %%G in ('dir /a-d /b /s *%VORJAHR%_%VORMONAT%*') do (
    SET FILENAME=%%nxG
    set NEWNAME=!FILENAME:%VORJAHR%_%VORMONAT%=%JAHR%_%MONAT%!
)
```
but the output on screen just was

```
%nxG
```
The filename itself had been untouched.


----------



## Squashman (Apr 4, 2003)

You need to show me the exact code you used.
The above code snippet I gave you isn't going to do anything because none of the variables have been assigned. It is basically the base code you need to use to get the main function of the program to work. You needed to fill in the rest of asking your users for input and such.


----------



## fibre_on (Feb 1, 2012)

I tried to integrate your code into my code but with no success.

Unfortunately today I had to give up my activities on that behalf.

Thanks very much for your effort on my task. I still learned a lot though!


----------



## Squashman (Apr 4, 2003)

So what is wrong with posting the code you used here for me to see it? I am not a big fan of giving up on something. Especially something as easy as this.


----------



## PatrickMc (Jun 5, 2009)

Slightly different approach using biterscript.


```
var str list, file, newname
# Collect list of files
lf -n "*" "C:/Somefolder" > $list
while ($list <> "")
do
    # Get next file from list
    lex "1" $list > $file
     # Form new name by replacing 2011 with 2012
    sal -p -c "^2011^" "2012" $file > $newname
     # Rename
    system rename $file $newname
done
```
Can pass "2011" and "2012" as values to 2 string variables from outside.


----------

