Reputation: 87
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.
Upvotes: 0
Views: 2793
Reputation: 24142
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:
- Copy of XXX_blah.bmp;
- XXX_blah.bmp;
- XXX_1234.ppt;
- 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!
Upvotes: 1
Reputation: 145439
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
Upvotes: 0