Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Windows Batch - Variable Increment Inside FOR Loop

Posted on 2009-02-23
11
Medium Priority
?
4,391 Views
Last Modified: 2012-06-27
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

0
Comment
Question by:benjunmun
  • 4
  • 4
  • 3
11 Comments
 
LVL 21

Accepted Solution

by:
AmazingTech earned 300 total points
ID: 23714088
You need to use ! for your variables
@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

0
 
LVL 16

Assisted Solution

by:t0t0
t0t0 earned 200 total points
ID: 23714365
Try this.... No need for expansion of variables. No need for token. No need for arithmetic.

@echo off
set outfile=tempfile.txt
FOR /F "tokens=*" %%i IN ('DIR /a-d /s /b') DO (
   for /f "tokens=*" %%a in ('findstr /i "example" "%%i" ^| findstr /i /v "examplestring"') do (
      echo %%~nxi>>%outfile%
   )
)
0
 
LVL 21

Expert Comment

by:AmazingTech
ID: 23715350
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

0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

 

Author Closing Comment

by:benjunmun
ID: 31550158
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!
0
 

Author Comment

by:benjunmun
ID: 23715627
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?
0
 
LVL 21

Expert Comment

by:AmazingTech
ID: 23715673
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
0
 
LVL 16

Expert Comment

by:t0t0
ID: 23715847
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
0
 
LVL 16

Expert Comment

by:t0t0
ID: 23715878
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?
0
 
LVL 16

Expert Comment

by:t0t0
ID: 23715889
AmazingTech, by the way, now that a solution has been accepted, there's no need to muck about with the testdata etc I posted....
0
 
LVL 21

Expert Comment

by:AmazingTech
ID: 23715950
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.
0
 

Author Comment

by:benjunmun
ID: 23715986
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!
0

Featured Post

[Webinar] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The following is a collection of cases for strange behaviour when using advanced techniques in DOS batch files. You should have some basic experience in batch "programming", as I'm assuming some knowledge and not further explain the basics. For some…
Introduction: Recently, I got a requirement to zip all files individually with batch file script in Windows OS. I don't know much about scripting, but I searched Google and found a lot of examples and websites to complete my task. Finally, I was ab…
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Suggested Courses
Course of the Month10 days, 18 hours left to enroll

572 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question