Computing.Net > Forums > Windows XP > How can I print out a variable in batch file

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

How can I print out a variable in batch file

Reply to Message Icon

Name: codelearner
Date: October 6, 2009 at 10:35:57 Pacific
OS: Windows XP
Product: Microsoft Windows xp professional edition
Subcategory: General
Comment:

Hi,

Please show me how to print out the below line in batch programming:

perl -e "sleep (rand 30)"

I tried:
set random_sec=perl -e "sleep (rand 30)"
echo %random_sec"

and it does not work.

Thanks for helping.
Codelearner



Sponsored Link
Ads by Google

Response Number 1
Name: kptech
Date: October 6, 2009 at 12:25:03 Pacific
Reply:

Codelearner,

You need to enclose the random_sec variable with "%".

Like this:

echo %random_sec%

-- kptech


0

Response Number 2
Name: codelearner
Date: October 6, 2009 at 14:25:44 Pacific
Reply:

I tried
echo random_sec = %random_sec%

and its output is nothing as shown below:

random_sec=


0

Response Number 3
Name: Wahine
Date: October 6, 2009 at 18:40:42 Pacific
Reply:

No problems setting the var and echoing here, perhaps you should check the syntax you are using in the Set command..

You have a space before the = in this
echo random_sec = %random_sec%

but it's not shown on the output
random_sec=


0

Response Number 4
Name: codelearner
Date: October 7, 2009 at 14:45:42 Pacific
Reply:

Here is my code:
set number_of_loop=%1
set /a counter=0

rem Number of loop must be enter:
if "%number_of_loop%"=="" (
goto error
)

:loop
set /a counter+=1
set random_sec=perl -e "sleep (rand 30)"
echo random_sec=%random_sec%
echo Number of loops=%counter%
if "%counter%"=="%number_of_loop%" (
goto end
) else (
goto loop
)


:error
echo Need to enter number of loop
echo Usage: Script_Name number_of_loop


:end

Result:
C:\WorkingFolderTemp>set number_of_loop=1

C:\WorkingFolderTemp>set /a counter=0

C:\WorkingFolderTemp>rem Number of loop must be enter:

C:\WorkingFolderTemp>if "1" == "" (goto error )

C:\WorkingFolderTemp>set /a counter+=1

C:\WorkingFolderTemp>set random_sec=perl -e "sleep (rand 30)"

C:\WorkingFolderTemp>echo random_sec=perl -e "sleep (rand 30)"
random_sec=perl -e "sleep (rand 30)"

C:\WorkingFolderTemp>echo Number of loops=1
Number of loops=1

Problem: cannot print out the random second


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More






Use following form to reply to current message:

Login or Register to Reply
LoginRegister


Sponsored links

Ads by Google


Results for: How can I print out a variable in batch file

How can I Print from a DOS program??? www.computing.net/answers/windows-xp/how-can-i-print-from-a-dos-program/34668.html

Help in BATCH file www.computing.net/answers/windows-xp/help-in-batch-file/176561.html

how can I print to pdf www.computing.net/answers/windows-xp/how-can-i-print-to-pdf/54140.html