Page 1 of 1

Findstr help

Posted: 19 Dec 2010 12:43
by rfpd
Hey guys i need some help i want to do a file that searchs multiple things in multiple files, say what files has that strings and then delete the files that has that string.

Regrats rfpd

Re: Findstr help

Posted: 20 Dec 2010 16:34
by aGerman
The following code displays .txt files of the current directory which contain 111 AND 222 AND 333.

Code: Select all

@echo off

for /f "delims=" %%a in ('dir /a-d /b *.txt') do (
  for /f "delims=" %%b in ('
    findstr /c:"111" "%%a" ^>nul ^&^&
    findstr /c:"222" "%%a" ^>nul ^&^&
    findstr /mc:"333" "%%a"
  ') do (
    echo "%%b"
  )
)

pause

Regards
aGerman

Re: Findstr help

Posted: 21 Dec 2010 08:24
by rfpd
it's not showing the name of the files that has the string

Re: Findstr help

Posted: 21 Dec 2010 09:05
by aGerman
Hmm, works for me. Please give an example for testing.

Regards
aGerman

Re: Findstr help

Posted: 22 Dec 2010 17:23
by rfpd
it's working sorry my mistake but it's not deleting the files i'm tring but i can't this is what i have:

Code: Select all

@echo off

for /f "delims=" %%a in ('dir /a-d /b *.txt') do (
  for /f "delims=" %%b in ('
    findstr /c:"hey" "%%a" ^>nul ^&^&
    findstr /c:"gh" "%%a" ^>nul ^&^&
    findstr /mc:"delete" "%%a"
  ') do (
    echo %%b
del %%b
  )
)
pause

Re: Findstr help

Posted: 23 Dec 2010 09:23
by aGerman
But the ECHO command shows the right files?
In this case it could be that you've got file names with spaces. Try to enclose the variable in double quotes.

Code: Select all

@echo off
for /f "delims=" %%a in ('dir /a-d /b *.txt') do (
  for /f "delims=" %%b in ('
    findstr /c:"hey" "%%a" ^>nul ^&^&
    findstr /c:"gh" "%%a" ^>nul ^&^&
    findstr /mc:"delete" "%%a"
  ') do (
    echo %%b
    del "%%b"
  )
)
pause

Re: Findstr help

Posted: 23 Dec 2010 11:28
by rfpd
i run it as administrator and it says file not founded

Re: Findstr help

Posted: 23 Dec 2010 11:45
by aGerman
I see. If you run it as administrator the working directory is %SystemRoot%\system32.
If you want to run it in the batch directory you could try this:

Code: Select all

@echo off
pushd "%~dp0"
for /f "delims=" %%a in ('dir /a-d /b *.txt') do (
  for /f "delims=" %%b in ('
    findstr /c:"hey" "%%a" ^>nul ^&^&
    findstr /c:"gh" "%%a" ^>nul ^&^&
    findstr /mc:"delete" "%%a"
  ') do (
    echo %%b
    del "%%b"
  )
)
popd
pause


Regards
aGerman

Re: Findstr help

Posted: 23 Dec 2010 16:53
by rfpd
stil not working but thanks anyways