1. Computer problem? Tech Support Guy is completely free -- paid for by advertisers and donations. Click here to join today! If you're new to Tech Support Guy, we highly recommend that you visit our Guide for New Members.

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

Discussion in 'DOS/Other' started by austincwebb, Jul 9, 2012.

Thread Status:
Not open for further replies.
Advertisement
  1. austincwebb

    austincwebb Thread Starter

    Joined:
    Jul 9, 2012
    Messages:
    8
    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.
     
  2. Squashman

    Squashman Trusted Advisor

    Joined:
    Apr 4, 2003
    Messages:
    19,731
    Any Chance you might have something weird like this.
    E:\folder1\abc123.co\Folder2\abc123.cojt
     
  3. austincwebb

    austincwebb Thread Starter

    Joined:
    Jul 9, 2012
    Messages:
    8
    No. They may be in different folders but never inside of each other.
     
  4. Squashman

    Squashman Trusted Advisor

    Joined:
    Apr 4, 2003
    Messages:
    19,731
    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?
     
  5. austincwebb

    austincwebb Thread Starter

    Joined:
    Jul 9, 2012
    Messages:
    8
    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.
     
  6. Squashman

    Squashman Trusted Advisor

    Joined:
    Apr 4, 2003
    Messages:
    19,731
    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.
     
  7. austincwebb

    austincwebb Thread Starter

    Joined:
    Jul 9, 2012
    Messages:
    8
    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.
     
  8. Squashman

    Squashman Trusted Advisor

    Joined:
    Apr 4, 2003
    Messages:
    19,731
    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>
     
  9. austincwebb

    austincwebb Thread Starter

    Joined:
    Jul 9, 2012
    Messages:
    8
    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.
     
  10. Squashman

    Squashman Trusted Advisor

    Joined:
    Apr 4, 2003
    Messages:
    19,731
    Well that changes the code a lot.
     
  11. Squashman

    Squashman Trusted Advisor

    Joined:
    Apr 4, 2003
    Messages:
    19,731
    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.
     
  12. austincwebb

    austincwebb Thread Starter

    Joined:
    Jul 9, 2012
    Messages:
    8
    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
     
  13. Squashman

    Squashman Trusted Advisor

    Joined:
    Apr 4, 2003
    Messages:
    19,731
    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.......
     
  14. austincwebb

    austincwebb Thread Starter

    Joined:
    Jul 9, 2012
    Messages:
    8
    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
     
  15. Squashman

    Squashman Trusted Advisor

    Joined:
    Apr 4, 2003
    Messages:
    19,731
    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.
     
  16. Sponsor

As Seen On
As Seen On...

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.

Join over 733,556 other people just like you!

Loading...
Thread Status:
Not open for further replies.

Short URL to this thread: https://techguy.org/1060279