# Solved: Batch File Renaming Files



## scrfix (May 3, 2009)

I am not sure what I am screwing up here. This should be a stupidly simple process.

*Objective:* Walk a directory or change directories based upon a string and rename files based upon what is in them.

*Scenario:* I have a bunch of image files without extenstions in multiple directories. The batch file will start in the logs directory.
Examples:
logs>>aa>>zt>>(image files with no extensions)
logs>>bc>>zt>>(image files with no extensions)
logs>>df>>zt>>(image files with no extensions)
logs>>zb>>zt>>(image files with no extensions)

I would like to rename all of the files within the zt directory (They are always image files) to have a .jpg extension so they can be clicked on.

The code below is just set to echo back what directory it is in and according to FOR /? I have it written correctly with single quotes vs. double quotes. I have tried SETLOCAL EnableExtensions before the FOR statement to no avail. I am sure that it is something stupid. Any help please would be fantastic. It believes that I am trying to process a command file instead of reading a string.

*Current Broken Code*

```
@echo off
set _folders=aa,bc,df,zb
 
:: ---------- Do not edit anything below this line. ------------- ::
set _zt=\zt
set _rn=ren * *.jpg
 
For /F "tokens=* delims=," %%a In ('%_folders%') Do (
pushd %%a
Echo I am currently in %CD%
popd
)
 
:: set _zt=
:: set _rn=
:: set _folders=
pause
```
I am trying to get it to output to the screen:
I am currently in <directory path>\aa
I am currently in <directory path>\bc
I am currently in <directory path>\df
I am currently in <directory path>\zb

If I know that it is in that directory then I can simple put in ren * *.jpg and that will work for my application.


----------



## scrfix (May 3, 2009)

NeverMind, I figured it out


```
@echo off
set _folders=[B][COLOR=red]aa bc df zb[/COLOR][/B]
 
:: ---------- Do not edit anything below this line. ------------- ::
set _zt=\zt
set _rn=ren * *.jpg
[B][COLOR=red]SETLOCAL EnableDelayedExpansion[/COLOR][/B]
 
For /F "tokens=[B][COLOR=red]1-4[/COLOR][/B]" %%a In ([B][COLOR=red]"[/COLOR][/B]%_folders%[B][COLOR=red]"[/COLOR][/B]) Do (
pushd %%a
Echo I am currently in [B][COLOR=red]!CD![/COLOR][/B]
popd
pushd %%b
Echo I am currently in [B][COLOR=red]!CD![/COLOR][/B]
popd
pushd %%c
Echo I am currently in [B][COLOR=red]!CD![/COLOR][/B]
popd
pushd %%d
Echo I am currently in [B][COLOR=red]!CD![/COLOR][/B]
popd
)
 
:: set _zt=
:: set _rn=
:: set _folders=
pause
```
However there is a much easier way to do this. As I am looking in to this.

```
for /F "tokens=*" %%A IN ('dir /B /L /S ^|findstr /C:"zt"') DO echo %%A
```
This will step through and only find the zt directories and then you can rename the files from there instead of echo. I cannot quite get it to rename the files though. I have it set to show me the files without an issue. It simply will not execute the command ren * *.jpg.


----------



## scrfix (May 3, 2009)

Got this resolved in a different forum


```
@echo off 
set startfolder=%cd%
for /f "delims=" %%A in ('dir /s /b /ad *dt*') do (
  cd /d "%%A"
  ren * *.jpg
  )
cd /d "%startfolder%"
```
Even better than what I was thinking.


----------



## Squashman (Apr 4, 2003)

I knew you would figure it out eventually but I thought you would have been able to figure it out on your own. TheOutCaste and I have given you more than enough examples in the past to solve this issue.

I personally would do it slightly differently but whatever works for you.


----------



## scrfix (May 3, 2009)

I got it most of the way working on my own. I could not figure out why it wasn't moving. I do not typically ask about the other things that I have been able to figure out. This one I had not dealt with before so didn't know how to take care of it otherwise I would not have asked.

I am thankful to the other person that did help. As a matter of fact, they not only helped but made it better than what I would have.

How would you have completed it? I already have this one and it works. It was just for something simple so it works. Just curious as to how you would have handled it.


----------



## Ent (Apr 11, 2009)

If I were modifying this, I'd use the FOR command directly against the folder instead of working on the output of the DIR command.


----------

