views:

71

answers:

2

I have many folders in a directory that contain various files. Each filename begins with XXX_ where XXX could be the name of the folder the file is in. What I am needing to do is to go through all those folders and delete any file where XXX is the name of the folder that file is in.

A: 

Please have an eye out this question: Iterating through folders and files in batch file?.

I think this should help you.

Please let me know if you need further assistance.

EDIT #1

The joker character in DOS command line is *. Then, while searching a directory for certain files, you may consider your regular expression, that is, your XXX_, and complete it with *, this shall return only the files for which you're looking for.

This means that instead of *.zip pattern in one of the FOR loops given the linked question, your first FOR loop should contain your directory name, then take this variable concatenated with the * character to obtain only the files you're looking for.

For example, consider trying the following:

dir /s XXX_*.*

This should return only the files you're interested in, given the right folder name.

EDIT #2

Thanks for having precised your concern.

Here is a code sample that, I do hope so, should help. Now I know you say you have the looping correct, so that perhaps only piece of this code might be needed.

@echo off

setlocal enableextensions enabledelayedexpansion

for /F "delims==" %%d in ('dir /ogne /ad /b /s .') do (
    for /F "delims==" %%f in ('dir /b "%%d\%%~nd_*.*"') do (
        echo %%d\%%f
    )
)

endlocal

This works and lists the files contained in subfolders from the current (.) folder.

I have tested it from the following folder:

C:\Docume~1\marw1\MyDocu~1\MyMusi~1

Where a 'XXX' folder is contained. This 'XXX' folder contains the following files:

  1. Copy of XXX_blah.bmp;
  2. XXX_blah.bmp;
  3. XXX_1234.ppt;
  4. XXX_textfile.txt.

From this structure, the output is:

C:\Docume~1\marw1\MyDocu~1\MyMusi~1\XXX\XXX_blah.bmp C:\Docume~1\marw1\MyDocu~1\MyMusi~1\XXX\XXX_1234.ppt C:\Docume~1\marw1\MyDocu~1\MyMusi~1\XXX\XXX_textfile.txt

I then suspect that putting a del instruction instead of an echo command shall do the trick. This means that to isolate the foldername itself from its path, you need to use the ~n instruction with your folder variable name like %%~nd, where your iterating folder variable name is %%d.

Furthermore, you could even use a parameterized batch file in the process, instead of hardcoding it, that is, if your 'set YourFolder =...' is part of your production code. This could look like:

@echo off

setlocal...

set root = %1
set root = %root:~1%
set root = %root:~0,-1%

...

endlocal

Instead of having '.' as pictured in my first FOR loop, your would replace it with "%root%" in order to consider your command line parameter instead of a hardcoded filepath.

I do help this helps, sincerely!

Will Marcouiller
Not really what I want to do. I really just need a script to delete the file if it contains the folder name it is in. I can script the rest to do it to the whole directory myself.
How do I match XXX to the folder name I have the whole loop written I just do not know how to write if or where xxx folder == xxx from begining of filename?
Here is what I have but this doesn't work
@echo offset YourFolder=D:\MAPPS\file_processing\computersfor /f "tokens=*" %%A in ('dir /a:-d /b /s "%YourFolder%\*.server_outgoing"') do (pushd %%~dpAfor %%b in (.) if not "%%A"=="%%~nb" do del *.server_outgoingpopd)
AWESOME, this was a tail end of a huge project I have been working on and just needed to move these files to finish processing them. You rock dude and made my day with the help! Here is the final code I used:
@echo offsetlocal enableextensions enabledelayedexpansionfor /F "delims==" %%d in ('dir /ad /b /s "%YourFolder%"') do ( for /F "delims==" %%f in ('dir /b "%%d\%%~nd*.*"') do ( del %%d\%%f ))endlocal
I'm glad I could help! I can feel your gratitude with this "AWESOME" comment. Thanks for having accepted my answer. You may even upvote! =)
Will Marcouiller
A: 

As Ron says, since the question is tagged "windows".

EDIT:

Ron's answer, which seems to have disappeared!, was to use del /s

EDIT2:

OK, it's valid only for file names, not for directories. For the directories you'd have to use something like sketched below.

Additional info: when you want to do the same thing recursively to files in a directory tree, and (unlike del) there's no command that already does the traversing for you, you can use the /R option of the for command.

To see the for command's docs, do e.g. start "for-help" cmd /k for /?.

Cheers & hth.,

– Alf

Alf P. Steinbach
I didn't see Ron's answer so I'm not really understanding yours.