1.

Solve : Runs, then Closes no idea why?

Answer»

I have the following code

Code: [Select]@echo off
cls
echo *********************************************
echo **Delete Given File Names that can Be found**
echo *********************************************

FOR /F "tokens=1-3 delims=:" %%a IN (gameList.txt) DO (
echo Setting Information Up

set filename=%%a
set searchString=%%b
set hiddenString=%%c

echo %searchString%
title %searchString%

for /f "delims==" %%D in ('dir /s /b %filename%') do (
echo %filename% found in folder %%~dpD
echo removing files/folder
rmdir /s /q "%%~dpD" && echo OK
)

echo %hiddenString%

for /f "delims==" %%D in ('dir /s /b /a:H %filename%') do (
echo %filename% found in folder %%~dpD
echo removing files and folders
rmdir /s /q "%%~dpD" && echo OK
)
)
pause
[/code

It worked for a moment doing what it was supposed to, then I made some CHANGES, don't remember what, and now it runs, then closes and deletes all the files in the directory no matter what. Any help would be great.

Chriswhat does gameList.txt look like?

I would advise CHANGING the "killer" lines, the ones with rmdir in them, by prefacing them with echo so that you can see what they would do before you let them loose e.g.

echo rmdir /s /q "%%~dpD" && echo OK

also, if you are in danger of making changes and then forgetting what they were, save multiple versions with (for example) numbered filenames such as test1.bat, test2.bat, etc. That way you can always go back.

also remove the @echo off line or comment it out (rem @echo off) that way you can watch the execution and see all variables being expanded.

The variables in the main loop

Code: [Select]set filename=%%a
set searchString=%%b
set hiddenString=%%c

that is, filename, searchstring, and hiddenstring, are not going to be what you apparently think they are, because you NEED to remember about delayed expansion in a loop. realized after I left for my activity that I forgot to post gameList.txt

it looks something like

xxx.exe:Searching for xxx: Searching hidden files for xxx

also, what do you mean by Quote

that is, filename, searchstring, and hiddenstring, are not going to be what you apparently think they are, because you need to remember about delayed expansion in a loop.
Quote from: demosthenes705 on March 31, 2008, 08:24:52 PM
also, what do you mean by Quote
that is, filename, searchstring, and hiddenstring, are not going to be what you apparently think they are, because you need to remember about delayed expansion in a loop.

http://www.google.co.uk/search?source=ig&hl=en&rlz=&q=nt+batch+delayed+expansion&btnG=Google+Search&meta=&aq=fthanks for that, that seems to work a little better. Now the PROBLEM I am having is that it runs three times even though there are only two lines in the gameList.txt file. Here is everything I am using

Code: [Select]rem echo off
SETLOCAL ENABLEDELAYEDEXPANSION

cls
echo *********************************************
echo **Delete Given File Names that can Be found**
echo *********************************************

FOR /F "tokens=1-3 delims=:" %%a IN (gameList.txt) DO (
SETLOCAL ENABLEDELAYEDEXPANSION
set filename=%%a
set searchString=%%b
set hiddenString=%%c
@ping 127.0.0.1 -n 2 -w 1000 > nul
@ping 127.0.0.1 -n %1% -w 1000> nul
echo %searchString%
title %searchString%

for /f "delims==" %%D in ('dir /s /b %filename%') do (
echo %filename% found in folder %%~dpD
echo removing files/folder
echo rmdir /s /q "%%~dpD" && echo OK
)

echo %hiddenString%

for /f "delims==" %%D in ('dir /s /b /a:H %filename%') do (
echo %filename% found in folder %%~dpD
echo removing files and folders
echo rmdir /s /q "%%~dpD" && echo OK
)
)
pause
gameList.txt
Code: [Select]halo.exe:Searching for Halo:Searching Hidden Files for Halo
Project64.rdb:Searching for Project64: Searching Hidden Files/Folders for Project64
You made a good start with this line

Code: [Select]SETLOCAL ENABLEDELAYEDEXPANSION
But you should have read a little further. In the loops, you need to replace the percent signs around the variable names with exclamation marks (points).

e.g.

Code: [Select]echo !searchString!
Maybe this will make it clear

demo.bat

Code: [Select]@echo off
setlocal enabledelayedexpansion

rem create test file

echo cat > animals.txt
echo dog >> animals.txt
echo mouse >> animals.txt

echo variables with percent signs

for /f %%A in (animals.txt) do (
set animal=%%A
echo animal is %animal%
)

echo variables with exclamation marks

for /f %%A in (animals.txt) do (
set animal=%%A
echo animal is !animal!
)

Code: [Select]D:\Test\delexdemo>demo
variables with percent signs
animal is
animal is
animal is
variables with exclamation marks
animal is cat
animal is dog
animal is mouse

When the batch file is parsed at run time, even with delayed expansion enabled, the variables with percent signs (such as %animal%) are expanded to their known values. As you can see, in the first loop their known value is... blank. Cmd.exe cannot read the file ahead of time and substitute the values.

However, any variables with exclamation marks are expanded each time around the loop, so in the second loop they get the value you want.

try this

it only goes round twice for me - I only see "main loop" twice

Code: [Select]@echo off
SETLOCAL ENABLEDELAYEDEXPANSION

cls
echo *********************************************
echo **Delete Given File Names that can Be found**
echo *********************************************

FOR /F "tokens=1,2,3 delims=:" %%a IN (gameList.txt) DO (
echo ----main loop
set filename=%%a
set searchString=%%b
set hiddenString=%%c
@ping 127.0.0.1 -n 2 -w 1000 > nul

rem not sure what this is meant to do
rem @ping 127.0.0.1 -n %1% -w 1000> nul
rem ^^^
rem ???

echo !searchString!
title !searchString!

for /f "delims==" %%D in ('dir /s /b !filename!') do (
echo !filename! found in folder %%~dpD
echo removing files/folder
echo rmdir /s /q "%%~dpD" && echo OK
echo.
)

echo !hiddenString!

for /f "delims==" %%D in ('dir /s /b /a:H !filename!') do (
echo !filename! found in folder %%~dpD
echo removing files and folders
echo rmdir /s /q "%%~dpD" && echo OK
)
)
pause

Code: [Select]*********************************************
**Delete Given File Names that can Be found**
*********************************************

----main loop
Searching for Halo
halo.exe found in folder D:\Test\delexdemo\sub1\
removing files/folder
rmdir /s /q "D:\Test\delexdemo\sub1\"
OK

halo.exe found in folder D:\Test\delexdemo\sub2\
removing files/folder
rmdir /s /q "D:\Test\delexdemo\sub2\"
OK

Searching Hidden Files for Halo
File Not Found

----main loop
Searching for Project64
Project64.rdb found in folder D:\Test\delexdemo\sub3\
removing files/folder
rmdir /s /q "D:\Test\delexdemo\sub3\"
OK

Searching Hidden Files/Folders for Project64
File Not Found
Press any key to continue . . .


thanks so much, it took a little bit more work and I decided not to delineate it because I just could not get it to work.

Code: [Select]@ping 127.0.0.1 -n 2 -w 1000 > nul
that was there to see if I could make it wait to see if I made it wait long enough, it would grab the variables.Quote from: demosthenes705 on April 01, 2008, 02:44:34 PM
Code: [Select]@ping 127.0.0.1 -n 2 -w 1000 > nul
that was there to see if I could make it wait to see if I made it wait long enough, it would grab the variables.

You could make it wait a month and it still wouldn't grab them...

I meant what was the purpose of %1% in this line... that sequence of characters would be expanded to... nothing, a blank.

Code: [Select]ping 127.0.0.1 -n %1% -w 1000> nul
anyway, no matter. I removed the echo from the rmdir lines and it removed the test folders I had created with "bait" files in them.





thanks so much, i found it online.... i thought it might work, but i guess I was wrong.

Now, I am trying to add a loop to this, so incase they stick it in like C:\windows or c:\program files it doesn't delete the entire folder. I think i got the loop correct but it doesn't do much so I am not sure.

Here is my code with the loop that doesn't function.
Code: [Select]@echo off
setlocal enabledelayedexpansion

cls
echo *********************************************
echo **Delete Given File Names that can Be found**
echo *********************************************

echo Studying the file for banned file names.

for /f %%A in (gameList.txt) do (
set filename=%%A


echo Searching for !filename!
title Searching...

for /f "delims==" %%D in ('dir /s /b "!filename!"') do (
echo !filename! found in folder %%~dpD
if "%%~dpD"=="C:\Windows" (Goto skip) ELSE (Goto continueScript

:skip
echo Found in the Windows Directory (Not Deleting...)
:skip2
echo Found in the Program Files Directory (Not deleting...)
:continueScript
if "%%~dpD"=="C:\Program Files" (Goto skip2) ELSE (Goto continueScript)
echo removing files/folder
echo rmdir /s /q "%%~dpD" && echo OK
)

echo Searching Hidden Files and Folders for !filename!

for /f "delims==" %%D in ('dir /s /b /a:H "!filename!"') do (
echo !filename! found in folder %%~dpD
echo removing files and folders
rmdir /s /q "%%~dpD" && echo OK
)
)
pause
Code: [Select]if "%%~dpD"=="C:\Windows" (Goto skip) ELSE (Goto continueScript

:skip
echo Found in the Windows Directory (Not Deleting...)
<-----:skip2 <---------------------------------------------------------------------
| echo Found in the Program Files Directory (Not deleting...) | Infinite loop
---->:continueScript |
if "%%~dpD"=="C:\Program Files" (Goto skip2) ELSE (Goto continueScript) <---
echo removing files/folder
echo rmdir /s /q "%%~dpD" && echo OK
)

Unfortunately this will not ignore folders under C:\Windows such as C:\Windows\System32 etc, and if it finds a program in C:\Program Files it will go into an infinite loop
so if I were to do something like this

Code: [Select]if "%%~dpD"=="C:\Windows" (Goto skip) ELSE (Goto continueScript

:skip
echo Found in the Windows Directory (Not Deleting...)

:continueScript
if "%%~dpD"=="C:\Program Files" (Goto skip2) ELSE (Goto continueScript)
echo removing files/folder
echo rmdir /s /q "%%~dpD" && echo OK

:skip2
echo Found in the Program Files Directory (Not deleting...)
)

also, I don't want to ignore the folders like C:\windows or c:\windows\system32 or c:\program files, I just don't want to delete the folder but make a note of it.sorry for the double post, I have made some changes and now I am not sure if the script deletes the objects or not but it only loops through the file once and I am not sure why. I believe it to be my testing statements, but once again, not sure why it doesn't loop.

Code: [Select]@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 (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...)
Goto endScript

:endScript
)

echo Searching Hidden Files and Folders for !filename!

for /f "delims==" %%D in ('dir /s /b /a:H "!filename!"') do (
echo !filename! found in folder %%~dpD
echo removing files and folders
rmdir /s /q "%%~dpD" && echo OK
)
)
pause


I am going to be adding the same looping statement to the hidden folder search, but that is when I can get the regular search to work.


Discussion

No Comment Found