We help IT Professionals succeed at work.

command line Batch for /f lookup, into for /r loop

urfried
urfried asked
on
821 Views
Last Modified: 2012-05-09
Hello gents,

Now i am getting the hang of creating very handy batch files,
i am trying to gain the complexity of my batches into a next level, but i have a problem immediately :).

I'll try to explain simple:

I have multiple files in a single dir.
With a FOR /R "%CD%" %%F IN (*.txt) DO (.......loop, i know which files and want to build a log file.
I echo some text with name of the 1st file found in the loop. So far it works.

Than the problem:
I would like to like to search for text-string in the files found in the loop above, and output the text-string into the log file:

for /f "tokens=* delims= " %%A in ( 'FIND /i "usr_device" ^< "%cd%\%%~nF.txt"' )  do set usr_output0=%%A
     echo %usr_output0% >> %log%

The single commandline is correct (tested), but it's not working within the loop.

Anyone having an idea ?
Is the question clear enough ?

I want a logfile like this:
 
09390.txt
------------------------------------------
usr_device                        NL03PR178
==========================================
      
09628.txt
------------------------------------------
usr_device                        NL03PR176
==========================================      




 09628.txt 09390.txt
set log="%cd%\test.txt"
echo. > %log%

:############################################################################################

FOR /R "%CD%" %%F IN (*.txt) DO (
 echo %%~nF.txt >> "%cd%\test.txt"
 echo ------------------------------------------>> %log%
   ( for /f "tokens=* delims= " %%A in ( 'FIND /i "usr_device" ^< "%cd%\%%~nF.txt"' )  do set usr_output0=%%A
     echo %usr_output0% >> %log%
   )
 echo ==========================================>> %log%	
)

pause

Open in new window

batch.jpg
Comment
Watch Question

CERTIFIED EXPERT
Most Valuable Expert 2019
Most Valuable Expert 2018
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

Author

Commented:
NICE.....Works like a charm.

Thanx a lot :)

Author

Commented:
This issue is probably a common mistake, and is very usefull for those looking for an answer on simular problem like this.

Also this is very difficult to search for and find proper articles on internet.

Commented:

No need to use delaye expansion at all. Also note a few differences such as not using %CD% which will come with more experience.

You'll notice the code is shorter and sleeker.




@echo off

(for /r . %%F in (*.c) do (
   echo %%~nxF
   echo ------------------------------------------
   for /f "tokens=*" %%a in ('find /i "#include" ^<"%%F"') do (
      echo %%a
   )
   echo ==========================================
   echo.
))>test.txt

Author

Commented:
Nice....

I will have a look at this too !

Thanx you both gents

Commented:
I forgot to mention...

The only problem with using FOR /R with FOR /F when searching through files is that FOR /R will recurse subfolders and if you use only ECHO %%~nxF then there is no real indication (where files are found in nested folders) which file's output is which.

If you do not intend to recurse subfolders and all you files are in a single flat folder then you would have been better using the following:

   for %%F in (*.txt) do (

as the outer loop.


Commented:
Sorry! I meant the following:



@echo off

(for /r . %%F in (*.txt) do (
   echo %%~nxF
   echo ------------------------------------------
   for /f "tokens=*" %%a in ('find /i "usr_device" ^<"%%F"') do (
      echo %%a
   )
   echo ==========================================
   echo.
))>test.txt



The previous code included test data i used to test the batch file worked okay before posting!

Commented:
I note the points have already been awarded nevertheless, I hope I did not mislead you with my first code - as explained above.

Author

Commented:
Thanx for the code and explanation.
Both comments were very usefull and have been a good lesson for me :)

I am trying to finish the batch with some more line, and past the result.

Grtz

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.