Value not deciphered - batch-file

I am trying to print the value in BUILD_VER using below code,for some reason instead of the value in BUILD_VER ,"BUILD_VER" gets printed,
can anyone suggest why is it so?
#echo off
REM set $NetPath="Z:\Build_ver\build_ver.txt"
set $NetPath="\\Network\Build_ver\build_ver.txt"
set /p version=<\\Network\Build_ver\build_ver.txt
set $BUILD_VER= %version%
echo BUILD_VER

#echo off
REM set $NetPath="Z:\Build_ver\build_ver.txt"
set $NetPath="\\Network\Build_ver\build_ver.txt"
set /p version=<\\Network\Build_ver\build_ver.txt
set BUILD_VER=%version%
echo %BUILD_VER%
You need to encase variables in % (percent) symbols, to output their values.
I also took out the dollar symbol. If you included it the last two lines would be -
set $BUILD_VER=%version%
echo %$BUILD_VER%
It's just a bit redundant.

Related

How to use an environment variable for length in a variable string substitution?

I have the following code:
set name=James
echo %name:~0,4%
And it displays the first 4 letters of the variable: Jame.
But I'd like to insert a variable for the number of letters displayed which I can change with set. Something like this:
set num=3
set name=James
echo %name:~0,%num%%
The result should be showing the first num characters of string value of variable name.
Is there any method to get desired result?
You can either add a layer of % around the echo statement and call echo it.
#echo off
set num=3
set name=James
call echo %%name:~0,%num%%%
or you can simply enabledelayedexpansion
#echo off
setlocal enabledelayedexpansion
set num=3
set name=James
echo !name:~0,%num%!
For more help on delayedexpansion just run setlocal /? from cmdline.

How to set a variable that is saved on another file in batch

Okay, so I want to be able to set my variable "EQP1" to whatever "chareqp1.txt" has printed in it. I have this, but it doesn't work, when I echo the variable it is saved as the text "C:\Users\Slots\Slot1\chareqp1.txt".
set /a "EQP1=C:\Users\Slots\Slot1\chareqp1.txt"
What's wrong with it?
Use set /p instead of set /a
Set /P "EQP1="<"C:\Users\Slots\Slot1\chareqp1.txt"
You are using the wrong parameter, it's set /p and you need redirection to read the first line from a file.
set /p "EQP1=" <"C:\Users\Slots\Slot1\chareqp1.txt"
If it's not the first line you'll have to parse the file with for /f and maybe findstr

How to echo a variable multiple times by multiplying it with another variable?

I am writing a batch script and I am having trouble echoing a variable, here is the script,
#echo off
set num1=1
set num2=10
set /a "out=%num1%*%num2%"
echo %out%
pause`
The output I receive is 10 which makes sense but I want it to echo 'num1' ten times instead of multiplying 'num1' by 'num2'. So I want the output to be 1111111111.
Also I don't want to loop the command 10 times as I am putting the output into a text file with 'output>> file.txt' otherwise I will end up with this in the text file,
1
1
1
1
1
1
1
1
1
1
I want to end up with 1111111111, thank you.
#ECHO OFF
SETLOCAL
set num1=1
set num2=10
SET "out="&FOR /L %%a IN (1,1,%num2%) DO CALL SET "out=%%out%%%%num1%%"
echo %out%
GOTO :EOF
The SET "out=" and FOR /L %%a IN (1,1,%num2%) DO CALL SET "out=%%out%%%%num1%%" may be on separate lines if desired. Setting out to nothing is simply a safety measure to ensure that if it contains a value, it's cleared first.
The for /L performs the call command num2 times.
The call command executes SET "out=%out%%num1%" in a subprocess because each %% is interpreted as an escaped-% (% is the escape character for %) - "escaping" a character means turning off its special meaning.
The syntax SET "var=value" (where value may be empty) is used to ensure that any stray trailing spaces are NOT included in the value assigned.
Just to show a different method with set /A and a loop:
#echo off
set /A "num1=1,num2=10,out=0"
:loop
set /a "out*=10,out+=num1,num2-=1"
If %num2% gtr 0 goto :loop
echo %out%
pause
This is the simplest way to solve this problem, using Delayed Expansion.
#echo off
setlocal EnableDelayedExpansion
set num1=1
set num2=10
set "out="
for /L %%i in (1,1,%num2%) do set "out=!out!%num1%"
echo %out%
pause
PS - The multiply term is not exact in this case; perhaps "echo a variable the times indicated by another variable" be more clear...
If what you want is to print num1 num2 times in the same line you can do something like:
#echo off
set "num1=1"
set "num2=10"
(for /L %%i in (1,1,%num2%) do set /p "=%num1%" <nul
echo()>file.txt
The command set /p "=%num1%" <nul prints the text %num1% in the current line without the LF character. So num1 gets printed num2 times in the same line.

Clear variables values in batch

I am reading two text files to set the values of two variables (u,l). Now I want to write script to run multiple files. When it is reading first file it will set the variables from the respective files but when it is reading second file it will set the same values of those variables.
#echo on
set /p u=< ul.txt
set /p l=< ll.txt
echo %u%-%l%
I tried SETLOCAL/ENDLOCAL option but in that case it is not reading variables values and getting error that ECHO is off. Even I wrote set u= and set l= at the initial of the script but not working in my case.
Your code, as given, works fine. However, I'm guessing it is code from inside an if statement, or for loop. If that is the case, you should use delayed expansion. You can use delayded expansion like this:
This is an example, not the exact code you need:
#echo on
setlocal EnableDelayedExpansion
if 1 equ 1 (
set /p "u=< ul.txt"
set /p "l=< ll.txt"
echo !u!-!l!
)
pause
FOR /L %%G IN (1,1,1) DO (
set /p "u=< ul.txt"
set /p "l=< ll.txt"
echo !u!-!l!
)
pause
set /p u=< ul.txt
set /p l=< ll.txt
echo %u%-%l%
pause
Note that inside the if statement and for loop, you replace % signs, when they are around variable names, with !. So %someVar% becomes !someVar!, but %%F stays %%F.
Outside of if statements and for loops, so outside of (), you use the normal %someVar%

Batch File - Capture the Time Down to the Second into a Variable

How would I capture the time down the second into a variable using a batch file. My script right now looks like this.
CLS
#ECHO OFF
set yy=%date:~-4%
set mm=%date:~-7,2%
set dd=%date:~-10,2%
set newdate=%dd%%mm%%yy%
echo %newdate%
I've captured the date, and it prints exactly what I need, but now I need to append the time to the variable. How would I do it?
#ECHO OFF
CLS
set yy=%date:~-4%
set dd=%date:~-7,2%
set mm=%date:~-10,2%
set newdate=%dd%%mm%%yy%_%Time:~0,8%
set newdate=%newdate::=%
echo %newdate%
pause
Read the time into a variable first to get a snapshot so that it doesn't keep ticking while you're extracting the fields. Then use the set substring operators to extract what you want.
The time has the format HH:MM:SS.MS (at least in my en-us locale). One gotcha is that the hour field might start with a leading space, so you need an if condition to change it to a leading zero or to remove the space.
set "current_time=%time%"
set "hour=%current_time:~0,2%"
if "%current_time:~0,1%"==" " set "hour=0%current_time:~1,1%"
set "min=%current_time:~3,2%"
set "sec=%current_time:~6,2%"
set "ms=%current_time:~-2%"
set "newtime=%hour% %min% %sec% %ms%"
echo %newtime%
If you want to remove the front space in the hour instead of changing it to a leading zero, then you'd do this instead:
set "hour=%current_time:~0,2%"
if "%current_time:~0,1%"==" " set "hour=%current_time:~1,1%"
This worked for me:
CLS
#ECHO OFF
set yy=%date:~-4%
set mm=%date:~-7,2%
set dd=%date:~-10,2%
set newdate=%dd%%mm%%yy% & time /T
echo %newdate%
pause
You can run multiple commands by adding the & symbol. If you wish to only append time, if the first command suceed, use && instead.

Resources