How to get input from user and use it in CMD using DXL? - ibm-doors

Hello I have been trying to get user input and store it in a variable, and then use that variable in a CMD. ALL of this in a DXL script!
Take a glance into the code I have tried this but still not working.
string wheretosave = ""
DB exBox = create "Get Path"
DBE stringInn = field(exBox, "ADD Path where to save:", "", 80)
void doGet(DB exBox) {
wheretosave = get stringInn
print wheretosave "\n"
system("cmd.exe /C cd /d wheretosave & dir & PAUSE")
}
apply(exBox, "Get", doGet)
show exBox

to add "wheretosave" to a string, use
system("cmd.exe /C cd /d " wheretosave " & dir & PAUSE")
But I think the system call does not work. When I start a command line and do a command
cmd.exe /C cd /d d:\temp & dir & PAUSE
then I get the dir of the original directory.
#root: ADDITION: my problem was that I did not have a directory d:\temp on my PC...
But after reading your comment, it seems the point was not understood.
To be more precise:
First: there is no interpolation in DXL. If you want to create a string which contains a) some fixed characters, b) the content of a variable (here: wheretosave) and c) some more fixed characters, you have to use a < space > to concanate the three parts. So, do it like this: string s1 = "fixedtexta" wheretosave "fixedtextb".
Second: If the fixed characters contains a quotation mark then you have to escape it with a backslash. So, this example would become to string s2 = "fixedtexta\"" wheretosave "\"fixedtextb" to get fixedtexta"hello world"fixedtextb if wheretosavecontains hello world
BUT your example is even more difficult.
Third: cmd.exe /C takes only one parameter. So, if you want to do more than one command in the cmd subshell, you have to surround all the commands with enclosing quotation marks cmd.exe /C "cmd 1 & cmd 2 & cmd 3" The way you wrote it would have translated to
create a subshell which does a "cd /d", end the subshell
in the main shell (which is still in the original directory), do a "dir"
in the main shell, do a "pause"
Fourth: In DOS, if you have a quotation mark inside a string, you have to escape it with a second quotation mark.
All in all the command you are looking for is
string wheretosave = "d:\\temp x" // or get stringInn in your example
system("cmd.exe /C \"cd /d \"\"" wheretosave "\"\" & dir & pause\"")

Try providing the full path and apply Mike's correction. Using "C:\Windows" works as expected (for me): the cmd shows the content of my Windows folder on drive C.

Related

How to setup completion of environment variable names in zsh?

In bash when I try to use autocompletion in double-quoted strings it works:
E.g.
echo "My home directory is $HO<t>"
# expands to
echo "My home directory is $HOME"
But when I try the same thing in zsh it just does nothing.
At the same time it works outside strings:
echo My\ home\ directory\ is\ $HO<t>
# expands to
echo My\ home\ directory\ is\ $HOME
Is it possible to make it work the same as bash?
Zsh completion has a bug where completion of parameter names fails if a partial parameter name is followed by a ".
As a workaround, just remove the final " from your string before pressing Tab. Then, Zsh's completion will work correctly and it will automatically add the final " for you.

Error when trying to use variables in the post build event with delphi 10.2

I use windows 7 pro service pack 1.
I have the following code in the post build event :
SET VAR1=BLABLA
ECHO %VAR1% > Test.txt
It wont work. In the file, i get «command echo activated» (translated from french).
Embarcadero documentation says that i can use any valid dos command in those events.
If i just use :
ECHO BLABLA > Test.txt
It works, no problem. Is this a bug or there is a problem with % character ? % is an ascii char so i dont even know what could be the problem.
ty for your help.
% is used in cmd to delimit variablenames when the value of the variable is required, hence echo %var1% > test.txt will write the current value of the environment variable var1 to the file.
If var1 is not defined at the time, it will report the echo status (Echo is on/off`.
This can be circumvented by using echo(%var1% - the ( modifies echo's behaviour to not report the echo status if the arguments are resolved to nothing.
If you want to echo a literal % then you need to escape the % with another %. cmd normally uses ^ to escape symbols with a special meaning - % is the exception; %% to echo a literal %.
BTW - the space between the string to be echoed and the redirector will be output to the file. To prevent this, use > test.txt echo %var1% Note that > creates a file anew. >> will create or append if the file already exists. The space between the redirector and the filename is optional.
However, it's important when using batch to post exactly the code that's in use.
SET VAR1=BLABLA
ECHO %VAR1% > Test.txt
will work happily.
SET VAR1 = BLABLA
ECHO %VAR1% > Test.txt
will not because this latter code sets a variable named "var1Space"
On my 10.2.1 system, I've tried the code as published.
The actual code that's executed is
SET VAR1=BLABLA&ECHO %VAR1% > Test.txt
not
SET VAR1=BLABLA
ECHO %VAR1% > Test.txt
as shown in the "Build events commands" window.
This will not work because the entire line is executed as published on the "build events" page - SET VAR1=BLABLA&ECHO %VAR1% > Test.txt which will be interpreted by cmd after cmd performs its standard parsing routine.
cmd replaces any %var% with the actual value at parse time, not at run time hence as var1 has no value when the line SET VAR1=BLABLA&ECHO %VAR1% > Test.txt is parsed, the code is executed as SET VAR1=BLABLA&ECHO > Test.txt hence the problem encountered.
To cure this, you need to use
SET VAR1=BLABLA&call ECHO %%VAR1%% > Test.txt
where cmd will execute the parsed-ECHO command in a subshell. % is the escape character for % so the subshell executes ECHO %VAR1% > Test.txt after var1 has been set.
I'd suggest you raise this as a problem with EMBT. Batch commands cannot be strung together with & without side-effects. The code entered into the "Build events commands" window should be executed without reformatting - just written to a (temporary) batch file and the batch file then executed.
No doubt the eager downvoters will support the resolution of this problem.

Lua - popen command with multiple double quotes

I am writing a Lua script to manage Virtualbox on windows.
It seems that multiple double quotes are not parsed correctly. I am using the following function to implement this:
--Get output from an OS command - http://stackoverflow.com/questions/132397/get-back-the-output-of-os-execute-in-lua
function os.capture(cmd, raw)
local f = assert(io.popen(cmd, 'r'))
local s = assert(f:read('*a'))
f:close()
if raw then return s end
s = string.gsub(s, '^%s+', '')
s = string.gsub(s, '%s+$', '')
s = string.gsub(s, '[\n\r]+', ' ')
return s
end
This code works so long an the machine name doesn't have a space, but machines can have spaces so I have to support them:
local command = '"\\Program Files\\Oracle\\VirtualBox\\VBoxManage\" showvminfo '..key
The following code does not work at all but it does give the correct format of the command to the log file so the syntax should be correct:
local command = '"\\Program Files\\Oracle\\VirtualBox\\VBoxManage\" showvminfo "'..key..'"'
logger:write("[",os.date("%Y-%m-%d %H:%M:%S"),"] Command: ",command,"\n")
vmStateRaw = os.capture(command, "raw")
Log file entry:
[2014-12-06 16:09:18] Command: "\Program Files\Oracle\VirtualBox\VBoxManage" showvminfo "Cerium"
Interpreter output:
'\Program' is not recognized as an internal or external command,
operable program or batch file.
I have found that the following syntax works:
local command = '""\\Program Files\\Oracle\\VirtualBox\\VBoxManage\" showvminfo "'..key..'"'
Log file output:
[2014-12-06 16:27:54] Command: ""\Program Files\Oracle\VirtualBox\VBoxManage" showvminfo "Cerium"
So this question isn't to solve a problem as I have aleady done that. I want to understand why the last command works as my current understand means this should not work.
TIA
The issue has to do with how system in C works. Under windows, system internally calls
cmd /c yourinput
Since os.execute just delegates to system (see here), your command likely ends up executing as:
cmd /c "\Program Files\Oracle\VirtualBox\VBoxManage" showvminfo "Cerium"
For reference, from help cmd:
If /C or /K is specified, then the remainder of the command line after
the switch is processed as a command line, where the following logic is
used to process quote (") characters:
If all of the following conditions are met, then quote characters
on the command line are preserved:
no /S switch
exactly two quote characters
no special characters between the two quote characters,
where special is one of: &<>()#^|
there are one or more whitespace characters between the
two quote characters
the string between the two quote characters is the name
of an executable file.
Otherwise, old behavior is to see if the first character is
a quote character and if so, strip the leading character and
remove the last quote character on the command line, preserving
any text after the last quote character.
Since your command contains 4 double quotes in there, it parses your command with the old behavior. This is why you need to surround your entire command with an extra set of " double quotes.

I can't send some command line parameters via Autohotkey to VLC

I can't figure out how to get my audio extractor script working via commandline arguments on ahk. I know the command line argument is correct, as I'm able to get it working through a batch file, but I keep getting the error below. I think I'm probably doing something wrong syntactically but I just can't figure out what.
I'd really appreciate any help. Thanks.
Error: the following variable name contains an illegal character"
channels=2,samplerate=44100}:standard{access="file",mux=dummy,dst="%A_LoopField%.mp3"}
Code:
fileselectfile, File_Name, M3
SplitPath, File_Name, name
Loop, parse, name, `n
if a_index = 2
{
msgbox, %A_LoopField%
Run, "C:\Program Files\VideoLAN\VLC\vlc.exe" "-I dummy -v %File_Name% :sout=#transcode{vcodec=none,acodec=mp3,ab=128,channels=2,samplerate=44100}:standard{access="file",mux=dummy,dst="%A_LoopField%.mp3"}"
}
Here is the original batch code if you're curious about the audio extraction function I was talking about
#ECHO OFF
REM Loop through files (Recurse subfolders)
REM Syntax
REM FOR /R [[drive:]path] %%parameter IN (set) DO command
REM
REM Key
REM drive:path : The folder tree where the files are located.
REM
REM set : A set of one or more files. Wildcards must be used.
REM If (set) is a period character (.) then FOR will
REM loop through every folder.
REM
REM command : The command(s) to carry out, including any
REM command-line parameters.
REM
REM %%parameter : A replaceable parameter:
REM in a batch file use %%G (on the command line %G)
FOR /R %%G IN (*.mp3) DO (CALL :SUB_VLC "%%G")
FOR /R %%G IN (*.mp3.mp*) DO (CALL :SUB_RENAME "%%G")
GOTO :eof
:SUB_VLC
SET _firstbit=%1
SET _qt="
CALL SET _newnm=%%_firstbit:%_qt%=%%
SET _commanm=%_newnm:,=_COMMA_%
REM echo %_commanm%
ECHO Transcoding %1
REM Here's where the actual transcoding/conversion happens. The next line
REM fires off a command to VLC.exe with the relevant arguments:
CALL "C:\Program Files\VideoLAN\VLC\vlc" -I dummy -v %1 :sout=#transcode{vcodec=none,acodec=mp3,ab=128,channels=2,samplerate=44100}:standard{access="file",mux=dummy,dst="%_commanm%.mp3"} vlc://quit
REM Having no SLEEP-esque command, we have to trick DOS/Windows into pausing
REM for a bit between encode ops - To give the host OS a chance to do what it
REM needs to - Via clever use of the PING utility:
REM (Thanks to http://www.computing.net/answers/programming/dos-command-for-wait-5-seconds/11192.html for the tip! :-)
PING -n 1 -w 10000 1.1.1.1 > NUL
GOTO :eof
:SUB_RENAME
SET _origfnm=%1
SET _endbit=%_origfnm:*.mp3=%
CALL SET _newfilenm=%%_origfnm:.mp3%_endbit%=.mp3%%
SET _newfilenm=%_newfilenm:_COMMA_=,%
COPY %1 %_newfilenm%
GOTO :eof
:eof
REM My own little addition to prevent the batch window from "vanishing" without
REM trace at the end of execution, as if a critical error had occurred.
PAUSE
Have you tried without the SplitPath, File_Name, name? I got rid of the error like this, but I don't know if it produces the result you want in the end.
I found the answer. I was making syntatical errors I just didn't have the knowledge to fix myself. The new RUN statement works perfectly.
Here is the newly revised script
fileselectfile, File_Name, M3
SplitPath, File_Name, name, dir, ext, name_no_ext, drive
StringReplace, File_Name, File_Name,`n, \
Loop, parse, name, `n
{if a_index = 2
msgbox, %A_LoopField%
Run % "C:\Program Files\VideoLAN\VLC\vlc.exe -I dummy -v """ File_Name """ :sout=#transcode{vcodec=none,acodec=mp3,ab=128,channels=2,samplerate=44100}:standard{access=""file"",mux=dummy,dst=""" A_LoopField ".mp3""} "
}

how can I strip the filename from a path in tcsh?

Given this variable in tcsh:
set i = ~/foo/bar.c
how can I get just the directory part of $i?
~/foo
If your system provides a 'dirname' command you could:
set i = `dirname ~/foo/bar.c`
echo $i
Note the missing $ in front of the variable name. This solution is shell agnostic though.
Here is something different from above:
Available in tcsh but few other shells AFAIK
> set i = ~/foo/bar.c
> echo ${i:t}
bar.c
> echo ${i:h}
/home/erflungued/foo
The way I found to do it while waiting for answers here:
set i = ~/foo/bar.c
echo $i:h
result:
~/foo
For completely, getting the file name is accomplished with the basename command:
set j = `basename ~/foo/bar.c`
echo $j
echo $i | awk -F"/" '{$NF="";print}' OFS="/"
Use dirname command, for example:
set i = `dirname "~/foo/bar.c"`
Notice the quotation marks around path. It's important to include them. If you skip the quotation marks, dirname will fail for paths which contain spaces. Mind that ~/ expression evaluates before dirname is executed, thus even such simple example may fail if quotation marks are not used and home path includes spaces.
Of course the same problem applies also to all other commands, it's good practice to always surround argument to a command with quotation marks.
Use dirname "$i" indeed, and not ${i:h}.
The latter does not produce the intended result if $i contains only a file name (no path), while dirname correctly returns the current directory . in that case.
> set i = bar.c
> echo ${i:h}
bar.c
> dirname "$i"
.

Resources