perl call not piping parameters to executable - perl

I am calling a cmd like:
perl ScriptGen.pl Target.c %PreProcessorPath%\preproc.exe Arg1 Arg2 > clean.c
And I get errors which mean Arg1 and Arg2 are not correctly passed to the pre processor.
I would like to call this perl script like this by having the target file, and preprocessor path itself as arguments to the perl script, and Arg1 Arg2 are arguments to the perl script which the preproc understands.
EDIT: There is a
my $cmd = shift #ARGV
at the begginning of the script to parse the arguments, and then the script works with that.

This is my Suggestion:
You can use the batchfile instead of using in a single line several parameters with applications;
Create one batchfile - scriptgen.bat
SETPATH=%PATH%;%PreProcessorPath%\preproc.exe
call preproc.exe %1 %2
rem %1 = Arg1 %2 = Arg1 %3 = Target.c
perl -w ScriptGen.pl %3 %1 %2 > clean.c
In MS-dos Prompt run:
scriptgen.bat Arg1 Arg2 Arg3

Related

How can I set a file as an input variable in a bat file?

I'm wanting a bat file to drag'n drop files and open them with a program. The bat file goes as follows normally:
#echo off
C:\path\to\program\program.exe -variables "C:\path\to\file\randomfile.*"
Now, I want to keep the same structure but I want to set a variable to replace "randomfile.*", that variable being the file I drag'n drop, but I don't know how to set it up.
How do I set a variable to replace randomfile.* when I drag and drop a file?
When you drag and drop a file onto a batch file the name of that file is passed as a parameter %1 to the batch file.
To do this edit your batch file as follows:
#echo off
C:\path\to\program\program.exe -variables "%1"
Notes:
You can get the value of any argument using a % followed by it's numerical position on the command line. The first item passed is always %1, the second item is always %2 and so on.
%* in a batch script refers to all the arguments (e.g. %1 %2 %3 %4 %5 ... %255)
Only arguments %1 to %9 can be referenced by number.
Further Reading
An A-Z Index of the Windows CMD command line - An excellent reference for all things Windows cmd line related.
parameters - A command line argument (or parameter) is any value passed into a batch script.

Start process, wait for it, and throw away all output

I'd like to do this in PowerShell:
myprogram.exe arg1 arg2 >nul 2>nul
How does it work? There are like a million options to start processes in PowerShell, it's impossible to know the right one you need at a time.
nul is a special device (see this answer on SuperUser for instance) that's available in CMD, but not in PowerShell. In PowerShell you can use $null instead. Starting with PowerShell v3 you can use *> to redirect all output streams, but since you want to redirect output from an external program there should only be output on STDOUT and STDERR (Success and Error output stream in PowerShell terms), so >$null 2>$null should be fine.
A notable difference between CMD and PowerShell is that PowerShell doesn't include the current working directory in the PATH (the list of directories that is searched when you call a program/script without a path). If you want to run myprogram.exe from the current directory you need to prepend it with the path to the current directory (./).
You may also want to use the call operator (&). Although it's not required in this particular case I consider using it good practice. If you specify the command as a string (for instance because the path or filename contains spaces, or you want to use a variable instead of a literal) you MUST use the call operator, otherwise the statement would throw an error.
Something like this
& ./myprogram.exe arg1 arg2 >$null 2>$null
or like this
& ./myprogram.exe arg1 arg2 *>$null
should work.

Comma separate input parameters?

I have a script foo.cmd:
echo %1 %2
In PowerShell I run:
foo.cmd "a,b" "c"
Expected output: a,b c
Actual output: a b
Why?
The double quotes are removed after PowerShell parsed the command line and passes it to CMD for execution, so CMD actually sees a statement
foo.cmd a,b c
Since the comma is one of CMD's delimiter characters that statement is equivalent to
foo.cmd a b c
To avoid this behavior you need to ensure that the double quotes are preserved when passing the arguments to CMD. There are several ways to achieve this. For instance, you could put the double quoted arguments in single qoutes:
foo.cmd '"a,b"' "c"
and change the positional parameters in the batch script to %~1, %~2 so that CMD removes the double quotes from the arguments.
If you have PowerShell v3 or newer you can use the "magic parameter" --% to avoid the nested quotes:
foo.cmd --% "a,b" "c"
You still need %~1 and %~2 in the batch script, though.

Wrapping Applications with a .bat Script

I recently decided to start using Google's command-line JavaScript compiler (Closure Compiler) to minify JavaScript in my web application. Unfortunately, the compiler is written in Java so the command I have to type to use the compiler is somewhat cumbersome:
java -jar compiler.jar --js hello.js --js_output_file hello-compiled.js
What would be the simplest way for me to wrap this in .bat file that would allow me to simply type closure hello.js hello-compiled.js, while still allowing me to use closure's other command line flags such as --warning-level and --help?
:Start
#echo off
if "%1"=="" Echo Error - input and output parameters were not specified.&goto End
if "%2"=="" Echo Error - output parameter was not specified.&goto End
if not exist %1 Echo Error - input file does not exist.&goto End
set infile=%1
set outfile=%2
set "params="
:Loop
if "%3"=="" goto Continue
set params=%params% %3
shift
goto Loop
:Continue
java -jar compiler.jar --js %infile% --js_output_file %outfile% %params%
set "infile="
set "outfile="
set "params="
:End
I don't know how automated you want this to be, but this will prompt you for your script, your compiled output file, and if you want any extra parameters.
Easily editable if you want more options.
#echo off
set /p script=Script file:
set /p compiled=Compiled script:
set /p params=Enter any extra params (leave blank if none):
if defined params (
java -jar compiler.jar --js %script% --js_output_file %compiled% %params%
) else (
java -jar compiler.jar --js %script% --js_output_file %compiled%
)
echo Done
pause >nul

Using command line arguments in VBscript

How can I pass and access command line arguments in VBscript?
Set args = Wscript.Arguments
For Each arg In args
Wscript.Echo arg
Next
From a command prompt, run the script like this:
CSCRIPT MyScript.vbs 1 2 A B "Arg with spaces"
Will give results like this:
1
2
A
B
Arg with spaces
If you need direct access:
WScript.Arguments.Item(0)
WScript.Arguments.Item(1)
...