findstr /f - Can you use wildcards in the file list?

Microsoft Windows
Post Reply
User avatar
MigrationUser
Posts: 336
Joined: 2021-Jul-12, 1:37 pm
Contact:

findstr /f - Can you use wildcards in the file list?

Post by MigrationUser »

07 Jul 2011 17:12
nc_jed

Here is my batch script:

Code: Select all

@ECHO OFF

For /f "tokens=2-4 delims=/ " %%a in ('date /t') do (set scandate=%%c%%a%%b)
For /f "tokens=1-3 delims=/:." %%a in ("%TIME%") do (set scantime=%%a%%b%%c)

REM echo %scandate%_%scantime%
REM echo %1
REM echo %scans%
REM echo findstr /si /c:"%1" /f:%pgms%\z_dirs.txt > %1.txt


findstr /si /c:"%1" /f:"z_dirs.txt" > %1.txt
echo z:\ done

findstr /si /c:"%1" /f:"y_dirs.txt" >> %1.txt
echo y:\ done

findstr /si /c:"%1" /f:"x_dirs.txt" >> %1.txt
echo x:\ done

findstr /si /c:"%1" /f:"w_dirs.txt" >> %1.txt
echo w:\ done
Here is the contents of z_dirs.txt:

z:\dashboar\app\*.fex
z:\yxwxun0r\app\*.fex
z:\eqnksjzt\app\*.fex
z:\infoassi\app\*.fex
z:\mimzjyxz\app\*.fex
z:\pulsee1j\app\*.fex
z:\pulsecmb\app\*.fex

When running it for a known present word, it returns nothing. It seems like the wildcard is not expanding or otherwise it is not using the file list. Note: There are fex files in this directory, it does not have to find them in a lower level directory.

When taken out of the batch, and substituted, everything is fine:

C:\Documents and Settings\E41334\My Documents\pgms>findstr /si runevent z:\pulseisu\app\*.fex
z:\pulseisu\app\chgclose.fex:-MRNOEDIT -INCLUDE RUNEVENT
z:\pulseisu\app\chgclsee.fex:-MRNOEDIT -INCLUDE RUNEVENT
z:\pulseisu\app\chgpntd.fex:-MRNOEDIT -INCLUDE RUNEVENT
z:\pulseisu\app\cpaudit.fex:-MRNOEDIT -INCLUDE RUNEVENT
z:\pulseisu\app\cpmyprje.fex:-MRNOEDIT -INCLUDE RUNEVENT
z:\pulseisu\app\cpmyproj.fex:-MRNOEDIT -INCLUDE RUNEVENT
z:\pulseisu\app\cppostav.fex:-MRNOEDIT -INCLUDE RUNEVENT
z:\pulseisu\app\cppostrm.fex:-MRNOEDIT -INCLUDE RUNEVENT
z:\pulseisu\app\cppostrs.fex:-MRNOEDIT -INCLUDE RUNEVENT
z:\pulseisu\app\doraudit.fex:-MRNOEDIT -INCLUDE RUNEVENT
z:\pulseisu\app\dorrobin.fex:-MRNOEDIT -INCLUDE RUNEVENT
z:\pulseisu\app\gluhspdt.fex:-MRNOEDIT -INCLUDE RUNEVENT
z:\pulseisu\app\gluhspdt.fex:-*-MRNOEDIT -INCLUDE RUNEVENT
z:\pulseisu\app\gluhspsm.fex:-MRNOEDIT -INCLUDE RUNEVENT
z:\pulseisu\app\golive_uhsp_emr_details1.fex:-MRNOEDIT -INCLUDE RUNEVENT
z:\pulseisu\app\golive_uhsp_emr_summary1.fex:-MRNOEDIT -INCLUDE RUNEVENT
z:\pulseisu\app\hsgldtls.fex:-MRNOEDIT -INCLUDE RUNEVENT
z:\pulseisu\app\hsgolive.fex:-MRNOEDIT -INCLUDE RUNEVENT
z:\pulseisu\app\mgrgraph.fex:-MRNOEDIT -INCLUDE RUNEVENT
z:\pulseisu\app\projstat.fex:-MRNOEDIT -INCLUDE RUNEVENT
z:\pulseisu\app\rptcpgr1.fex:-MRNOEDIT -INCLUDE RUNEVENT
z:\pulseisu\app\ticketds.fex:-*-MRNOEDIT -INCLUDE RUNEVENT
z:\pulseisu\app\tickethr.fex:-*-MRNOEDIT -INCLUDE RUNEVENT
z:\pulseisu\app\ticketrd.fex:-MRNOEDIT -INCLUDE RUNEVENT

When z_files.txt is forced to use pulseisu only, here are the results:

C:\Documents and Settings\E41334\My Documents\pgms>findstr /si runevent /f:"z_dirs.txt"

C:\Documents and Settings\E41334\My Documents\pgms>

What am I doing wrong?

----------------------------

#2 08 Jul 2011 03:55
RG


What are you passing in for the argument (%1)?
I would put
ECHO.1=%1]
Near the beginning of your file. You are using %1 as the string to search for and you are also redirecting the output to %1.

Windows Shell Scripting and InstallShield

----------------------------

#3 08 Jul 2011 16:10
nc_jed


RG, thanks for the feedback. The variable is being interpreted correct. I've placed an ECHO to see it, along with the other variables, in the batch file to see that they are substituted as intended (see the last 2 blocks of code for my usage). I commented them out in the first block of code after I verified that. The file is not getting overwritten. The redirection is to put the command output into a file with that name (plus a .txt extension). The file is created, so I know that is not the issue (it is just empty).

----------------------------

#4 09 Jul 2011 01:41
RG


nc_jed,
Sorry for being so slow to respond. I must admit that I don't have my mind wrapped around exactly what it is you are trying to do. I was waiting to see if someone else who does would chime in. Having an example of what you are passing in for %1 would be helpful so that others could try to duplicate what you are doing.

You can use the * in [drive:][path]filename as part of the FINDSTR command. But I think your problem is in your usage of the * in the file specified by /F:. Don't think you can do that. Perhaps you could use a FOR loop to iterate through the files. Something like this:

Code: Select all

for /f %%a in (z_dirs.txt) do findstr /si /c:%1 "%%a" > %1.txt
If all you are trying to do is do something with all the .fex files in a directory and all subdirectories you can simplify this by doing something like:

Code: Select all

for /r "%cd%" %%a in (*.fex) do echo.%%a
Where you would replace %cd% with the desired folder; and of course do something else other than echo.%%a

Last edited by RG (09 Jul 2011 16:12)

Windows Shell Scripting and InstallShield

----------------------------

#5 11 Jul 2011 12:08
nc_jed
RG wrote:

nc_jed,
Having an example of what you are passing in for %1 would be helpful so that others could try to duplicate what you are doing.

The word 'runevent'.

- nc_jed

----------------------------

#6 11 Jul 2011 23:31
RG


Assuming z_dirs.txt contains a list of files, you need a FOR loop to iterate through them.

Code: Select all

for /f %%a in (z_dirs.txt) do findstr /si /c:%1 "%%a" > %1.txt
Will do the job, assuming %1 is found in some lines of the file(s)

Windows Shell Scripting and InstallShield

----------------------------

#7 12 Jul 2011 14:35

nc_jed
RG wrote:

Assuming z_dirs.txt contains a list of files, you need a FOR loop to iterate through them.

Code: Select all

    for /f %%a in (z_dirs.txt) do findstr /si /c:%1 "%%a" > %1.txt
Will do the job, assuming %1 is found in some lines of the file(s)

That did the trick. I guess I'm still a little frustrated that the /f flag doesn't understand wildcards as I think it should, but in this business you have to be flexible and creative. Kudos on the workaround!

- Jed

----------------------------

#8 13 Jul 2011 16:53
flabdablet


Seems to me that

Code: Select all

for /f "delims=" %%a in (z_dirs.txt) do findstr /si /c:%1 "%%a" > %1.txt
would be safer; without "delims=" you'll get unexpected results if z_dirs.txt contains filenames with embedded spaces.
Post Reply