Passing parameter to windows command script - batch-file

I need to pass argument from wsf file to bat file to windows command script.
In wsf file I have:
Shell.Run("Something.bat ",&varparam,1,true)
In Something.bat:
sftp.exe testcommand.cmd %1
In testcommand.cmd:
open user#address
put %1
But .cmd file does not get access to the parameter value. How can I get this to work?

In order to pass values into a batch you can use call
Try this:
CALL Something.bat %varparam%
And I think part of your problem is that you are trying to pass values into a command file that is already part of a separate string.
You could get this to work by haveing your Something.bat create your testcommand file.
Something.bat:
echo open user#address testcommand.bat
echo put %1 >> testcommand.bat
sftp.exe -b testcommand.bat
It's not perfect but I'm pretty sure the syntax of
sftp.exe testcommand.cmd %variable%
is your problem.

Related

Calling command with variable through batch file

I have a batch file that copies and moves stuff, but I am getting stuck dealing with Certificates. I have to use a command the vendor provides called installrootcacert.cmd, but I also need to pass the file name of the cert which is aptly named rootca.cer. I have to use the script the vendor provides so there is no way around that.
Normally I would run this from the command like like so:
c:\vendor\Software\Conf\Security\installrootcacert.cmd rootca.cer
I have attempted to call the command from my batch file, but with no luck.
I tried to use a variable, but because that command calls several other processes, it is looking for "rootca.cer" after the command. If I place it in a variable, the other processes fail. I cannot modify the other processes.
echo #off
cd E:\vendor\Software\Conf\Security\trustedCA
e:
call "e:\vendor\Software\Conf\Security\installrootcacert.cmd rootCA.cer"
A possible solution, might be:
#echo off
cd /d "E:\vendor\Software\Conf\Security\trustedCA"
call "E:\vendor\Software\Conf\Security\installrootcacert.cmd" rootCA.cer
echo #off replaced with #echo off because echo #off will echo #off in cmd.
It is mentioned by Compo in comments, you are changing drives: use /d option with cd.
Finally, quote only the filename in the call command, else, windows will interpret this as a complete filename (i.e. E:\vendor\Software\Conf\Security\installrootcacert.cmd rootCA.cer, so filename will be installrootcacert.cmd rootCA.cer).
Try the following. You need to adjust the # in your echo statement and your quotations:
#echo off
cd E:\vendor\Software\Conf\Security\trustedCA
e:
call "e:\vendor\Software\Conf\Security\installrootcacert.cmd" rootCA.cer
The reason that this works is because putting the command #echo off in your script stops ALL commands from being output. If you just have echo #off, you're literally going to echo that. (credit to double-beep for initially suggesting that)
As for the quotes, what you are trying to do is pass that as a command, so when you call the rootCA.cer, you need to make sure you are passing it the proper parameters, which is why you place that filepath in quotes. If you place the WHOLE object in quotes, you aren't actually issuing a call to the rootCA.cer command. (credit to LotPings for initially suggesting that).

How to pass commands in a text file to an EXE running through the command line with batch?

I have a text file with lots of commands in it and I want to sent those commands to a software called thermocal. It is a console application. I found the command below, but it doesn't work for me.
Do I need to put this .exe file in the same folder of the batch file to make it work or any thing else?
type somefile.txt | Thermocal.exe
Batch scripts can be considered as a collection of lines you could also type in a command line prompt one after another. With respect to this it might be helpful for you, to play with cmd in order to get a feeling for what is happening.
About starting thermocal: Assuming thermocal is not part of PATH then the batch file either needs to change the current directory to the one with termocal.exe. Alternatively you might be able to call thermocal.exe with adding a path like C:\ProgramFiles\Thermocal\thermocal.exe . Play with cmd to find out, what works and what doesn't
When you are able to start thermocal from the command line prompt window, you can start experimenting with the call. You will probably end of with something like this in your command line window:
C:\ProgramFiles\Thermocal> thermocal argument1 argument 2
If this works, you can start with batch programming :)
Assuming your arguments are stored in somefile.txt like this:
argument1 argument 2
TYPE does nothing more than printing a file:
TYPE somefile.txt
Now you need to use the result of the output as command line arguments:
for /f %%i in ('type somefile.txt') do (thermocal.exe %%i)

create a Batch file with a file path as an input argument

I need to create a batch file which will run a program (which has been created in C#.Net) and also take a path of a text file as an input.
Not quite sure, how to achieve this.
So far, I have the below command working,
C:\>Folder Path to executable>xxxx.exe -console
-console is my predefined command argument to run this program in console mode.
The part until running the program from the console, with -console, works perfectly fine with a hard coded file path. However, I want to give the functionality to the user to give the file path as they want and create a batch file for the same command. Everytime user can update the batch file with new text file path and simply run it.
Thanks,
Are you just wanting to use the -console parameter? Are there any other parameters you wanted to pass in?
If wanting just what you have in your snippet, Save the following into a batch file. (e.g. StartMyProgram.bat)
start "C:\Folder path to executable\xxxx.exe" -console
See start /? for help and more options.
You need to add %~1 to your script: C:\Path\to\executable\xxxx.exe -console %~1
Now you can call it like this: StartMyApp.cmd C:\Docs\readme.txt
%~1 contains the full path of the text-file. You could also ensure that only text-files are passed to your application:
if "%~x1"==".txt" (
C:\Path\to\executable\xxxx.exe -console %~1
) else (
echo Not a textfile! & pause
)

Command Alias within a Batch Program

Hello,
I don't know if anybody can help me, because I've search a lot without success.
On my DOS Terminal (XP) I send successfully those commands :
doskey CS=call smile.bat $*
CS
or
CS arg1
It works PERFECTLY !
Then I insert these commands in a new batch file :
#echo off
doskey CS=call smile.bat $*
CS arg1
Error returned : CS is an unknown command...
I do absolutely want to use aliases in my batch program, but I do not want to modify my Path nor my Reg, because it is just to use in the Batch itself. I want to find a solution, but I need any idea. Please if some one can help me with an example, it would be very useful.
EDIT
You cannot run a doskey macro from a batch program.
If you just want to call a Batch file via another name, then just define another Batch file (with the second name) that call the first one. For example
CS.bat file may have this line:
#call smile.bat %*
then, in the first Batch file:
call CS
or
call CS arg1
If this is enough for you, then you may even create the alias Batch file inside the first Batch file this way:
echo #call smile.bat %%*> CS.bat
I hope it helps...

Read command-line parameters to .bat from file

I have a build.bat file which uses %1 internally... so you might call:
build 1.23
I wanted it to read the parameter from a separate file, so I tried putting "1.23" in version.txt and doing:
build < version.txt
But it doesn't work. Isn't this how piping works? Is what I want possible and if so how?
The FOR command in DOS has a form which parses files and assigns the tokens it finds to variables, which can then be used as arguments to other batch files. Assuming version.txt contains the single line "1.23", this batch file will open it, assign 1.23 to the variable, then call your original batch file, passing the variable's value as a command-line argument.
#echo off
for /f %%i in (version.txt) do call build.bat %%i
If version.txt contains more than one line, build.bat is called once for each line. Type help for at a DOS prompt to see what other features you might be able to use.
I think it would make more sense if you handle the file processing within the batch file on the off chance that version.txt is not edited correctly.
You should modify your script to parse the file to get the version if the .bat file is executed as:
build FILE version.txt

Resources