Help with IF NOT Problem

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
cwsterling
Posts: 2
Joined: 16 Apr 2008 14:01

Help with IF NOT Problem

#1 Post by cwsterling » 16 Apr 2008 14:08

i am working on some code that works fine when I am not testing with IF NOT, but when I have the IF NOT statements in, the code only loops once, then stops working.

the file gameList.txt looks something like

Code: Select all

name1.exe
name2.exe
name3.dll


and then the script I am using looks like this

Code: Select all

@echo off
setlocal enabledelayedexpansion
c:
cls
echo *********************************************
echo **Delete Given File Names that can Be found**
echo *********************************************
 
echo Studying the file for banned file names.
 
for /f %%A in (H:\gameLocations\gameList.txt) do (
  set filename=%%A
 
 
  echo Searching for !filename!
  title Searching...
 
  for /f "delims==" %%D in ('dir /s /b "!filename!"') do (
    set direc=%%~dpD

   
    IF "!direc!"=="C:\Windows\*" Goto skip
   
    :check
      IF "!direc!"=="C:\Program Files\" Goto skip2
     
    :continueScript
      echo !filename! found in folder !direc!
      echo removing files/folder
      echo rmdir /s /q "!direc!" && echo OK
      Goto endScript
         
    :skip
      echo Found in the Windows Directory (Not Deleting...)
      Goto endScript
 
    :skip2
      echo Found in the Program Files Directory (Not Deleting...)
     
    :endScript
  )
 
  echo Searching Hidden Files and Folders for !filename!
 
  for /f "delims==" %%D in ('dir /s /b /a:H "!filename!"') do (
    set direc=%%~dpD
   
    IF "!direc!"=="C:\Windows\*" Goto skip
   
    :check
      IF "!direc!"=="C:\Program Files\" Goto skip2
     
    :continueScript
      echo !filename! found in folder !direc!
      echo removing files/folder
      echo rmdir /s /q "!direc!" && echo OK
      Goto endScript
         
    :skip
      echo Found in the Windows Directory (Not Deleting...)
      Goto endScript
 
    :skip2
      echo Found in the Program Files Directory (Not Deleting...)
     
    :endScript
  )
)
pause


Now I know it works as like I said, when I find the files it says what directory it is going to delete. The only problem is when I am testing it, it only loops through once and then stops working as if it didn't see the other two lines.

jeb
Expert
Posts: 1042
Joined: 30 Aug 2007 08:05
Location: Germany, Bochum

#2 Post by jeb » 18 Apr 2008 07:03

Hi cwsterling,

nice error you found.

Your porblem is, you want to use goto and jump-marks inside a for loop.

That's not allowed.

Code: Select all

for /f "delims==" %%D in ('dir /s /b "!filename!"') do ( 
:xy
)

results in an error

You build up a double error, therefore you got no error message.
Your second error are the () in the echo statements

Simple extract your code into subroutines and call them from the loop like

Code: Select all

for /f "delims==" %%D in ('dir /s /b "!filename!"') do ( 
    call :CheckSomething %%D myResultVar
)


jeb

cwsterling
Posts: 2
Joined: 16 Apr 2008 14:01

#3 Post by cwsterling » 18 Apr 2008 14:00

So you are saying something like

Code: Select all

@echo off
setlocal enabledelayedexpansion
c:
cls
echo *********************************************
echo **Delete Given File Names that can Be found**
echo *********************************************
 
echo Studying the file for banned file names.
 
for /f %%A in (H:\gameLocations\gameList.txt) do (
  set filename=%%A
 
 
  echo Searching for !filename!
  title Searching...
 
  for /f "delims==" %%D in ('dir /s /b "!filename!"') do (
    set direc=%%~dpD
   
    IF "!direc!"=="C:\Windows\*" Call skip
    IF "!direc!"=="C:\Program Files\" Call skip2
     
   echo !filename! found in folder !direc!
    echo removing files/folder
    echo rmdir /s /q "!direc!" && echo OK
  )
 
  echo Searching Hidden Files and Folders for !filename!
 
  for /f "delims==" %%D in ('dir /s /b /a:H "!filename!"') do (
    set direc=%%~dpD
   
    IF "!direc!"=="C:\Windows\*" Call skip
    IF "!direc!"=="C:\Program Files\" Call skip2
     
   echo !filename! found in folder !direc!
    echo removing files/folder
    echo rmdir /s /q "!direc!" && echo OK
  )
)
pause
         
    :skip
      echo Found in the Windows Directory Not Deleting...
      )
 
    :skip2
      echo Found in the Program Files Directory Not Deleting...
     )

jeb
Expert
Posts: 1042
Joined: 30 Aug 2007 08:05
Location: Germany, Bochum

#4 Post by jeb » 20 Apr 2008 14:36

Hi,

yes it should work, I tested it on my system with some testfiles.

But you should add after the "pause" a
"goto :eof"

else the ":skip" and the ":skip2" part are always run after the pause

jeb

Post Reply