1.

Solve : Search String and delete it?

Answer»

Hello,

I am trying to create a batch file that looks for a string (let say abcde) on every file in the computer and if it finds it calls an other progran ( let say sub.bat). So I would like to make a combination of "finstr "and "del"

I am using Windows XP Pro.

Thank You

AlexandreThis can probably be done in batch (good luck with that!) , but with Windows, you'll do better with a recursive script. As written, the script will list the files to be deleted.

Code: [Select]On Error Resume Next

Set fso = CreateObject("Scripting.FileSystemObject")
Set dc = fso.Drives

For each ds in dc
Select Case ds.DriveType
Case 2
Set RootDir = fso.GetFolder(ds & "\")
GetThePaths(RootDir)
End Select
Next

Function GetThePaths(Folder)
For Each Subfolder in Folder.SubFolders
GetTheFiles(Subfolder.Path)
GetThePaths Subfolder
Next
End Function

Function GetTheFiles(FileFolder)
Set f = fso.GetFolder(FileFolder)
Set fc = f.Files
For Each fs in fc
If Instr(1, fs, "abcde") Then 'Change SEARCH string if necessary
WScript.Echo fs 'Replace line when ready to do deletes
End If
Next
End Function

Save the script with a vbs extension and run as cscript scriptname.vbs If you are satisfied with the results, change wscript.echo fs to fso.deletefile fs, True

Note: Checks fixed DISKS only.

Good luck. 8-)If I understand what you want, this should do the trick:
Code: [Select]@echo off
for /f "tokens=*" %%a in ('dir c:\ /s /a-d /b') do findstr /i /c:"abcde" "%%a" >NUL&if not errorlevel 1 echo sub.bat "%%a"For simplicity sake, I am assuming the computer has just a C: drive. If there is more than ONE drive, we can add a check for that, or just repeat the line for each drive.
I assume the string you are searching for is not case sensetitive
When you say "if it finds it calls an other progran ( let say sub.bat)" I assume it calls sub.bat with the found file as a parameter.
I'm also not sure where the "delete" comes in. Do you want to delete the found string? Or the file containing the found string?thank you for your answer, I tried it and it didin't do anything not even echo the file in which it found the string . For the delete part it would be deleting the file in which it ound the srting.

Thank YouGuess I misread your post. Thought you wanted to search the file names not the contents of the files.

Code: [Select]@echo off
for /f "tokens=*" %%a in ('dir c:\ /s /a-d /b') do findstr /i "abcde" "%%a" > nul & if not errorlevel 1 echo "%%a"

As previously posted, the script works fine. Are you sure the search string actually exists in any of your files? If you plan on deleting the found files, change ECHO to DEL or as previously mentioned, replace ECHO with CALL SUB.BAT %%a

Personally I would run it with ECHO first, where it will display what will happen if you change it to DEL.

Just a thought 8-)Thank You !!
It works very well
However it seems to be using alot of CPU
My final question is how would you LOOK for more than string in every file ( basicaly the above command x 2 except in one ) ?

Thank You

AlexandreWell, it will take a lot of CPU (actually probably a lot more disk I/O than CPU depending on your maching) to search your entire HD for the string of text you are searching for. I don't think there is anything that can be done about that.

I'm not sure what you mean by "how would you look for more than string in every file". Do you mean list each file that CONTAINS 2 different strings?I guess you want to look for more than one string? Here is a nice compact solution:

@echo off
for /f %%a in ('findstr /s /m /c:"abcde" /c:"http://[u][b]dostips.com[/b][/u]" *.*') do @echo %%a

Hope this helps Code: [Select]@echo off
for /f "tokens=*" %%a in ('dir c:\ /s /a-d /b') do findstr /i "abcde fghij" %%a" > nul & if not errorlevel 1 echo "%%a"

A space deliminates (??) multiple arguments. You don't need the /c: switch unless the argument contains embedded spaces.

Happy Computing. 8-)

Note: You can get help on any command by typing commandname /? at any command prompt.

When /c: switch is omitted then besides spaces the search string should not contain .*^$\
as these characters will be interpreted as part of a regular expression.
Hope this additional information is useful.



Discussion

No Comment Found