|
Answer» I have a script that searches the entire hard drive and if it finds a file to delete the corresponding folder, now I would like to search the whole hard drive, but if the file is found in say the Program Files directory, how can I tell the program to print out a warning, but and to continue on instead of deleting that file.
Here is what I am doing so far Code: [Select] 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 I'm guessing this is part of a larger script where your syntax is correct:
Code: [Select]for /f "delims==" %%D in ('dir /s /b "!filename!"') do ( set direc=%%~dpD IF "!direc!"=="C:\Windows\*" echo Warning else IF "!direc!"=="C:\Program Files\*" echo Warning else echo put your delete logic here
Someday, someone will have to explain to me at what POINT in time batch language became a programming language. Well I can I can post my entire script because when I was trying the checks I have, it wasn't working.
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 (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... )
So I was calling :skip and :skip2 if it was found in the directories, but the problem is that one of the files that I was searching for to test this script was IEXPLORE.exe and it found it in c:\windows\system32\... and it failed the check test c:\windows\* and it acted like it was going to delete the folder c:\windows\system32
So, what I need to do is see how I can have it check the folders, but print the warning.%%~dpD produces a path with the trailing backslash. In your Windows directory compare you used a wildcard which presumably is recognized as a literal.
You don't need a separate pass for the hidden items, use /a without any attributes.
Made the if instructions case-insensitive; you never know! Kept the for loops self-contained. Outside calls can screwup the return address.
Code: [Select]@echo off setlocal enabledelayedexpansion cd /d 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 /a /s /b "!filename!"') do ( set direc=%%~dpD IF /i "!direc!"=="C:\Windows\" echo !direc! Found in the Windows Directory Not Deleting... else IF /i "!direc!"=="C:\Program Files\" echo !direc! echo Found in the Program Files Directory Not Deleting... else ( echo !filename! found in folder !direc! echo removing files/folder echo rmdir /s /q "!direc!" && echo OK ) ) )
Good luck. is there any way to test against recursive folders in the windows directory. Like if it is anywhere below c:\windows don't delete it, but notify me?
I mean if its under the base c:\windows i can test it, but there are many subfolders under c:\windows and many are important. So is there anyway to recursively check the c:\windows\anyotherfoldernotallowednomatterhowdeep (any other folder not allowed no matter how deep)The bells and whistles added to the solution are screaming for you to use another tool. However in the interest of CH policy that the customer is always right, this little batch snippet might be helpful.
Code: [Select]@echo off setlocal enabledelayedexpansion cd /d c:\ cls echo ********************************************* echo **Delete Given File Names that can Be found** echo *********************************************
echo Studying the file for banned file names. title Searching...
for /f %%A in (H:\gameLocations\gameList.txt) do ( for /f "tokens=* delims=" %%i in ('dir "%%A" /a /s /b ^| find /i "%%A"') do ( set ddr=%%i set dw=!ddr:~0,10! set dp=!ddr:~0,16! if /i "!dw!" equ "c:\windows" (echo %%A Found In Windows ) else if /i "!dp!" equ "c:\program files" (echo %%A Found In Program Files ) else (echo %%A Found but not in Windows or Program Files) ) )
If the batch police see this, I'm a dead duck.
Note: replace this line (echo %%A Found but not in Windows or Program Files) with your delete logic.
Good luck.well can you name another language that I could use? I am using cmd because they basically want a solution that we can modify (its for an internship) but still be able to distribute to other tech support groups in our county.
Thanks for the help anyways. I think I will look into your solution. I ended up using a different solution because the one you posted had some errors (the one from like yesterday). It currently WORKS and can discover in the base of the windows directory, but not much father.
Is there a way I can break apart the string into tokens like you were doing but then join the first 2 tokens and see if it matches c:\WINDOWS?Yesterdays solution was not designed to look further down the directory tree than Windows or Program Files. Used the blueprint in your original post.
Today's snippet slices off a piece of the directory and checks for Windows or Program Files. This should include all the subdirectories under Windows and Program Files.
Batch code is designed as command language. A batch file ACTS as a container so you can stack program calls without having to repetitively type them in at the command prompt. Batch code is not designed as a programming language.
VBScript and JScript come installed with Windows and are great at down and dirty solutions. As with batch, they are interpreted with no need of all that pesky compile and linking of full-blown programming languages
I'll post a VBScript solution shortly.
Can you post the record layouts and the record count of the gamelist.txt file.gameList.txt looks like this
Code: [Select]halo.exe project64.exe
Not really sure by what you mean by this Quote Can you post the record layouts and the record count of the gamelist.txt file
I was actually surprised by the VBScript, but it seems pretty zippy. At LEAST this is readable and if your tech support groups have Windows they can run this.
Code: [Select]Const ForReading = 1
strComputer = "."
Set fso = CreateObject("Scripting.FileSystemObject") Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2") Set f = fso.OpenTextFile("H:\gameLocations\gameList.txt", ForReading)
Do Until f.AtEndOfStream = True retString = f.ReadLine fname = Split(retstring, ".")(0) fext = Split(retString, ".")(1) Set colFiles = objWMIService.ExecQuery _ ("Select * From CIM_DataFile Where FileName = '" & fname & "'" & " and Extension = " & "'" & fext & "'" & "") For Each objFile in colFiles If InStr(1, objFile.Caption, "c:\windows") > 0 Then WScript.Echo retstring & " Found In Windows...Not Deleting" Else If InStr(1, objFile.Caption, "c:\program files") > 0 Then WScript.Echo retstring & " Found In Program Files...Not Deleting" Else WScript.Echo retstring & " Found In " & objFile.Drive & objFile.Path WScript.Echo "Removing Files/Folder " & objFile.Drive & objFile.Path strFolder = Left(objFile.Drive & objFile.Path, Len(objFile.Drive & objFile.Path) - 1) 'fso.DeleteFolder strFolder, True End If End If Next Loop f.Close
Save script with a vbs extension and run as cscript scriptname.vbs
You can test this as written and see what it will do. When you ready for the production run, uncomment this line: 'fso.DeleteFolder strFolder, True by removing the single quote.
This script can be most destructive, be very careful in it's application.
Good luck.
thanks so much, that works like a charm. I changed one line to actually say what it was searching for, other than that, I left it how it was.
|