# .bat renaming code



## lool123 (Jan 6, 2012)

Hi, 
Im wondering if there is any kind of guide on how to write the script for .bat scripts?
I have a .bat script which i haven't made, and want to further improve it. I can't post it here though.

Anyway, if there is any guide or something on making such scripts, what's what, like there is w3schools.com for learning other types of scripting.

If there isn't any such guide, then could anyone here help me with what im trying to improve?

A part of the script takes the file that's run through it, and renames it.
It takes %fn% (The file, "%fn%" = Filename), and spits it back out "Moretext %fn%". Like if the filename is "Image JPG_2254", it'll come back out "Summer Image JPG_2254"

But what i want it to do is to come out just "Summer JPG_2254". In other words, to remove the "Image" in the process too.

Even though i can't post the script itself here, i can post the small bit of it which names the output file

```
-o "Finished Images/Summer %fn%.jpg"
```
It also has to be in a way that it can rename any filename, not just "Image JPG_2254", but also for instance "Image JPG_76395".

I hope somebody understands my explanation.
Thank you in advance for any replies.


----------



## Squashman (Apr 4, 2003)

Would be helpful to see the entire batch file or at least the code that is renaming it. I don't see why you can post the code here. It is not like Batch code is proprietary.

The word Summer must be hard coded in the batch script. So you should easily be able to remove that.

In general with your example you could do the following.

rename "%fn%" "Summer%FN:Image=%"
If your filename is *Image JPG_2254* this will remove the word Image and add the word summer to the file name and it should come out like this:*Summer JPG_2254*

```
H:\>dir /b Image*
Image JPG_2254

H:\>echo %fn%
Image JPG_2254

H:\>rename "%fn%" "Summer%fn:Image=%"

H:\>dir /b Summ*
Summer JPG_2254

H:\>
```


----------



## lool123 (Jan 6, 2012)

There is no more code that renames it then what i wrote in the first post - It's renamed when it's done going through the process it's supposed to go through, as an output file (thus the -o).

Anyway, thank you very much, it worked.
A little more elaborate for some other files with longer names, i want the same function as here, in addition to a function that can replace a word in the middle of the filename with a different one.

Right now the code looks like this

```
-o "Finished Images/Summer %fn:Image=%.jpg"
```
And i want it to be able to replace another word with a different one.

If the filename is, for instance "Image Las Palmas Beach JPG_2054", and i want to replace "Beach" with just a dash " - ", how can i do that in the same line of code?

"Image Las Palmas Beach JPG_2054" Become "Summer Las Palmas - JPG_2054"


----------



## Squashman (Apr 4, 2003)

You would need to do separate SET statements to build a new variable before you do the rename command. You can't string substitute multiple strings in one command.

set NewFN=%fn:Image=%
set NewFN=%NewFN:Beach=-%
rename "%fn%" "Summer %NewFN%.jpg"


----------



## lool123 (Jan 6, 2012)

Unfortunately there's only room for one line, since it's the output filename after having run it through a process.
I can probably however, rename it afterwards? (I have very little scripting experience, and only know what i ccan learn out from reading a script)
I put a 

```
goto :rename
```
beneath the lines were the output file was specified

then i put 

```
:rename
rename "Finished Images/Summer %fn%.jpg" "Summer %fn:Image=%.jpg"
rename "Summer %fn:Image=%.jpg" "Summer %fn:Beach=-%.jpg"
```
Lower down in the script, but it wouldn't work.
Did i do something wrong?


----------



## Squashman (Apr 4, 2003)

You need to use the *SET* statements as I provided them to you and then do the rename with the new variable.


----------



## lool123 (Jan 6, 2012)

So the code you provided could be used as-is even after the file has been made and named (Because the output command is the last command in the script before it terminates, and im planning on adding this command after that again)?


----------



## lool123 (Jan 6, 2012)

It didn't work when i used the method above where i tried to integrate it into the already existing script using:

```
goto :rename

:rename
set NewFN=%fn:Image=%
set NewFN=%NewFN:Beach=-%
rename "%fn%" "Summer %NewFN%.jpg"
```
However when i tried to make it two separate scripts, it worked. I just put the renaming script you gave me in a .bat in the Finished Images folder, and used it afterwards on the file, and it worked.
But it would be nice to have it fully automated ... 
The directory the not-renamed images and the first .bat which i originally had is in C:/Images
The destination of the renamed files is C:/Images/Finished Images


----------



## Squashman (Apr 4, 2003)

Since you refuse to show me the entire script it is rather hard for me to see how to integrate it into a single script.


----------



## lool123 (Jan 6, 2012)

Ah, yes, im sorry about that, i just don't feel comfy showing random people other people's things without their permissions, which i can't get since he's he's no longer here, so ...

At the end of the script it's run through a program, and then it proceeds to goto :clean which deletes the source image, and then the script terminates.
I tried adding the goto :rename after the cleaning had finished, but didn't work.


----------



## lool123 (Jan 6, 2012)

Oh, nevermind, it was my fault, i had used the wrong kind of slash in the path to the file in the Finished Images folder. It's working. Thank you very much.

Last thing im wondering about ... Is there a way to remove everything that's behind a certain point in filenames? What that everything is can vary very much, from being a few letters and numbers to being alot of letters and numbers.

For instance, i'd like to integrate into the above script a function to get rid of everything except .jpg behind for instance 
"Image Las Palmas Beach JPG_742 hfnsog-3728jfbs.jpg" Become "Summer Las Palmas - JPG_742.jpg"
"Image Las Palmas Beach JPG_63954 h425og-3gwdjfbs.jpg" Become "Summer Las Palmas - JPG_63954.jpg"


----------



## Squashman (Apr 4, 2003)

Well I don't see what could be so damning about some batch code.


----------



## lool123 (Jan 6, 2012)

I just don't feel comfy doing that, sorry.

Anyway, thank you very much for having gone along with me and helped me with this. Highly appreciated.


----------



## Squashman (Apr 4, 2003)

Well your logic makes no sense to me. Code is code and there is nothing secret about batch code because it is all builtin to the OS.


----------



## Squashman (Apr 4, 2003)

lool123 said:


> Ah, yes, im sorry about that, i just don't feel comfy showing random people other people's things without their permissions, which i can't get since he's he's no longer here, so ...


And if the person is no longer with your company what does it matter.


----------



## lool123 (Jan 6, 2012)

There's no company or anything, just me as a private person.
And it was a different kind of no longer here i meant ...


----------



## Squashman (Apr 4, 2003)

Still doesn't make any sense. Batch code is batch code. There is nothing special or private about it. Coming here and asking for FREE help on how to write a batch file and then not sharing any existing code you have is pretty hypocritical.


----------

