Link to home
Start Free TrialLog in
Avatar of benjunmun
benjunmun

asked on

Windows Batch - Variable Increment Inside FOR Loop

Hi, I've reviewed several other solutions on this topic and nothing has seemed to work -- I was hoping someone could help analyze my code and figure out what I'm doing wrong.

My goal is to parse all the files in a directory and look for a specific string. If my string were "examplestring" I would need to look for files containing "example" but not "examplestring"

I realize the code I'm using to accomplish this is a tad circuitous, but I've been through many incarnations of this script and all have failed due to some of batch scripting's irregularities. The problem I'm seeing right now is that my variable "token" never gets set. When I pepper echo statements throughout the FOR loop, token always evaluates out to a blank variable.

Anyone have any ideas?

Below is the functional part of my code (there's some user input stuff and prompting for which folder to scan that I took out)

P.S. When I preview my code, it changes a tiny (but important) bit -- apologies if this explanation is unnecessary, but all of the SET statements that are &&'d or ||'d to FINDSTR read SET /a token<plussign><equalssign>1
@echo off
SETLOCAL ENABLEDELAYEDEXPANSION
 
 
FOR /F "DELIMS=" %%i IN ('DIR somedirectory /a-d /s /b') DO (
       SET /a token=0
 
	FINDSTR /I example %%i>nul && SET /a token+=1
 
	IF %token% == 1 (
		FINDSTR /I examplestring %%i>nul || SET /a token+=1
	)
 
	IF %token% == 2 (
		echo %%i>>"%outfile%"
	)
)

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of AmazingTech
AmazingTech

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of AmazingTech
AmazingTech

To be honest I haven't read the code in what it does. Merely trying to help in understanding why the token variable wasn't changing within the for loop.

However, t0t0 your code looks to be a fine example of how to get around using expansion variable.

I haven't tried this code but this would do the same thing. I think.
@echo off
 
FOR /F "DELIMS=" %%i IN ('DIR somedirectory /a-d /s /b') DO (
	FINDSTR /I "example" "%%i">nul && FINDSTR /I "examplestring" "%%i">nul && 	echo %%i>>"%outfile%"
)

Open in new window

Avatar of benjunmun

ASKER

All 3 solutions work (I've tested them now ^_^) but the first solution from Amazing was exactly what I was looking for. Thanks so much!
Thanks for the prompt responses, gang -- You really got me out of a bind. Before I let you all off the hook -- does anyone have a concise explanation for what the difference between %var% and !var! is, as far as batch is concerned?
When you use SETLOCAL ENABLEDELAYEDEXPANSION the !var! will be evaluated when it gets to that line.

The %var% was evaluated when the line with the FOR loop was reached.

So !var! dynamically changes within the for loop while %var% would stay static within the for loop
AmazingTech

Your solution does not give the correct results.

FOR /F "delims=" %%i IN ('DIR /a-d /s /b') DO (
   FINDSTR /I "example" "%%i">nul && FINDSTR /I "examplestring" "%%i">nul && echo %%~nxi>>"%outfile%"
)

Admittedly, I had to read the question at least twice myself because the logic of the code suggested the author wanted to find files containing both "example" AND "examplestring" however, this is not the case.

I have removed the "somedirectory" from your code as it's superflous also, I've changed your output to filename and extension only (for simplicity).

During testing, I included a "/v" switch in your second findstr however, I was still unable to get the desired output of only files containing "example" (and not "examplestring").

I have attached my data test files (rushed, but they do the job). The output should be:

file1.txt
file3.txt
file4.txt

The batchfile, while in the same folder as the test files, should not be included in your output (obviously!!).

My batchfile FINDEXAMPLE.BAT includes a jump to your code. REM it out and compare the results.

file1.txt
file2.txt
file3.txt
file4.txt
file5.txt
findexample.txt
tempfile.txt
AmazingTech....

Looks like I'm your assistant again.... hmmm.... The master and the apprentice!!

Your solution was accepted while I was still respondng (I hate that happening). Not to worry though, It's nice to have a quick turnover for a change.

By the way, did you know I was suspended last week for what I wrote about someone's code?
AmazingTech, by the way, now that a solution has been accepted, there's no need to muck about with the testdata etc I posted....
Yeah. I try to answer the question first. Then add more if solutions haven't been accepted. I too had one that I was added more to only to find out an answer was accepted. Oh well.
Many thanks to the both of you anyways. I gave amazing's first solution the most points because not only did it work with me simply replacing the %s with !s (thanks for the explanation, btw) but it also means that I can stop tip-toeing around for loops in batch now! You have NO idea how grateful I am for that =)

Cheers to you both and thank again!