Shortcut Commandfile Commandline-Parameter Win7 more than one value - windows

I know how to set commandline parameters for shortcuts to cmd files, by modifying a target-shortcut property.
"C:\test\cT.cmd" -logpk cur -lp c:\xxx\testlogs
But how can i feed more than one argument values to an argument point
C:\test\cT.cmd -logpk cur -lp c:\xxx\testlogs -s 22.07.2014 14:30:00
The thing above works as cmd command but if i add it to a shortcut target the "-s" option is not filled by the cmd properly.
"C:\test\cT.cmd" -logpk cur -lp c:\xxx\testlogs -s 22.07.2014 14:30:00
Maybe you know how to split "morenode arguments" so they can be used for shortcuts too.
best regards
EDIT:
if "%argument%" == "-s" (
set startDay=%argumentValueOne%
set startTime=%argumentValueTwo%
if "%argumentValueOne%" == "a" set startDay=""
if "%argumentValueTwo%" == "A" set startTime=""
set startDate=-s %startDay% %startTime%
echo startDate %startDate%
shift
shift
shift
goto validate
)
EDIT2:
C:\test\>
if "-s" == "-s" (
set startDay=22.07.2014
set startTime=14:30:00
if "22.07.2014" == "a" set startDay=""
if "14:30:00" == "A" set startTime=""
set startDate=-s
echo startDate
shift
shift
shift
goto validate
)
-s -logpk cur -vendor -lp c:\xxx\testlogs
If called from cmd shortcut
EDIT3:
C:\test\>
if "-s" == "-s" (
set startDay=22.07.2014
set startTime=14:30:00
if "22.07.2014" == "a" set startDay=""
if "14:30:00" == "A" set startTime=""
set startDate=-s 22.07.2014 14:30:00
echo startDate -s 22.07.2014 14:30:00
shift
shift
shift
goto validate
)
-s 22.07.2014 14:30:00 -logpk cur -vendor -lp c:\xxx\testlogs
If called from opened cmd

you are running into the infamous early variable expansion problem. your variables are replaced with their values at the start of the block. since at that time startday and starttime are empty, this is what you get.
i suspect that in your testing from the command line, the variables are already set from the first try (they remain in the environment, you can see it with set).
the way around that is putting setlocal enabledelayedexpansion at the beginning of your batch file, and then reference your variables with !startday! instead of %startday%.

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

Lua - popen 2x '&&' chained set /p - stdout takes a vacation

I'm working in an environment that has approximately zero functional ui while a lua script is running. This just isn't acceptable for the script I need to write, which relies heavily on user input. The only way I've found to circumvent this is using io.popen with some rather crafty commands.
I recognize that what I'm trying to do here is strange and very much wrong, and that I've brought this upon myself, but I can't figure out what's going wrong in this code snippet:
local a = 'f'
local p = io.popen(
'echo -~`~-,_,- Editing '..(a == 'f' and 'foreground' or 'background')..' -~`~-,_,- > con && '.. --display text to the user
'set /p f= Find block: > con < con && '.. --user input
'call echo %f% &&' .. --pass f back to the lua script
'set /p r= Replace with: > con < con && '.. --user input
'call echo %r% &&' .. --pass r back to the lua script
'pause < con', "r")
local f = p:read("*a") --read what was passed back, later parse it back into 2 variables
p:close()
What I expect to happen:
A 'command prompt window' is displayed to the user, asking for input.
The user enters 2 values.
The values are echoed back to the lua script as they are entered.
The values are read from the pipe and stored for later use.
The command line waits for a keypress, and then closes.
What actually happens:
A 'command prompt window' is displayed to the user, asking for input.
The user enters a value for f.
f is echoed back to the lua script.
The user enters a value for r.
r is echoed back to the console. (!!!)
f is read from the pipe. r is not present.
The command line waits for a keypress, and then closes.
This very similar code sample works just fine, but only returns 1 variable:
p = io.popen(
'echo What do you want to do? > con && '..
'echo G: remove girders > con && '..
'echo F: swap foreground > con && '..
'echo B: swap background > con && '..
'echo U: undo all edits > con && '..
'echo C: cancel > con && '..
'set /p a= Choose an option: > con < con && '..
'call echo %a%', "r")
a = string.lower(p:read("*a"):gsub("\n",""))
p:close()
What am I doing wrong, and how can I rewrite this to work for my purposes?
What in the world have I unleashed, and how do I put the genie back into the bottle?
After a good while googling, I found this:
(
echo some output
echo more output
)>"Your logfile
Redirecting Output from within Batch file
I had no clue you could wrap commands like that, and I've been tinkering with the Windows CLI since I first discovered it.
local a = 'f'
local p = io.popen(
'echo -~`~-,_,- Editing '..(a == 'f' and 'foreground' or 'background')..' -~`~-,_,- > con && '.. --display text to the user
'( set /p f= Find block: && '.. --user input
'set /p r= Replace with: ) > con < con && '.. --user input
'call echo %f% &&' .. --pass f back to the lua script
'call echo %r% &&' .. --pass r back to the lua script
'pause < con > con', "r")
local f = p:read("*a") --read what was passed back, later parse it back into 2 variables
p:close()
Wrapping the two set /p statements as above works - I get the expected output of f, followed by a newline, and then r, all sent back to the lua script, where they belong.
Still, if anyone can clue me in on why this was a problem in the first place, I would be very much interested in the explanation.
local p = io.popen(
-- create temporary .bat-file
'set tmpf="%TEMP%\\TMP%RANDOM%.BAT" &&'..
'cmd /c"(echo #set p=^%1&echo #set /p x= ^%p:~1,-1^%^>con&echo #call echo ^%x^%)>%tmpf%" &&'..
-- Your main program
'echo. -~`~-,_,- Editing '..(a == 'f' and 'foreground' or 'background')..' -~`~-,_,- > con &&'.. --display text to the user
'call %tmpf% "Find block:" < con &&'.. -- pass f back to the lua script
'call %tmpf% "Replace with:" < con &&'..-- pass r back to the lua script
-- delete temporary .bat-file
'call del %tmpf% > con &&'..
'pause < con', "r")
local f = p:read'*a'
p:close()

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

Working with files in bat

I have to show the filenames using given template. I've written the following code:
if "%2" == "" (
echo "Missing second argument!"
set /p FileName="Input file name template ('*', '?' are allowed): "
set /p FileType="Input file type ('text', 'bat', 'all' only): "
if FileType == "all" (set FileType = "*")
) else (
set FileType="%2"
)
echo %DirSearch%\%FileName%.%FileType%
for %%i in (%DirSearch%\%FileName%.%FileType%) do (echo "Thats it: %%i")
If the second argument is empty, I ask user about filename template, extension (if its equal to 'all' I rewrite it's value as '*'.
Now the first trouble is that it isn't rewritten. When I put 'all' the 'FileType' is still 'all' after setting it to '*'. Why?
And echo shows up:
"C:\Folder"\test.all
"Thats it: "C:\Folder"\test.all"
How to interpretate it as single value and use in for?
New code:
if "%2" == "" (
...
if "%FileType%" == "all" (set FileType=*)
) else (
...
)
set result=%DirSearch%\%FileName%.%FileType%
echo %result%
for %%i in (%result%) do (echo "Thats it: %%i")
// echo %result%:
"C:\Data\test"\test.all
// in for cycle
"Thats it: "C:\Data\test"\test.all"
The right string should be: "C:\Data\test\test.all"
You are not testing the value of FileType in the correct manner. Also, you are not setting the new value in the correct manner. The code should read
if "%FileType%" == "all" (set FileType=*)
Otherwise, you are just comparing the strings "FileType" and "all", which of course never succeeds.
Aside: You also seem to have some error in the code that sets DirSearch; there's an extra trailing double quote there that shouldn't be.

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