# Need to write a batch file that compares folder names and deletes one.



## austincwebb (Jul 9, 2012)

I have hundreds of folder names xxxx.co and xxxx.cojt. These files come in as *.co and we use a program to convert some of them to *.cojt. I have all of my files located on a E:\ drive. These are FOLDERS and not files. It's almost like a folder has an extension, but not really. Just a naming convention. 

The Folders have a name before the .co and after conversion, they have the same name but now .cojt at the end of the folder name. I need to search my E:\ drive and find all FOLDERS that have the same name before the .co and .cojt. Some *.co will not have a .cojt as they are not all converted. The ones that do have a match, however, I need this batch file to delete the original xxxx.co and leave the xxxx.cojt. So basically comparing folder names before the period, and anything that matches with .co and .cojt, the .co folder gets deleted.

Thanks for the help. 

Folder example:

abc123.co
abc123.cojt
123abc.co
1234.cojt
microsoft.co
microsoft.cojt

So if i had these 6 folders, the ones to be deleted would be abc123.co and microsoft.co as they have an identical .cojt folder. 

One other twist in this is that some of these may be all over the E:\ drive in different subfolders. For example.

E:\folder1\abc123.co
E:\folder2\abc123.cojt

The first one, E:\folder1\abc123.co still needs to be deleted because they are the same just in different sub-directories.

Thanks for the help.


----------



## Squashman (Apr 4, 2003)

Any Chance you might have something weird like this.
E:\folder1\abc123.co\Folder2\abc123.cojt


----------



## austincwebb (Jul 9, 2012)

No. They may be in different folders but never inside of each other.


----------



## Squashman (Apr 4, 2003)

Have you posted this question on any other forums and have they come up with any type of solution or possible way to do it?


----------



## austincwebb (Jul 9, 2012)

No. You seemed very knowledgeable as I read over this site so I posted it here thinking you may be able to help me. Ive been doing batch files for many years, and typically have no issues. But this one stumped me. Files would be no problem but folders seem to be a different story. This issue is unresolved, but needs to be solved very soon. These files are in the 10s-100s of MB and so our server is filling up quickly. Once this is done and ran, I will be able to free up about 3-4 TB of space. Needs to be accomplished quick in other words. Thank you for your help by the way.


----------



## Squashman (Apr 4, 2003)

austincwebb said:


> These files are in the 10s-100s of MB and so our server is filling up quickly.


You mean the files inside these folders?
You seem to be confusing Files and Folders as I read your first post. A folder can be named anything you want. Just because it has a PERIOD in it doesn't make it a file. You could have a dozen periods in your Folder name if you wanted to. I myself try to avoid naming my folders with periods because it some times wreaks havoc with FOR Loops in batch files when you are just processing files it may match a Folder Name.

Show me what you have tried so far.


----------



## austincwebb (Jul 9, 2012)

Squashman said:


> You mean the files inside these folders?
> You seem to be confusing Files and Folders as I read your first post. A folder can be named anything you want. Just because it has a PERIOD in it doesn't make it a file. You could have a dozen periods in your Folder name if you wanted to. I myself try to avoid naming my folders with periods because it some times wreaks havoc with FOR Loops in batch files when you are just processing files it may match a Folder Name.
> 
> Show me what you have tried so far.


Well thats what I am saying. We do not care about the files inside of these folders... We are wanting to compare FOLDER names and delete based on match. Contents are ignored. And I haven't really written anything. I was taking written notes on paper and kept realizing things that would cause it not to work. So I've basically scrapped everything I have and decided to start over.

And Yes I do mean files inside these folders add up to that much size.


----------



## Squashman (Apr 4, 2003)

Remove_Dir.bat

```
@echo off
setlocal enabledelayedexpansion
FOR /F "DELIMS=" %%G IN ('DIR /AD /B /S *.cojt') DO (
	SET CO=
	FOR /F "DELIMS=" %%H IN ('DIR /AD /B /S ^| findstr /E /I /C:"%%~nG.co" 2^>nul') DO SET CO=%%~H
	IF DEFINED CO RMDIR /Q /S "!CO!"
)
endlocal
```
Running the tree command before and after I execute the Batch file to show you that it removed the CO folders.

```
C:\batch files\Remove_Like_Dir>tree
Folder PATH listing for volume 7_10P
Volume serial number is 00690044 549E:2BEB
C:.
&#9500;&#9472;&#9472;&#9472;folder1
&#9474;   &#9492;&#9472;&#9472;&#9472;abc123.co
&#9500;&#9472;&#9472;&#9472;folder2
&#9474;   &#9492;&#9472;&#9472;&#9472;abc123.cojt
&#9500;&#9472;&#9472;&#9472;squash.co
&#9492;&#9472;&#9472;&#9472;squash.cojt

C:\batch files\Remove_Like_Dir>Remove_Dir.bat

C:\batch files\Remove_Like_Dir>tree
Folder PATH listing for volume 7_10P
Volume serial number is 00690044 549E:2BEB
C:.
&#9500;&#9472;&#9472;&#9472;folder1
&#9500;&#9472;&#9472;&#9472;folder2
&#9474;   &#9492;&#9472;&#9472;&#9472;abc123.cojt
&#9492;&#9472;&#9472;&#9472;squash.cojt

C:\batch files\Remove_Like_Dir>
```


----------



## austincwebb (Jul 9, 2012)

OK. Thank you for the help. This works perfectly! However, I have discovered an issue. MY FAULT on the wrong information to you. So obviously if we delete certain ones, our programs no longer work. So the issue is ***The have to be in the same folder***. And this is my fault because I told you wrong. So this batch file is good, it just needs to be changed for only deleting the ***.co when it finds a match, but only in each folder. So basically in the example you ran, abc123.co would not be deleted because it and the abc123.cojt were in different folders, however squash.co would be deleted as its match is right next to it. 

If you could help me fix this I would greatly appreciate it! Thanks for your help so much and I'm sorry this was bad information on my part.


----------



## Squashman (Apr 4, 2003)

Well that changes the code a lot.


----------



## Squashman (Apr 4, 2003)

This actually simplifies the code alot as well.
I will give you a hint. Remove the 3 lines of code inside the FOR loop and replace it with:
IF EXIST............RMDIR............ (fill in the blanks)

Look at the variable I used with the FINDSTR command in my original script to find the CO folders. You will use a variable like that with your IF EXIST except you will need to add two modifiers to it. Read the very end of the help file for the FOR command on how to do that.


----------



## austincwebb (Jul 9, 2012)

So this is what I tried. Not working. And I may have made a mistake earlier when I said I'm pretty good with batch files. Im good with simple batch files but I typically never get this complex...

@echo off
setlocal enabledelayedexpansion
FOR /F "DELIMS=" %%G IN ('DIR /AD /B /S *.cojt') DO (
SET CO=
FOR /F "DELIMS=" %%H IN (IF EXIST CO RMDIR /Q /S "!CO!") DO SET CO=%%~H
)
endlocal


----------



## Squashman (Apr 4, 2003)

Yeah. you don't have a clue!
I meant to say REMOVE the 3 lines inside the first for loop and replace it with an IF EXIST.......


----------



## austincwebb (Jul 9, 2012)

Yea... Im really not sure. I've moved a couple of things around and deleted the 3 lines but I'm not getting anything...

@echo off
setlocal enabledelayedexpansion
FOR /F "DELIMS=" %%G IN ('DIR /AD /B /S *.cojt') DO (
IF EXIST RMDIR /Q /S "!CO!"
)
endlocal


?? IM SO LOST ON THIS MAN. I'VE NEVER DONE A FOR LOOP BATCH FILE. And you may find that interesting, but for all that I do, I'm only 25 so dos and batch files have never been a HUGE part of my life. Thanks why I'm asking for help. haha. Thanks


----------



## Squashman (Apr 4, 2003)

I am kind of done for the week. 
This is really just one line of code to do what you want now that you changed the parameters. Once you see it you will probably kick yourself and say that was pretty simple.


----------



## austincwebb (Jul 9, 2012)

So do you think you can give me this one line of code before you hang up the week? I really need to have this completed so that I can move forward on some subsequent issues over the weekend, however this has to be completed first. Thank you so much.


----------



## Squashman (Apr 4, 2003)

I am off my computer now. Just have my phone.


----------



## Squashman (Apr 4, 2003)

austincwebb said:


> Yea... Im really not sure. I've moved a couple of things around and deleted the 3 lines but I'm not getting anything...
> 
> @echo off
> setlocal enabledelayedexpansion
> ...


In this code you are checking for the existence of basically nothing and then trying remove a directory from a variable that doesn't even exist.


----------



## Squashman (Apr 4, 2003)

```
@echo off
FOR /F "DELIMS=" %%G IN ('DIR /AD /B /S *.co') DO (
     IF EXIST "%%~Gjt" RMDIR /Q /S "%%~G"
)
```
And you could technically just have one line of code but for readability purposes I broke up the code.

```
FOR /F "DELIMS=" %%G IN ('DIR /AD /B /S *.co') DO IF EXIST "%%~Gjt" RMDIR /Q /S "%%~G"
```


----------

