How to assign decimal to variable on windows command line? - windows

echo %time:~-5% returns a 5-character string, such as
18.09
However, i cannot seem to set that string to a variable:
set start = %time:~-5%
echo %start%
returns
10:07:18.09
what's simple way to get those last 5 chars into a variable?
thx

Windows doesn't like space next to =. So try this ...
set start=%time:~-5%
echo %start%

Related

How to compare characters in Batch script when left hand side is double quote

I have Windows Batch code where I'm trying to test if the first character of value is a hyphen but the code fails when the first character of value is a double quote. (value is coming from elsewhere in the real code.)
set value=""This is a test""
set arg="%value%"
set prefix=%arg:~1,1%
if "%prefix%" == "-" ...
Evaluates to
if """ == "-"
and generates The syntax of the command is incorrect. I tried inserting a caret into both sides of the equality check
if "^%prefix%" == "^-" ...
but that generates the same error with
if "^"" == "^-"
You can use the caret to escape a single character, like
if ^"^%prefix%^" == "-" ...
The trick is to escape the quotes, too, else the inner caret has no special meaning.
This can't work for more than one character, but you could switch to the even simple delayed expansion. It's expansion is always safe.
setlocal EnableDelayedExpansion
set "value="This is a test""
set "arg=!value!"
set "prefix=!arg:~1,1!"
if "!prefix!" == "-" ...
test if the first character of value is a hyphen
Another approach (using the original string, no additional variables):
#echo off
setlocal EnableDelayedExpansion
set value1=-correct
set value2=xfail
set value3="fail
set value
echo ---------
echo !value1!|findstr /bc:"-" >nul && echo hyphen || echo something else
echo !value2!|findstr /bc:"-" >nul && echo hyphen || echo something else
echo !value3!|findstr /bc:"-" >nul && echo hyphen || echo something else

Bash create a path with variables divided by _ and check if exist

In Bash, I am trying to create a path with two variables within:
/path/to/my/file/${variable1_-}${variable2}/Still/some/path
My variable2 is always set, but the variable1 might be empty and in that case I don't want to print the "_"
I have tried the line above, but doesn't seem to be correct.
Can someone help in getting the right path printed?
Thanks in advance for your suggestions!
You have a simple typo (the underscore should be after the separator, not part of the variable name) and you want to include the underscore if variable1 is set, not it it's unset (so plus instead of minus in the parameter expansion; and add a colon to also cover the set but empty case). Presumably you also want to include the actual value of variable1 when it's set.
/path/to/my/file/${variable1}${variable1:+_}${variable2}/Still/some/path
or equivalently, nested
/path/to/my/file/${variable1:+${variable1}_}${variable2}/Still/some/path
where the braces before the underscore are necessary to separate the variable name from the literal text.
You can use this.
https://linux.die.net/man/1/bash
${parameter:+word}
set also variable1
variable1=VAR1
variable2=VAR2
variable3=${variable1:+_}
echo /path/to/my/file/${variable1}${variable3}${variable2}/Still/some/path
set only variable2
variable1=
variable2=VAR2
variable3=${variable1:+_}
echo /path/to/my/file/${variable1}${variable3}${variable2}/Still/some/path
With a few more lines of code this could work:
run () {
prefix="" # empty
if [ -n "$variable1" ]; then
prefix="${variable1}_"
fi
echo "/path/to/my/file/${prefix}${variable2}/Still/some/path"
}
# set only variable2
variable2=var2
run
# set also variable1
variable1=var1
run
output:
/path/to/my/file/var2/Still/some/path
/path/to/my/file/var1_var2/Still/some/path
description:
-n tests if the string is not empty, in that case I fill prefix with variable1 and the underscore

Batch file does not echo empty string value, leaves %var1% in output

I thought that batch files would allow for empty variable to be output as a null string (""). If I set var1 to nothing, I thought I should
set "var1="
echo Variable value is: %var1%
I thought I'd get this:
Variable value is:
I actually get this as the output:
Variable value is: %var1%
Why isn't the var1 variable evaluating a blank string? For the record, I tested this with a new CMD instance in Windows 7.

Computername variable in cmd

In CMD the following variable will give you the name of the computer: %COMPUTERNAME%
I need a variable that takes a part of the computername.
I need a if statement that checks if the computername contains "KM" at the start and 00 at the end. It should not look at the number between KM and -00
KM100-00
KM200-00
This works here:
echo %computername%| findstr "^KM.*00$" >nul && echo found the right format
You can do this with substring commands, as per the following transcript:
pax> set xyzzy=KM100-00 KM200-00
pax> echo %xyzzy%
KM100-00 KM200-00
pax> echo %xyzzy:~0,2%
KM
pax> echo %xyzzy:~-2,2%
00
pax> if %xyzzy:~0,2%==KM if %xyzzy:~-2,2%==00 echo yes
yes
That final (chained) if statement is the one you're looking for to see if your variable starts with KM and ends with 00.
The expression %X:~Y,Z% will give you the Z characters starting at position Y (zero-based) of the variable X. You can provide a negative value of Y to make it relative to the end of the string.
echo %computername%| findstr /I /b "KM" | findstr /i /e "00" && echo computer name is like KM-XX-00
You can try also with hostname instead of echo %computername%
I recommend you to read this page, which is about substring usage in command prompt.
And why dont you try this;
set str=KM2000-00
echo.%str%
set pre=%str:~0,2%
echo.%pre%
set pst=%str:~-2%
echo.%pst%
IF %pre% == KM( IF %pst% == 00( echo.true ) )
pause

assigning an alphanumeric value to a variable in batch

I want to assign the alpha numeric value to one variable in Batch scripting.
I tried following one but getting error.
setlocal
set test = \765514e2aad02ca658cc56cdb7884947 *E:\\test1
echo %test%
endlocal
Error:
C:\Users\bgannu>setlocal
C:\Users\bgannu>set test = \765514e2aad02ca658cc56cdb7884947 *E:\\test1
C:\Users\bgannu>echo 0
0
C:\Users\bgannu>endlocal
The syntax for set is set [[/a [expression]] [/p [variable=]] string]
The = has to be directly after your variable so you need to change:
set test = \765514e2aad02ca658cc56cdb7884947 *E:\\test1
to:
set test=\765514e2aad02ca658cc56cdb7884947 *E:\\test1
Otherwise your variable name would have a space at the end. You can easily try this out:
> set bar = foo
> echo %bar%
%bar%
> echo %bar %
foo
Note that both the variable name and its content got a space.
Lose the /A. the /A is used for arithmetic.
C:\test>set var=\765514e2aad02ca658cc56cdb7884947 *E:\\test1
C:\test>echo %var%
\765514e2aad02ca658cc56cdb7884947 *E:\\test1

Resources