1.

Solve : How can you place a variable within a variable in batch file programming??

Answer»

I have been trying to create a batch file which outputs text followed by a respective number value, to a text document. My problem is that in order to do so, I need to place the variable %value% (in example below) within another variable.

I have been tearing my hair out - so any help would be greatly appreciated!

Note: the following example is not the exact task I am trying achieve but is a SIMPLIFIED example of the task, and I would probably prefer to limit the programs used, to batch programming and VBS script.

@echo off
set /a value=0
:start
set /a value=%value% + 1
set /p time%value%="When do you wish to begin your task?"
echo %time%value%% >> "Time Schedule.txt"
cls
choice /m "Do you wish to input another task?"
cls
if ERRORLEVEL 2 goto end
goto start
:end

Thanks in advance call echo %%time%value%%%Try not to use the TIME variable as it always changes. Set it as something like TME and use that variable instead. Quote from: Helpmeh on August 29, 2009, 05:03:09 AM

Try not to use the TIME variable as it always changes.

Please explain in the context of the posted script.

ThanksTry not to use %time% (the time variable) very often, as it always changes depending on the SYSTEM time.
Try this script and see what I mean. Time contains: hours (in 24hr format), minutes, seconds and milliseconds.

@echo off
echo %time%
ping localhost -n 1 > nul
echo %time%
pause > nulSure the System environment variable is changing every millisecond but what about Time set in the Command shell or Local environments?

A script running in either of those environments sets VARIABLES only for those environments not for the system so if Time is set in either environment the system environment variable remains unaffected and the variables set in Cmd and Local environments are fixed until changed within those environments or until the script finishes or until Endlocal is processed.

Try this:
Code: [Select]@echo off
cls

echo Echoing the time variable...
echo System environment time=%time%
echo.

echo Setting Time in Cmd shell environment to abcdefg...
set time=abcdefg
echo Echoing the time variable...
echo Cmd environment time=%time%
echo.

setlocal
echo Setting time in Local environment to qwerty...
set time=qwerty
echo Echoing the time variable...
echo Local environment time=%time%
echo.

echo Accessing System time from the Local environment...
set time=
echo Echoing the time variable...
echo New System environment time accessed from the Local environment=%time%
endlocal
echo.

echo Returning to Cmd time...
echo Echoing the time variable...
echo CMD environment time is still set to=%time%

Echo.
echo Returning to System time...
set time=
echo Echoing the time variable...
echo System environment %time%
Thanks guys - problem solved!

Particularly to 'Prince_' and i also learnt something from 'Helpmeh'

I wasn't looking for any WAY to set the actual computer time, just a way to use a variable with the name time in it (resolved by using 'tme' instead).
It was just a matter of placing an extra grouping of '%'s around the variable

ie
not echo %tme%value%%

but instead echo %%tme%value%%%

Thanks to everyone that replied
-Danielsrry. just found out it hadn't actually answered my problem but I found a great website that had already solved the problem:

http://www.codeproject.com/KB/winsdk/Batch_File_Bubble_Sort.aspx

Thanks for all ur help !


Discussion

No Comment Found