0

I've created this very simple batch file for the sake of testing a concept I'm hoping to utilize. I need to recursively delete all of one type of file except in folders with a specific name. Here's my code:

:recur
FOR /f %%a IN ('DIR /b') DO (
IF EXIST %%a\NUL (
    IF ["%%a" NEQ "subtest2"] (
        ECHO %%a
        CD %%a
        CALL :recur
        CD ..
    )
)
COPY "*.cfm" "*_copy.cfm"
REM DEL "*_copy*.cfm"
)

Right now I'm just testing using copy instead of delete. Basically, this should create a copy of all the .cfm files except in the folder "subtest2". Right now it's recursively making the copies everywhere, including subtest2. How do I stop this?

The structure of my base directory is:

TestFolder
---subtest1
------test.pdf
------test.txt
------test.cfm
---subtest2
------test.pdf
------test.txt
------test.cfm
---test.pdf
---test.txt
---test.cfm
---recur.bat

Dropout
  • 13,653
  • 10
  • 56
  • 109
Misty
  • 73
  • 4

3 Answers3

2

The square brackets are not balanced on both sides of the IF comparison, so it can never match. The brackets are not part of the IF syntax. If present, they simply become part of the string that is compared. The same is true for any enclosing quotes. Remove the square brackets, and it will work (assuming there are no other problems)

Here is a simple method to accomplish your goal. I've prefixed the DEL command with ECHO for testing purposes:

for /r /d %%F in (*) do echo %%F\|findstr /liv "\\subtest2\\" >nul && echo del "%%F\*.cfm"

The FOR /R /D simply recurses all folders. The full path of each folder is piped into a FINDSTR command that looks for paths that do not contain a \subtest2 folder. The ECHO DEL command is only executed if the \subtest2\ folder is not found in the path.

Remove the last ECHO when you have confirmed the command gives the correct results.

Change %%F to %F if you want to run the command on the command line instead of in a batch file.

dbenham
  • 127,446
  • 28
  • 251
  • 390
  • I ended up just taking the brackets out of my if statement. That way I got to keep the code I wrote myself. Now I'm working on improving it and using the more complex selections. I'm starting to understand it a lot better. This is my first batch file so I was still getting familiar with the syntax. Thanks! – Misty May 29 '13 at 21:09
1
for f in `find . -path YOURDIR -prune -o print`
  do
    rm whateveryouwanttodelete
  done

the find command in backticks finds all files but ignores the directory -prune you want to ignore. Then in the body of the loop you nuke the files. You can do even better with

find . -path YOURDIR -prune -o -print0 | xargs -0 rm -f

no need for the loop. DISCLAIMER: I haven't tested it so perhaps you want to start adopting it with cp instead of rm.

user1666959
  • 1,805
  • 12
  • 11
1

You can try this:

@echo off&setlocal
for /r /d %%i in (*) do (
    pushd "%%i"
    echo(%%i|findstr /ri "\\subtest2$" || COPY "*.cfm" "*_copy.cfm"
    popd
)
Endoro
  • 37,015
  • 8
  • 50
  • 63
  • Could you explain your answer a little more. I think I might be able to use it better if I understood it more. – Misty May 29 '13 at 19:56
  • @Mysti - This command only excludes files that are directly in a "subtest2" folder. I believe you also want to exclude files that are in subfolders under "subtest2". – dbenham May 29 '13 at 20:06
  • 1
    The backslash in a regex must be escaped as \\\\. See http://stackoverflow.com/q/8844868/1012053 for more info. – dbenham May 29 '13 at 20:07
  • @dbenham -- please look at `echo(\subtest2|findstr /ri "\\\\subtest2$"` : it doesn't work. This works: `echo(\subtest2|findstr /ri "\\\subtest2$"`, escaping the "s" and this does also work: `echo(\subtest2|findstr /ri "\\subtest2$"`. This also doesn't work: `echo(\tsubtest2|findstr /ri "\\subtest2$"`, & `echo(\ubtest2|findstr /ri "\\subtest2$"`. The answer in your link might be **not** correct. I'm on XP, could you please test this? – Endoro May 29 '13 at 20:48
  • 1
    Thanks Endoro. I've updated my [Undocumented FINDSTR features](http://stackoverflow.com/q/8844868/1012053) information with escape sequences for XP and Win7. I'm trusting that my Vista info is still correct. I did a *lot* of testing on multiple Vista machines, but I no longer have access to Vista. – dbenham May 30 '13 at 11:33