| 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. 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 ! |
|