finding the files that have two strings matched?

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
nnnmmm
Posts: 141
Joined: 26 Aug 2017 06:11

finding the files that have two strings matched?

#1 Post by nnnmmm » 27 Aug 2017 20:34

i need to find some files from c: to z: drives

i need to find the files that have two strings matched in the filename

SET S1=GO
SET S2=DER

a batch should find files like below
c:\abc\ddd\flamingo in a meandering swamp.txt

what would be the batch file that can do this?

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

Re: finding the files that have two strings matched?

#2 Post by aGerman » 28 Aug 2017 00:55

Code: Select all

@echo off &setlocal
for %%i in (c d e f g h i j k l m n o p q r s t u v w x y z) do (
  if exist "%%i:\" (
    2>nul dir /a-d /b /s "%%i\*go*der*.txt"
  )
)
pause

I assume it will take ages.

Steffen

nnnmmm
Posts: 141
Joined: 26 Aug 2017 06:11

Re: finding the files that have two strings matched?

#3 Post by nnnmmm » 28 Aug 2017 03:17

Code: Select all

type nul> m:\11.txt
@echo on &setlocal
for %%i in (c d e f g h i j k l m n o p q r s t u v w x y z) do (
  if exist "%%i:\" (
    dir /a-d /b /s "%%i:\*go*ta*.*" >> m:\11.txt
    dir /a-d /b /s "%%i:\*ta*go*.*" >> m:\11.txt
  )
)
pause


what does "2>nul" mean in 2>nul dir /a-d /b /s "%%i\*go*der*.txt"?
it ran much much faster than using FIND and |

but i found that i could not cutomize this script without echo in it.
i need to import 11.txt to other application that requires few more strings need to be attached. because it can not tell if the datas are dirs or files or mixed.
i asked the quesiton wrongly in this thread, i will try some other time, thanks.
Last edited by nnnmmm on 28 Aug 2017 03:46, edited 1 time in total.

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

Re: finding the files that have two strings matched?

#4 Post by aGerman » 28 Aug 2017 03:42

There are 3 default stream numbers 0 for stdin, 1 for stdout, and 2 for stderr. While 0 (with <) and 1 (with > and |) are used by default you need to define the 2 if you want to redirect error messages using > or >>.
I wrote the 2>nul in order to suppress error messages of the DIR command in case no files were found that match the pattern.

You can define several patterns.

Code: Select all

>"m:\11.txt" type nul
for %%i in (c d e f g h i j k l m n o p q r s t u v w x y z) do (
  if exist "%%i:\" (
    pushd "%%i:\"
    >>"m:\11.txt" dir /a-d /b /s "*go*ta*.*" "*ta*go*.*"
    popd
  )
)

Steffen

Whoops seems you edited your post.

nnnmmm
Posts: 141
Joined: 26 Aug 2017 06:11

Re: finding the files that have two strings matched?

#5 Post by nnnmmm » 02 Sep 2017 06:36

Code: Select all

T="C:\UTI LITY\MOgoUSE SAMPLE RATtaE CHECKER.EXE"
T="C:\U TILITY\MOUSE SAMPLE atgoRATE CHECKER.TXT"
T="C:\UTILITY\XTGOLD\X   tata go TG_VSAM.XTP"


the output data format is like above

T= must be present at each line

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

Re: finding the files that have two strings matched?

#6 Post by aGerman » 02 Sep 2017 06:44

Just replace the DIR line with the following FOR /F loop.

Code: Select all

for /f "delims=" %%j in ('dir /a-d /b /s "*go*ta*.*" "*ta*go*.*"') do >>"m:\11.txt" echo T="%%j"

Steffen

nnnmmm
Posts: 141
Joined: 26 Aug 2017 06:11

Re: finding the files that have two strings matched?

#7 Post by nnnmmm » 03 Sep 2017 03:50

why couldnt i come up with this without using | and FIND
your line was totally good, i added
CMD /U /C echo T="%%j" >>11.sss to be even more perfect.

| and find.exe dont even use cache, even the 2nd run still takes half an hour
while
this

Code: Select all

for /f "delims=" %%j in ('dir /a-d /b /s "*go*ta*.*" "*ta*go*.*"') do >>"m:\11.txt" echo T="%%j"

takes few senconds for the 2nd and 3rd run even with different inputs
thanks for a help

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

Re: finding the files that have two strings matched?

#8 Post by aGerman » 03 Sep 2017 13:19

CMD /U /C
means you create a new cmd.exe process for each line you want to output.
You may want to run the whole FOR loop in a cmd.exe process with Unicode support.

Code: Select all

cmd /q /u /c "for /f "delims=" %%j in ('dir /a-d /b /s "*go*ta*.*" "*ta*go*.*"') do >>"m:\11.txt" echo T="%%j""

Steffen

nnnmmm
Posts: 141
Joined: 26 Aug 2017 06:11

Re: finding the files that have two strings matched?

#9 Post by nnnmmm » 03 Sep 2017 18:07

Code: Select all

AA=
SET FSPEC="*%FS1%*%FS2%*.*" "*%FS2%*%FS1%*.*"
TYPE NUL > %ZLS%
SET CC=0
FOR %%U IN (%DRSPEC%) DO (
   IF EXIST "%%U:\" (
      ECHO %%U:
      PUSHD "%%U:\"

      CMD /Q /U /C (
         FOR /F "DELIMS=" %%V IN ('DIR %FSPEC% /A-D /B /S') DO (
            ECHO T="%%V">>%ZLS%
            SET /A CC=CC+1
         )
      )
      POPD
   )
)


BB=
SET FSPEC="*%FS1%*%FS2%*.*" "*%FS2%*%FS1%*.*"
TYPE NUL > %ZLS%
SET CC=0
FOR %%U IN (%DRSPEC%) DO (
   IF EXIST "%%U:\" (
      ECHO %%U:
      PUSHD "%%U:\"

      CMD /U /C /Q (
         FOR /F "DELIMS=" %%V IN ('DIR %FSPEC% /A-D /B /S') DO (
            ECHO T="%%V">>%ZLS%
            SET /A CC=CC+1
         )
      )
      POPD
   )
)


DD=
SET FSPEC="*%FS1%*%FS2%*.*" "*%FS2%*%FS1%*.*"
TYPE NUL > %ZLS%
SET CC=0
FOR %%U IN (%DRSPEC%) DO (
   IF EXIST "%%U:\" (
      ECHO %%U:
      PUSHD "%%U:\"

      FOR /F "DELIMS=" %%V IN ('DIR %FSPEC% /A-D /B /S') DO (
         CMD /Q /U /C  ECHO T="%%V">>%ZLS%
         SET /A CC=CC+1
       )
      POPD
   )
)


AA=doesnt make unicode output
BB=doesnt make unicode output and causes error that seems big. CMD /U /C /Q i put /Q at the end, why would it make a difference?
DD=works, makes unicode output

I didnt use the "" that you surrounded them, because i used () to put the commands in a seperate lines.

ShadowThief
Expert
Posts: 1166
Joined: 06 Sep 2013 21:28
Location: Virginia, United States

Re: finding the files that have two strings matched?

#10 Post by ShadowThief » 03 Sep 2017 19:25

nnnmmm wrote:AA=doesnt make unicode output
BB=doesnt make unicode output and causes error that seems big. CMD /U /C /Q i put /Q at the end, why would it make a difference?
DD=works, makes unicode output

I really don't understand what these symbols are supposed to represent. Are they separate scripts?

nnnmmm wrote:I didnt use the "" that you surrounded them, because i used () to put the commands in a seperate lines.

Yeah... that's not valid batch syntax.

Have you considered Visual Basic?

nnnmmm
Posts: 141
Joined: 26 Aug 2017 06:11

Re: finding the files that have two strings matched?

#11 Post by nnnmmm » 03 Sep 2017 20:40

Code: Select all

SET DRSPEC=C D E G H I J K L M N O P Q R S T U V W

SET FS1=TA
SET FS2=GO
SET ZLS=M:\11.TXT

SET FSPEC=*%FS1%*%FS2%*.*" "*%FS2%*%FS1%*.*

nothing really fancy, they were just straight variable claims

too wordy, so re-edited

nnnmmm
Posts: 141
Joined: 26 Aug 2017 06:11

Re: finding the files that have two strings matched?

#12 Post by nnnmmm » 04 Sep 2017 19:25

Code: Select all

AA=
FOR /F "DELIMS=" %%V IN ('DIR %FSPEC% /A-D /B /S') DO (
   CMD /Q /U /C ECHO T="%%V">>%ZLS%
   SET /A CC+=1
)

BB=
CMD /Q /U /C "FOR /F "DELIMS=" %%V IN ('DIR %FSPEC% /A-D /B /S') DO ECHO T="%%V">>%ZLS%"
FOR /F %%D IN ('TYPE %ZLS% ^| FIND ":" /C') DO ( SET CC=%%D )


this is my conclusion
there may not be a way to make in this form CMD /Q /U /C () so that you could put commands in different lines

Code: Select all

DD=
CMD /U /C (             .... or CMD /U /C "&(  .... or CMD /U /C "& ... and etc
      FOR /F "DELIMS=" %%V IN ('DIR %FSPEC% /A-D /B /S') DO (
      ECHO T="%%V">>%ZLS%
      SET /A CC+=1
      )
 ...


i think i am done with this script, thanks for a help. i just need to test AA= and BB= for a while.

Post Reply