# Renaming a lot of files using a batch file



## ghe1 (May 1, 2012)

I have a folder with a lot of files having this name convension:

AAAA_AAAA&1&AAAANNNNNNNNNNNN&12345_X_XYZ.png

I want to rename this file to:

1&12345_S_REF.PNG

where A is alfanumeric and N is numeric.

The 1 between the & could be any number. So basically I want to strip the first 10 characters use character 11 followed by & followed by all the characters from 30 onwards.

Anyone able to help?

Many thanks


----------



## Squashman (Apr 4, 2003)

The ampersand has special meaning in batch files. It is a logical AND. Not sure why you want to name your files that way or why you have them that way now.

String parsing is easy enough in batch but the ampersand in your current file names may wreak havoc.

I will see if I can figure out how to escape the characters so that it works but personally I would just get rid of the Ampersand. Otherwise any future batch files you need are always going to have extra code to work around them.


----------



## ghe1 (May 1, 2012)

I know it is a nuisance the ampercent but this is how the files are coming. Instead of using the pipe(|) they use (&) as a saparator.


----------



## Squashman (Apr 4, 2003)

ghe1 said:


> I know it is a nuisance the ampercent but this is how the files are coming. Instead of using the pipe(|) they use (&) as a saparator.


Wow! You really are GREEN to batch files! You realize the pipe has special meaning as well!
A single pipe streams output to the next command.
A double pipe is logical OR.


----------



## Squashman (Apr 4, 2003)

Remove the ECHO in the code when you think it looks like it is working for you. Right now I just have it echoing to the screen what it is going to do.

```
for /F "tokens=1-4 delims=&" %%G in ('dir /a-d /b *.png') do echo rename "%%G^&%%H^&%%I^&%%J" "%%H^&%%J"
```


----------



## ghe1 (May 1, 2012)

thanks - there is a slight problem
when I run it with echo I get 
rename "CITY_LINK^&1^&ZZZ0000315290401^&12345_S_REF.png" "1^&12345_S_REF.png"

when I run it without the echo I get
rename "CITY_LINK^&1^&ZZZ0000315290401^&12345_S_REF.png" "1^&12345_S_REF.png"
The system cannot find the file specified.

I think it is to do with the position of the ampercent.

I'll try to work out to see if it is just the position.


----------



## ghe1 (May 1, 2012)

I took out the ^ and it worked.

Thanks very much - you're great.

Would it be too much to ask you to tell me what 


> %%G^&%%H^&%%I^&%%J means *%%G^&%%H^&%%I^&%%J* means in your code


----------



## Squashman (Apr 4, 2003)

The ^ escapes the & from working like a logical AND.

That is how I tested it an it worked for me.


----------



## Squashman (Apr 4, 2003)

Now I know why.
I originally tested it without "QUOTES" around the file names. When you put quotes around the file names it will keep the ^ in the file name instead of using it as the escape. I had put the quotes in there just in case one of your underscores might have turned into a space. Any file name with a space needs quotes around it.


----------

