Batch scripting Return result into a variable - windows

I am creating a generic script that will parse a statement and return the result. I have done so in shell, but don't know how this can happen in batch scripting
Script 1 Main script (main.bat).
SET Myresult=CALL child.bat "Statement"
Now Myresult should store the answer whatever I want to return in this variable.
Solution 1 : SET Myresult in Child.bat and use it in main.bat but now what if the user does not know what the variable name is .
So is their a way to return a value like in java
return xyz
xyz gets captured in the call statement elsewhere.
-------------------------------xxxxxxxxxxxxxxxxxxxxxxx-----------
PART 2
Here are the details of what I am doing .
The so called child script is getSQLResult.bat
What is does is
getSQLResult.bat -q "Select a from abc"
Now this above call statement can be used by anyone any how in any batch script .
So apart from passing a variablename(return name) or writing a for loop to parse the result set is there any simple straight forward way .

Option 1 - Pass the variable name
main.bat
call child.bat myresult "Statement"
echo %myresult%
child.bat
set "%~1=argument was %~2"
Option 2 - Process output of child process
main.bat
for /f "delims=" %%a in ('child.bat "Statement"') do set "myresult=%%a"
echo %myresult%
child.bat
#echo off
echo Argument was %~1
or you can use temporary files, the registry, the clipboard, ... to pass the information, but in any case if you are coding a element (your child.bat) to be reused by you or another person, you are creating an interface between this element and the rest of the code, a expected set of input arguments and a way to return information. Your question
... what if the user does not know what the variable name is?
is answered by the documentation of this interface.

This is the way to do it in batch:
for /f "delims=" %%a in ('echo hello world') do set myresult=%%a
echo it returned: %myresult%
(echo hello world is just an example)
Note: this will return the last (or only) line only. It can be modified to process more lines, but your example implies, there is only one line of output)
EDIT If I understand your edit correct, you want to pass just a statement to an external batchfile, which does the work and gives back the result into a variable, which is defined with the call.
REM child.bat
#echo off
set var=%1
for /f "tokens=1,*" %%i in ("%*") do (
set var=%%i
set statement="%%~j"
)
for /f "delims=" %%a in ('getSQLResult.bat -q %statement%') do set "%var%=%%a"
use it with:
call child.bat result "Select a from abc"
echo %result%
of course it will still return the last (or only) line. Can be easily modified to get the first line. If the output could be more lines, I suggest using an "array" with an counter to avoid the problems, MC ND adresses in his comment to your question. Maybe returning a filename to a file with the multiline output could be another solution.

Related

Why can't you use a question mark in a batch for loop?

Preface
While writing a separate piece of code, I encountered a problem with question marks in for loops. As shown below, the question mark is not accessed in the for loop.
Batch file:
#echo off
for %%x in (the, quick, ?, brown, fox) do (
echo %%x
)
Output:
the
quick
brown
fox
This also does not work in the CMD (using %x instead of %%x), or when using "", [], ^, \, % or other common methods of character escaping.
Using a counter variable to determine the number of times the code within the parentheses was accessed only results in a total count of 4, meaning it is clearly not a problem with the echo command.
Question
Why doesn't a question mark work in a standard for loop, and how would I go about fixing it?
It's because ? will be expanded into a list of filenames one character long. The "naked" for is using that list as a list of filenames.
If you run the following commands, you'll see this in action:
c:\> echo xx >a
c:\> echo xx >b
c:\> for %i in (1, ?) do echo %x
1
a
b
If you look at Rob van der Woude's excellent scripting pages, you'll see that the for page has options for processing command output, numbers and files - it's not really suited for arbitrary strings.
One way to get around that is to provide your own for-like command as shown in the following example:
#echo off
setlocal enableextensions enabledelayedexpansion
rem Call the callback function for each argument.
set escapee=/
call :doFor :processEach 1 2 ? 4 5
echo.Escapee was %escapee%
rem Execute simple command for each argument.
call :doFor echo 6 7 ? 9 10
endlocal
goto :eof
:processEach
set escapee=%escapee%%1/
goto :eof
:doFor
setlocal
rem Get action.
set cbAction=%1
shift
:dfloop
rem Process each argument with callback or command.
if not "%1" == "" (
call %cbAction% %1
shift
goto :dfloop
)
endlocal&&set escapee=%escapee%
goto :eof
This provides a single functions which can handle both callbacks and simple commands. For more complex commands, provide a callback function and it will get called with each argument in turn. The callback function can be arbitrarily complex but keep in mind that, because it's operating within a setlocal, changes to environment variables cannot escape back to the caller.
As a way around this, it allows one variable, escapee, to escape the scope - you could also add more if needed.
For simple commands (like echo) where you just need the argument placed at the end, you do the same thing. It doesn't need a callback function but it's restricted to very simple scenarios.
Also keep in mind that, although this seems like a lot of code, the vast majority of it only needs to exist in one place. To use it, you simply need a one-liner like the sample:
call :doFor echo my hovercraft is full of eels
Also keep in mind that there may be other characters that do not fare well, even with this scheme. It solves the ? issue but others may still cause problems. I suspect that this would be an ideal opportunity to add PowerShell to your CV, for example, a command that's almost bash-like in it's elegance and zen-ness:
PShell> foreach ($item in #("1", "?", "3", "4")) { echo $item }
1
?
3
4
You could switch to FOR /F.
But FOR /F is used to process multiple lines to split them into tokens.
In your case you don't need multiple tokens, you need one loop per item.
That can be done by splitting the items with linefeeds.
I'm using # as item delimiter, but you are free to use any other character
#echo off
setlocal EnableDelayedExpansion
(set \n=^
%=EMPTY=%
)
set "itemList=the#quick#?#brown#fox"
for %%L in ("!\n!") DO (
FOR /F "delims=" %%x in ("!itemList:#=%%~L!") DO echo - %%x -
)
Output:
- the -
- quick -
- ? -
- brown -
- fox -
I've been coding with batch many years, and I'm suprised to realize this issue until now!
I found another way to deal with this problem. May be somebody prefers it, like me.
In my particularly case, I'm using the FOR LOOP to get some named arguments of the current function. This is what I did:
:SomeFunct
rem Replace ?
set "args=%*"
set "args=%args:?=`%"
rem Iterate args
for %%p in (%args%) do (
for /f "tokens=1,* delims=: " %%a in ("%%~p") do (
rem Get and store values
if /i "%%~a" equ "/a" set "argA=%%~b"
if /i "%%~a" equ "/b" set "argB=%%~b"
if /i "%%~a" equ "/c" set "argC=%%~b"
)
)
rem Restore ?
if defined argA set "argA=%argA:`=?%"
if defined argB set "argB=%argB:`=?%"
if defined argC set "argC=%argC:`=?%"
rem I use the args
rem ...
rem Return
goto:eof
I call the function like this:
rem Calling example
call:SomeFunct "/a:Is there" "/b:a question mark" "/c in the arguments?"

How to get a specific line from command output in batch script into a variable?

I'd like to get a changelist description from perforce, which involves calling a p4 describe -s , so the ouput would be as below. Is there a way to get (trimmed characters from the third line) from the output just using windows batch syntax?
Change 6582 by username on 2016/12/06 00:35:41
MyChangeDescription
Affected files ...
... //depot/foo.txt#7 edit
... //depot/foo2.txt#6 edit
Give this a shot:
p4 -Ztag -F %Description% change -o 6582
#ECHO OFF
SETLOCAL
SET "sourcedir=U:\sourcedir"
SET "filename1=%sourcedir%\q40986156.txt"
FOR /f "usebackqskip=2tokens=*" %%a IN ("%filename1%") DO (
SET "desc=%%a"
GOTO show
)
:show
ECHO "%desc%"
GOTO :EOF
You would need to change the setting of sourcedir to suit your circumstances.
I used a file named q40986156.txt containing your data for my testing.
This uses a file as input. Since I don't have access to perforce, I can't test it but
#ECHO OFF
SETLOCAL
FOR /f "skip=2tokens=*" %%a IN ('p4 describe -s') DO (
SET "desc=%%a"
GOTO show
)
:show
ECHO "%desc%"
GOTO :EOF
should be equivalent.
Simply, read the output of the command, skip the first 2 lines, tokenise the entire line, skipping leading spaces. Assign the string found to a variable and immediately exit the loop.

Windows Batch - Findstr and assign to a variable

I am trying to find a string in a file and assign the results to a variable for further processing. But for some reasons the code below doesn't work.
My string is always available on the 2nd line so I tried the code below, from which I can print the correct string on the console, but cannot assign it to a variable:
for /f "tokens=1*delims=:" %%G in ('findstr /n "^" C:\myfolder\payload.xml') do if %%G equ 2 echo %%H
Problem: Using the above method I'm unable to store the result in a variable
I have tried another method as well:
findstr /g "FilePath" C:\myfolder\payload.xml>>D:\Data\tmp.txt
set /p "Prev_FileName="<D:\Data\tmp.txt
echo %Prev_FileName%
Problem: with this method getting the output in the file tmp.txt but not in the variable.
In both contexts used SETLOCAL EnableDelayedExpansion
Could you please help as I am a beginner?
There are no unusual steps that need to be taken to set a variable in either context. How do you know the variable has not been set?
The first code you posted does not attempt to set anything, though if you changed the echo %%H into set "Prev_FileName=%%H", then it should work just find.
The second code should be setting the variable.
Why do you think your variable is not being set? I suspect you are doing something like echo %Prev_FileName%, and not seeing your expected result. That could happen if you are within a parenthesized block of commands, since they are all parsed at once, and %Prev_FileName% is expanded at parse time. You say you setlocal enableDelayedExpansion, but that does nothing unless you you also change the syntax for variable expansion. You should use echo !Prev_FileName! instead.

using for loop to input multiple data

I am new to programming. Here is my dilemma. I have to replace multiple files in multiple locations across multiple computers.
I have written a bat script where I am defining all the variables and calling a txt file with appropriate information. For example -testing.txt has the values
Apple, Potato,Beef
Apple, Potato,Pork
The logic I am applying is as follows: I am using this txt file for reading and then going to each location to change the file
set Path=%Path%;c:\Tools\UnxUtils\usr\local\wbin
SET SORC=C:\tools\logosource\NEWImages\ApiSite\Content
for /F "usebackq delims=, tokens=1-3" %%a in (C:\tools\xxxx\testing.txt) do (
SET HOSTNAME=%%a
SET CUSTNAME=%%c
SET STYPE=%%b
SET DEST=\\%HOSTNAME%\c$\Documents and Settings\blahblah\My Documents\%CUSTNAME%\%STYPE%\goodman\
echo HOSTNAME is %HOSTNAME%
echo CUSTNAME is %CUSTNAME%
echo STYPE is %STYPE%
echo DEST is %DEST%
echo SORC is %SORC%
)
copy "%DEST%\ApiSite\Content\images\michael.gif" "%DEST%"
copy /b /y "%SORC%\images\george.gif" "%DEST%\ApiSite\Content\images\michael.gif"
goto End
:Error
ECHO Error! You must pass in a servername
goto End
:End
The problem is that my loop is only reading the last line my txt file. ie. it reads "Apple, Potato,Pork" and sets the DEST TO THAT VALUE.
What i really want is to read line 1 (Apple, Potato,Beef) set the DEST using these parameters and change the files, then go back and read the second line (Apple, Potato,Pork) and set the DEST using these parameters and change the files.
Actually your code is reading every line in the file, but your logic is wrong.
Your main problem is you want to do the COPY statements for each line, but you have the COPY statements outside the loop. Of course they will only get executed once, and the values used will be the values that were set by the last line in the file. The solution is to move the COPY statements inside the loop.
Your other problem is you are attempting to set a variable within a parenthesized block, and then access the value using %var% - that cannot work because the expansion occurs when the statement is parsed and the entire block is parsed once before any lines are read. You could solve that problem by using delayed expansion, (type HELP SET from the command line prompt for more information about delayed expansion). But there really isn't any need to save the values in variables. Simply use the FOR variables directly. Because the DEST is used multiple times, I used an additional FOR loop to define a %%d variable that contains the DEST value. The ~ removes the quotes that the FOR loop added.
Also, you are ending the script by using GOTO END and defining an :END label at the end of file. That works, but there is an implicit :EOF label at the end of every script. You can simply use GOTO :EOF without defining a label. The other option is to use EXIT /B
You have an :ERROR routine that is not being called - I presume you have additional code that you are not showing.
set Path=%Path%;c:\Tools\UnxUtils\usr\local\wbin
SET "SORC=C:\tools\logosource\NEWImages\ApiSite\Content"
for /F "usebackq delims=, tokens=1-3" %%a in (C:\tools\xxxx\testing.txt) do (
for %%d in (
"\\%%a\c$\Documents and Settings\blahblah\My Documents\%%b\%%b\goodman\"
) do (
echo HOSTNAME=%%a
echo CUSTNAME=%%c
echo STYPE=%%b
echo DEST=%%~d
echo SORC is %SORC%
copy "%%~d\ApiSite\Content\images\michael.gif" "%%~d"
copy /b /y "%SORC%\images\george.gif" "%%~d\ApiSite\Content\images\michael.gif"
)
)
exit /b
:Error
ECHO Error! You must pass in a servername
exit /b

how to use dos commands to do following

At the following location
\ncsusnasent02.na.jnj.com\its_diq_na_win_dev\PowerCenter\infa_shared\WCPIT_BIO_EDW\SrcFiles\DDDMD\DDD.CLI026.WK0933.DDDMR45.001.head
I have one file
DDD.CLI026.WK0933.DDDMR45.001.head
if i open this file
i get data as following(in a single line)
HEADER0101IMS HEALTHDMD Weekly D DD.CLI026.WK0933.DDDMR45 Centocor DMDDRM45 W2009080210120090831125325ssnyder#us.imshealth.com
TRAIL0101 000000000581 0000000000CKSUM000002236804730
we need to copy 581(it will not be same always it gets updated everyday) from this file
and put it in a variable
you can try the below. It will set the field into the environment variable id:
for /f "tokens=10" %%a IN (%1) do (
SET id=%%a
)
echo %id%
You can pass the full path and file name into the bat as the first argument.
edit:
This simple bat will take the input from the file you specify on the commandline (param %1), it will use the default separators of <space> and <tab> to break the line in your file - defined in the IN set - into a set of tokens. The "tokens=10" param tells the processor to pass the 10th token, which turns out to be your number in question, into the DO block. It is passed in as a param %%a. Within the DO block, I simply assign that value to an environment variable id. After the for command is complete, I echo the value out to the console.
Take a look at the FOR command, specifically the part about the /F parameter.
I'm not certain enough about the structure of that line to even try to write the full command, but you should be able to write it yourself given that information.
Hmm to me it looks more like the guy needs a dos substr... i.e.
#Echo Off
If not %1.==[]. (Cmd /V:On /C Call %0 [] %1 & GoTo :EOF)
Shift
Set MyVariable=HELLOWORLD
Set ASubStr=!MyVariable:~%1!
Echo [!ASubStr!]
So for example save this as test.bat and then call "test.bat 5" and it will echo WORLD
Google DOS Substring and work out how to parse your text variable the way you want it.

Resources