for loop find result into variable

November 11, 2009 at 05:20:57
Specs: win xp 5.1.2600, 3mhz/2 gig
Hi all, I have been puzzling over this for a while and can't crack it. Please help!
I need to store the FIND result into a variable.
Thank you.

setlocal enabledelayedexpansion
for /f "tokens=* delims=" %%a in for /f "tokens=* delims=" %%a in ('find /i "TotalKeyPresses" < "c:\docume~1\pirol\mydocu~1\samurize\plugins\KeyMouseCounts.ini"') do set KeyP=%%a
set KeyPresses=!KeyP:~16!
echo !KeyPresses!

See More: for loop find result into variable

Report •

November 11, 2009 at 07:03:08
Hi, I figured it out.
Had to add the '^' symbol. I looked at some code one of the pro's helped mw with recently and copied the idea. I don't understand what it does but it works!

here is the new line:
for /f "tokens=* delims=" %%a in ('find /i "TotalKeyPresses" ^< "c:\docume~1\pirol\mydocu~1\samurize\plugins\KeyMouseCounts.ini"') do set KeyP=%%

Report •

November 11, 2009 at 07:03:55
I'm not going to mess with a path half a block long, but note a couple things.

Within () you need to escape the <, like this:

in ('find "" ^< myfile')

Same goes for > and |.

I don't know which tokens or lines you need without seeing the file.

Helping others achieve escape felicity


Report •

November 11, 2009 at 07:07:23
"do set KeyP=%%"


Somebody get lee a cup of coffee.

Helping others achieve escape felicity


Report •

Related Solutions

November 11, 2009 at 07:13:39
Thanks M2! I originally used a variable for the file path, but the loop didn't work so I thought I would put the whole thing in for testing purposes.

Also, when I copied the for loop with '%%a' I didn't copy it properly. Sorry all. But it is working now!

I will keep in mind about using the '^' in my ( )'s!

M2, you don't think that I may have had too much coffee?!!


Report •

November 11, 2009 at 07:20:25
Do I need to use 'EndLocal' after the for loop?
I tried reading it up in CMD :endlocal /? but I can't make sense of it.

Please just explain it.

Thank you

Report •

November 11, 2009 at 07:57:45
Hi lee,

We probably need to ask IVO. I never use endlocal.

Helping others achieve escape felicity


Report •

November 11, 2009 at 08:01:30
Ok, thanks. I will send him a message.

Report •

November 12, 2009 at 03:25:17
Hi lee123abc, I got your message and here I am.

A post is not well suited to host a lesson on scripting style that probably looks cumbersome to you due to your poor knowledge of NT batch principles (see the ^ issue). So I just state some practical rules avoiding general theory.

- There are two basic SETLOCAL statements
setlocal (with no operands)
setlocal EnableDelayedExtension

- Both must be coded at the beginning of the script just after the @echo off initial command to suppress screen echo.

- The setlocal shields the system environment from the manipulations performed by the batch as setting or deleting variables.

- Actually you need to issue the long format of setlocal to enable the use of dynamic variables marked by ! inside For loops and if ... (commands) else (commands) statements.

- When the batch ends an ENDLOBAL is automatically issued, so you never need to code that command.

As I said these are raw rules as a detailed description of setlocal/endlocal requires a clear understanding of the mechanism related to environment inheritance and other issues about the behavior of cmd.exe in a multitasking system that are beyond a quick note.

To be short and avoid any confusion: issue setlocal at the beginning of your code (if needed to enable delayed expansion) and don't bother with endlocal, i.e. forget it.

Report •

November 12, 2009 at 03:41:22
Sorry about my poor knowledge. I just needed to get something done here and I am not a programmer. I try to help people in my office and at least I attempt the code.

Really I don't have time to learn all the details but I do try IVO.

I hope whoever helps me doesn't mind, I only ask because I think you enjoy helping.

Report •

November 12, 2009 at 03:42:06
IVO, thank you for replying though.

Report •

November 12, 2009 at 04:09:02
Hi lee123abc,

I apologize if I offended you; that was absolutely out of my mind. I just didn't want to bore you with principles that give little practical help and add confusion.

To solve problems is a must, to know programming is optional and life is rich of better opportunities.

Always glad to help.

Report •

November 12, 2009 at 04:13:59
Thanks IVO, I took it the wrong way. (Blame it on the rough morning)

I have always had all the help I ever needed from Computing.Net.
There are loads of great people like yourself that share their knowledge.

Thanks to all. (No doubt I will have more questions and problems soon so look out for my posts!)

Report •

Ask Question