Findstr help

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
rfpd
Posts: 78
Joined: 06 Jul 2009 16:19
Location: Lisbon, Portugal
Contact:

Findstr help

#1 Post by rfpd » 19 Dec 2010 12:43

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

aGerman
Expert
Posts: 4654
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Findstr help

#2 Post by aGerman » 20 Dec 2010 16:34

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

rfpd
Posts: 78
Joined: 06 Jul 2009 16:19
Location: Lisbon, Portugal
Contact:

Re: Findstr help

#3 Post by rfpd » 21 Dec 2010 08:24

it's not showing the name of the files that has the string

aGerman
Expert
Posts: 4654
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Findstr help

#4 Post by aGerman » 21 Dec 2010 09:05

Hmm, works for me. Please give an example for testing.

Regards
aGerman

rfpd
Posts: 78
Joined: 06 Jul 2009 16:19
Location: Lisbon, Portugal
Contact:

Re: Findstr help

#5 Post by rfpd » 22 Dec 2010 17:23

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

aGerman
Expert
Posts: 4654
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Findstr help

#6 Post by aGerman » 23 Dec 2010 09:23

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

rfpd
Posts: 78
Joined: 06 Jul 2009 16:19
Location: Lisbon, Portugal
Contact:

Re: Findstr help

#7 Post by rfpd » 23 Dec 2010 11:28

i run it as administrator and it says file not founded

aGerman
Expert
Posts: 4654
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Findstr help

#8 Post by aGerman » 23 Dec 2010 11:45

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

rfpd
Posts: 78
Joined: 06 Jul 2009 16:19
Location: Lisbon, Portugal
Contact:

Re: Findstr help

#9 Post by rfpd » 23 Dec 2010 16:53

stil not working but thanks anyways

Post Reply