Advertisement

There's no such thing as a stupid question, but they're the easiest to answer.
Login
Search

Advertisement

DOS/Other DOS/Other
Search Search
Search for:
Tech Support Guy > > >

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


(!)

austincwebb's Avatar
austincwebb austincwebb is offline
Member with 8 posts.
THREAD STARTER
 
Join Date: Jul 2012
Experience: Advanced
09-Jul-2012, 12:05 PM #1
Need to write a batch file that compares folder names and deletes one.
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's Avatar
Trusted Advisor with 19,633 posts.
 
Join Date: Apr 2003
Location: 1265 Lombardi Ave
12-Jul-2012, 01:08 PM #2
Any Chance you might have something weird like this.
E:\folder1\abc123.co\Folder2\abc123.cojt
austincwebb's Avatar
austincwebb austincwebb is offline
Member with 8 posts.
THREAD STARTER
 
Join Date: Jul 2012
Experience: Advanced
12-Jul-2012, 01:14 PM #3
No. They may be in different folders but never inside of each other.
Squashman's Avatar
Trusted Advisor with 19,633 posts.
 
Join Date: Apr 2003
Location: 1265 Lombardi Ave
12-Jul-2012, 01:17 PM #4
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's Avatar
austincwebb austincwebb is offline
Member with 8 posts.
THREAD STARTER
 
Join Date: Jul 2012
Experience: Advanced
12-Jul-2012, 01:20 PM #5
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's Avatar
Trusted Advisor with 19,633 posts.
 
Join Date: Apr 2003
Location: 1265 Lombardi Ave
12-Jul-2012, 01:25 PM #6
Quote:
Originally Posted by austincwebb View Post
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's Avatar
austincwebb austincwebb is offline
Member with 8 posts.
THREAD STARTER
 
Join Date: Jul 2012
Experience: Advanced
12-Jul-2012, 01:33 PM #7
Quote:
Originally Posted by Squashman View Post
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's Avatar
Trusted Advisor with 19,633 posts.
 
Join Date: Apr 2003
Location: 1265 Lombardi Ave
12-Jul-2012, 03:20 PM #8
Remove_Dir.bat
Code:
@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.
Code:
C:\batch files\Remove_Like_Dir>tree
Folder PATH listing for volume 7_10P
Volume serial number is 00690044 549E:2BEB
C:.
├───folder1
│   └───abc123.co
├───folder2
│   └───abc123.cojt
├───squash.co
└───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:.
├───folder1
├───folder2
│   └───abc123.cojt
└───squash.cojt

C:\batch files\Remove_Like_Dir>
austincwebb's Avatar
austincwebb austincwebb is offline
Member with 8 posts.
THREAD STARTER
 
Join Date: Jul 2012
Experience: Advanced
13-Jul-2012, 12:24 PM #9
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's Avatar
Trusted Advisor with 19,633 posts.
 
Join Date: Apr 2003
Location: 1265 Lombardi Ave
13-Jul-2012, 12:33 PM #10
Well that changes the code a lot.
Squashman's Avatar
Trusted Advisor with 19,633 posts.
 
Join Date: Apr 2003
Location: 1265 Lombardi Ave
13-Jul-2012, 12:53 PM #11
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's Avatar
austincwebb austincwebb is offline
Member with 8 posts.
THREAD STARTER
 
Join Date: Jul 2012
Experience: Advanced
13-Jul-2012, 01:59 PM #12
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's Avatar
Trusted Advisor with 19,633 posts.
 
Join Date: Apr 2003
Location: 1265 Lombardi Ave
13-Jul-2012, 02:08 PM #13
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's Avatar
austincwebb austincwebb is offline
Member with 8 posts.
THREAD STARTER
 
Join Date: Jul 2012
Experience: Advanced
13-Jul-2012, 03:38 PM #14
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's Avatar
Trusted Advisor with 19,633 posts.
 
Join Date: Apr 2003
Location: 1265 Lombardi Ave
13-Jul-2012, 04:32 PM #15
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.
As Seen On

BBC, Reader's Digest, PC Magazine, Today Show, Money Magazine
WELCOME TO TECH SUPPORT GUY!

Are you looking for the solution to your computer problem? Join our site today to ask your question. This site is completely free -- paid for by advertisers and donations.

If you're not already familiar with forums, watch our Welcome Guide to get started.


(clock)
THIS THREAD HAS EXPIRED.
Are you having the same problem? We have volunteers ready to answer your question, but first you'll have to join for free. Need help getting started? Check out our Welcome Guide.

Search Tech Support Guy

Find the solution to your
computer problem!




Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 


WELCOME
You Are Using: Server ID
Trusted Website Back to the Top ↑