# Solved: renaming file in folder



## jhl (Aug 13, 2008)

Hi, I need some help with a batch file that is to be used for deleting a files that have the same file name in a folder. Eg. The file to be deleted(original file in folder) will have an "s" as the first char of the file name. Eg s1234567.jpg. Another new file will be put in the same folder having a "t" as the first character eg t1234567.jpg. The last 7 character of the file name are exactly the same. The contents of both these files are exactly the same . I would like to know how to write a batch file to compare the two files and when the new file with the "t" as the first character comes into the folder the older file with the "s" as the first character will be deleted. Any help with this will be appreciated

thanks


----------



## Squashman (Apr 4, 2003)

You should be able to do this with 2 nested for Loops. The first For Loop would list all the files that begin with a T and the second for loop would list all the files with an S. You would then use the command string modifiers to strip off the first character of the file name and compare the two strings. If they equal each other then delete the file.


----------



## Squashman (Apr 4, 2003)

```
@echo off
setlocal enabledelayedexpansion
FOR /F "tokens=*" %%T IN ('dir /b /a-d t*') DO (
     set _Tfile=%%~nxT               .
     set _TFilename=!_Tfile:~1!
     FOR /F "tokens=*" %%S IN ('dir /b /a-d s*') DO (
          set _Sfile=%%~nxS               .
          set _SFilename=!_Sfile:~1!
          if !_TFilename! == !_Sfilename! del %%S 
          )   
     )
endlocal
```
I wrote this up real quickly and I know I added too much code now that I am looking at it, but I am getting the look from my wife because we are suppose to be leaving for vacation. Hopefully Outcaste or Devil_himself will come along and correct my code.


----------



## jhl (Aug 13, 2008)

Squashman, thanks for your reply, 
Is it possible to do the same thing with folders instead of files. Eg. say I have a folder named "receive" and there is a file in that folder named s1234567 and another comes into the folder named t1234567. How would I compare the two folders last 7 characters and if they are the same, then delete the first folder?

thanks again


----------



## TheOutcaste (Aug 8, 2007)

I simplified Squashman's code by removing one of the For loops. Also, the ~nx modifier is not really needed as the dir command does not list the drive letter and path unless the /S switch is used. I also added the pushd and popd commands so you don't have to have the batch file in the same folder as the files you are checking, just specify the folder to check on the Set _Fpath line

```
@echo off
setlocal enabledelayedexpansion
[COLOR=Blue] Set _Fpath=[/COLOR]c:\Pictures
Pushd %_Fpath%
FOR /F "tokens=*" %%T IN ('dir /b /a-d t*') DO (
    set _Tfile=%%T
    If EXIST %%T If EXIST s!_Tfile:~1! Del s!_Tfile:~1!
     )
Popd
endlocal
```
To modify it to check folder names you just need to change the dir command to list folders instead of files, and use RD instead of Del to delete the folder:

```
@echo off
setlocal enabledelayedexpansion
Set _Fpath=c:\receive
Pushd %_Fpath%
FOR /F "tokens=*" %%T IN ('dir /b [COLOR=Red]/ad[/COLOR] t*') DO (
    set _Tfile=%%T
    If EXIST %%T If EXIST s!_Tfile:~1! [COLOR=Red]RD /q /s[/COLOR] s!_Tfile:~1!
     )
Popd
endlocal
```
Note that this will only compare the folder NAMES, not the names of the files in the s* and t* folders, so even if the T* folders has different files than the matching S* folder, the S* folder will still be deleted.
For example, if you have the following folder structure, both the S1 and S2 folder will be deleted, even though T2 contains different files than S2

```
c:\Receive
    c:\Receive\S1
        Tom.jpg
        Dick.jpg
        Harry.jpg
    c:\Receive\S2
        Tom1.jpg
        Dick1.jpg
        Harry1.jpg
    c:\Receive\T1
        Tom.jpg
        Dick.jpg
        Harry.jpg
    c:\Receive\T2
        Mary.jpg
        Sue.jpg
        Jane.jpg
```
HTH

Jerry


----------



## jhl (Aug 13, 2008)

Outcaste, thanks for your help- I tried running the folder batch file you created but the folder that I wanted deleted still remains. I created a folder called receive in the c: root and placed 2 folders inside one was s801266-1 and the other was t801266-1. Both of these folders contain two other folders inside with jpeg images. Unfortunately the folder that I want to delete "s801266-1" still remains in the receive folder when the batch file is run. I suspect I am doing something wrong but am not sure what. any ideas?
thanks,
jhl


----------



## TheOutcaste (Aug 8, 2007)

Opps, my bad, forgot that del won't remove the actual folder, just the contents. Need to use RD instead, so use this:


```
@echo off
setlocal enabledelayedexpansion
Set _Fpath=c:\receive
Pushd %_Fpath%
FOR /F "tokens=*" %%T IN ('dir /b /ad t*') DO (
    set _Tfile=%%T
    If EXIST %%T If EXIST s!_Tfile:~1! [COLOR=Red]RD /q /s[/COLOR] s!_Tfile:~1!
     )
Popd
endlocal
```


----------



## jhl (Aug 13, 2008)

that works, thanks a lot for your help, Now i'm trying to figure out what parts of the code I would need to change if the first folder begins with an "e" eg e123456-1 and the new folder begins with an "o" eg o123456-1. as you can see I'm new to this

thanks


----------



## TheOutcaste (Aug 8, 2007)

Just need to change 3 letters:
First letter of new file
First letter of file to delete

```
@echo off
setlocal enabledelayedexpansion
Set _Fpath=c:\receive
Pushd %_Fpath%
FOR /F "tokens=*" %%T IN ('dir /b /ad [COLOR=Red]t[/COLOR]*') DO (
      set _Tfile=%%T
      If EXIST %%T If EXIST [COLOR=Lime]s[/COLOR]!_Tfile:~1! RD /q /s [COLOR=Lime]s[/COLOR]!_Tfile:~1!
      )
Popd
Endlocal
```
You can replace those letters with variables defined at the start, then only need to change the variables:
Note: I also changed _Tfile to _TFolder for consistancy since we are talking folders.

```
@Echo Off
Setlocal enabledelayedexpansion
Set _Fpath=c:\receive
Set [COLOR=DarkRed]_NewFolderStart[/COLOR]=[COLOR=Red]t[/COLOR]
Set [COLOR=DarkOrange]_OldFolderStart[/COLOR]=[COLOR=Lime]s[/COLOR]
Pushd %_Fpath%
FOR /F "tokens=*" %%T IN ('dir /b /ad [COLOR=DarkRed]%_NewFolderStart%[/COLOR]*') DO (
      Set _TFolder=%%T
      If EXIST %%T If EXIST [COLOR=DarkOrange]%_OldFolderStart%[/COLOR]!_TFolder:~1! RD /q /s [COLOR=DarkOrange]%_OldFolderStart%[/COLOR]!_TFolder:~1!
      )
Popd
Endlocal
```
You can even have the batch file prompt you for the letters:

```
@Echo Off
Setlocal enabledelayedexpansion
Set _Fpath=c:\receive
Set [COLOR=DarkRed]/p _NewFolderStar[/COLOR]t=[COLOR=DarkRed]Enter first letter of new folder name: [/COLOR]
Set [COLOR=DarkOrange]/p _OldFolderStart[/COLOR]=[COLOR=DarkOrange]Enter first letter of old folder name: [/COLOR]
Pushd %_Fpath%
FOR /F "tokens=*" %%T IN ('dir /b /ad [COLOR=DarkRed]%_NewFolderStart%[/COLOR]*') DO (
      Set _TFolder=%%T
      If EXIST %%T If EXIST [COLOR=DarkOrange]%_OldFolderStart%[/COLOR]!_TFolder:~1! RD /q /s [COLOR=DarkOrange]%_OldFolderStart%[/COLOR]!_TFolder:~1!
      )
Popd
Endlocal
```
HTH

Jerry


----------



## jhl (Aug 13, 2008)

WOW-Thanks Jerry, that worked great-one more question - how could I have that batch file running at all times so that it can monitor the receive folder to delete the old folder whenever the new one arrives in the receive folder.

thanks again
JHL


----------



## TheOutcaste (Aug 8, 2007)

Batch files aren't really meant to "run in the background". Easiest way is to just set a scheduled task to run every few minutes to check for the new file.

First, create a shortcut to the batch file. Make sure the shortcut has a different name than the batch file -- I usually add *Job* to the name. If it's the same as the batch file, Scheduled Tasks will link to the batch file instead of the shortcut unless you manually enter the shortcut name, so changing the name makes it easier. Using a shortcut lets us run the task Minimized instead of in a window.
Right click the shortcut and on the *Shortcut* tab set the *Run*: drop down box to *Minimized*.
Goto *Control Panel | Scheduled Tasks | Add Scheduled Task*
*Browse* to the batch file
Choose *Perform Daily*
Pick a start time -- to run every 5 minutes pick a time ending in 5, etc
choose *Every Day* and todays date
enter a user name and password for the file to run under -- I always run scheduled tasks using an Admin account, so I don't know if there are issues running batch files under a limited account
Before clicking *Finish*, check the *Open Advanced Properties* box
When the *Properties* box opens, click on the *Schedule* tab, then click the *Advanced* button
Check *Repeat task*, set *Every:* boxes to 5 minutes (or whatever you choose)
Check *Duration:* and set it to 24 hours, then click OK until the window closes.

You will get a command prompt window that will appear on the taskbar every 5 minutes while the file runs. (without the shortcut, the full window will appear)
There are apps that will hide a DOS window completely, but I've not played with them. Should be easy to find with a google search.

HTH

Jerry


----------



## jhl (Aug 13, 2008)

Jerry, just want to thank you for all your help, it was great!

all the best
jhl


----------



## Squashman (Apr 4, 2003)

Good Job Outcaste. That simplifies it much more. I guess I didn't think of hard coding the S or T.

Thanks for cleaning that up. I whipped that out real quick before I left for vacation.


----------

