How could I pipe a string into my batch file?

October 16, 2010 at 06:39:40
Specs: Windows 7
So I have this batch file:
@echo off
set /p searchName= 
tasklist|find "%searchName%" > buffer.txt
set /p var= <buffer.txt
IF "%var%."=="." GOTO notFound ELSE GOTO found

:found
FOR /F "tokens=1,2" %%i IN ('echo %var%') DO set proc=%%j & set name=%%i
echo %proc%

:notFound
echo 00000
GOTO end

:end
DEL buffer.txt
set var=
exit

And all this file does it, given a search string, it will return the first process ID of X program. For ex) I set searchName="cmd.exe" (sans the quotes) and get back "964". Beautiful.
Now here's the issue:
This little batch file is part of another program. I want to use ANY program to call this program, so I don't want to rely on environment variables.
I'd LIKE to just do something like:
FindProcID.bat cmd.exe or cmd.exe | FindProcID.bat
And get back:
964
But how?
How do I tell my batch file to grab the string entered just after itself?
Any/all help is appreciated. :-) Thanks!

PS: I realize that outputting from the command line to a file and then reading that file back in is redundant, but I can't figure out another way to do it, so I'm open to ideas. :-)


See More: How could I pipe a string into my batch file?

Report •


#1
October 16, 2010 at 07:44:37
I am surprised a scripter working out such a batch needs advice about this issue (even if your batch can be anyway streamlined). It is much like alien people can manage atomic power but have no knowledge of electricity.

So use the command tail parameter method, i.e.

FindProcId Your_String

and modify as follow your code

@echo off
set searchName=%1
if not defined searchName set /p searchName=

About your PS mow I have no time to answer, maybe later...


Report •

#2
October 16, 2010 at 15:39:50
Thanks so much for the response IVO.
I live mainly in the C++, PHP world, and I've only just recently started playing around with Batch scripting, thus the advanced nature of the script and the dumb question side by side. :-)
That works great! Thanks!
For the rest of the world, here is my program as it stands now:
@echo off
set searchName=%1
tasklist|find "%searchName%" > buffer.txt
set /p var= <buffer.txt
IF "%var%."=="." GOTO notFound ELSE GOTO found

:found
FOR /F "tokens=1,2" %%i IN ('echo %var%') DO set proc=%%j & set name=%%i
echo %proc%
GOTO end

:notFound
echo 00000
GOTO end

:end
DEL buffer.txt
set var=


Report •

#3
October 17, 2010 at 07:09:42
Well, here the streamlined batch that fits your PS question. Notice that 2^>nul may be omitted since I had to set it to workaround a bug under my system.

You can see I avoid to use GOTO statements: as far as you can follow the rules of structured programming in batch scripting to work out neater code.

I suggest you take a look at the on-line short tutorial related to commands by typing at prompt the /? switch after the opcode (e.g. set /?). Do this even for commands you believe to know fine as surprises may blow up.

I strongly raccomand you type SET /? to read about the "Delayed Expansion" of environment variables that affects FOR loops and any sequence of commands embraced by left and right parenthesis.

So you can achieve a full and safe control of "atomic power" avoiding common pittfalls

@echo off
set searchName=%1

set proc=00000
for /F "tokens=1,2" %%i in ('tasklist 2^>nul ^| find "%searchName%"') do (
  set name=%%i
  set proc=%%j
)
echo.%proc%
:: End_Of_Batch


Report •
Related Solutions


Ask Question