I have a executable "myfile.exe" file in location "C:\Users\me\Desktop" I want to execute a command "myfile.exe <firstArg> <secondArg>" at C:\Users\me\Desktop through batch file,
In command line:
C:\Users\me\Desktop>myfile.exe <firstArg> <secondArg>
How to write the batch script for the above command in windows
With argument variables the contents of you batchFile.bat would be:
#echo off
C:\Users\me\Desktop\myfile.exe %1 %2
or with fixed arguments:
#echo off
C:\Users\me\Desktop\myfile.exe <firstArg> <secondArg>
Just add the exact command you are executing from command line to .bat file.
Change working directory first if necessary.
cd C:\Users\me\Desktop
myfile.exe <firstArg> <secondArg>
Your batch file:
C:\Users\me\Desktop\myfile.exe %1 %2
Then you can call your batch file:
mybatchfile.bat <firstArg> <secondArg>
Also, you can use %* to pass all parameters rather than specifying each parameter.
Related
How to can I to write this linux-like loop in the Windows 7 command line?
for docker_path in `ls | grep "docker$"`
do
cd $docker_path
mvn -B -f pom.xml clean deploy -Pdocker
cd ..
done
I need found all *docker/ directory, exec there mvn-command and return into patern directory, but for Windows7 system.
On Windows command line use:
for /D %I in (*docker) do #pushd "%I" & mvn.exe -B -f pom.xml clean deploy -Pdocker & popd
Use the following command line if mvn is not an executable, but a *.bat or *.cmd file:
for /D %I in (*docker) do #pushd "%I" & call mvn.bat -B -f pom.xml clean deploy -Pdocker & popd
Well, on command prompt it is not really necessary to use command CALL to run a batch file in a loop together with two other commands as done here.
The first command line for usage in a batch file:
#for /D %%I in (*docker) do #pushd "%%I" & mvn.exe -B -f pom.xml clean deploy -Pdocker & popd
In a batch file the loop variable I must be referenced with two percent signs instead of just one as on command prompt. The loop variable can be only a single character.
The second command line for usage in a batch file:
for /D %%I in (*docker) do #pushd "%%I" & call mvn.bat -B -f pom.xml clean deploy -Pdocker & popd
In a batch file it is necessary to use command CALL to call another batch file and continue with next command respectively command line in current batch file after execution of the other batch file finished.
Windows command processor cmd.exe continues execution of current batch file on the other batch file on not using command CALL and exits batch file processing once the execution of other batch file finished without further processing command lines in current batch file.
See answer on How to call a batch file that is one level up from the current directory? for details on the existing methods to run a batch file from within a batch file.
The single command line can be also coded with multiple lines:
#echo off
for /D %%I in (*docker) do (
pushd "%%I"
mvn.exe -B -f pom.xml clean deploy -Pdocker
popd
)
And the same multi-line batch file solution in case of mvn is a batch file.
#echo off
for /D %%I in (*docker) do (
pushd "%%I"
call mvn.bat -B -f pom.xml clean deploy -Pdocker
popd
)
Command FOR with option /D searches with wildcard pattern *docker for non hidden directories in current directory of which directory name ends with the string docker.
It is advisable on Windows to reference a file to execute with complete name of file, i.e. file name + file extension. This makes it clear for Windows command processor as well as every reader of the code if the executed file is an executable or a script file which makes a difference as it can be seen here.
For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.
call /?
echo /?
for /?
popd /?
pushd /?
See also:
Single line with multiple commands using Windows batch file
Microsoft's command-line reference
SS64.com - A-Z index of the Windows CMD command line
I've a batch file that is opening a FTP connection to a server and putting a file on a specified location.
Here is how my ftpConnection.bat file look like..
open HOST
FTP_USER_NAME
FTP_USER_PASSWD
cd TO_DIR
lcd TO_FILE_LOCATION
put THE_FILE
quit
and from command prompt if i run it like this ftp -i -s:ftpConnection.bat it works fine.
My requirement is to pass HOST, USER_NAME and PASSWORD as argument
so i tried to use %1 %2 and %3 but this is not working for me.
Passing the argument like this
C:\Users\xxx\Desktop>ftp -i -s:ftpConnection.bat "HOST" "USER_NAME" "PASSWORD"
also tried without the quotes but result is same, it'S showing
Transfers files to and from a computer running an FTP server service
(sometimes called a daemon). Ftp can be used interactively.
FTP [-v] [-d] [-i] [-n] [-g] [-s:filename] [-a] [-A] [-x:sendbuffer] [-r:recvbuf
fer] [-b:asyncbuffers] [-w:windowsize] [host]
Followed and tried few document like How to pass multiple parameters in CMD to batch file and Batch file command line arguments
they suggested to use set and i tried it like below but result is same.
set host=%1
set uname=%2
set passwd=%3
open %1
%2
%3
Can anyone suggest me what i am doing wrong or any pointer to achieve this.
Thanks in advance.
Sorry, but you are using a text file with input and commands expected and interpreted by ftp command. This is not a windows batch file and can not use its syntax.
To use the required data as arguments in the call, you need a batch file that will generate the ftp script from the passed arguments
#echo off
setlocal enableextensions disabledelayedexpansion
if "%~3"=="" goto :eof
set "TO_DIR=......"
set "TO_FILE_LOCATION=......"
set "THE_FILE=........"
> ftpScript (
echo open %~1
echo %~2
echo %~3
echo cd %TO_DIR%
echo lcd %TO_FILE_LOCATION%
echo put %THE_FILE%
echo quit
)
ftp -i -s:ftpScript
Now, you have a batch file (let's call it doFTP.cmd), that you can call as
doFTP.cmd ftpserver userName pa$$word
The arguments in the command line are stored in the %1, %2 and %3 variables (%~1 is first argument without surrounding quotes if present) and used inside the batch file to generate (the block of echo commands are redirected to the ftpScript file) the text file that ftp will handle.
based on your use it is like
set host=%1
set uname=%2
set passwd=%3
open %host%
%username%
%passwd%
you can use the set variable back by enclosing between two %var-name% as i shown above
please give it a try
I can't find any way to do, for example, the following:
cmd.exe /C "script.txt"
In other words, I need Command Prompt to (try) to execute file with any extension (not necessarily .bat or .cmd) if it contains valid batch script code. I'm looking for behavior similar to Unix shells:
./script.txt
While on Unix the shebang (#!/bin/sh) is responsible for understanding that the file is actually a script, it seems like on Windows .bat or .cmd extensions play the same role, indicating a batch script file for Command Prompt.
Is it possible to avoid that and force Command Prompt to interpret a file with any name?
NOTE: Please, no answers like:
Give your file .bat or .cmd extension.
That's not what the question is about.
This depends on the complexity of the NON-Batch file. If the NON-Batch file does not use these facilities:
Access to Batch file parameters via %1 %2 ... and execution of SHIFT command.
Execution of GOTO command.
Execution of CALL :NAME command (internal subroutine).
Execution of SETLOCAL/ENDLOCAL commands.
then you may execute any file as a "Batch file" via this trick:
cmd < anyFile.ext
Further details at this post
you first need an "installation" script :
#echo off
rem :: A files with .TEST extension will be able to execute batch code but is not perfect as the %0 argument is lost
rem :: "installing" a caller.
if not exist "c:\caller.bat" (
echo #echo off
echo copy "%%~nx1" "%%temp%%\%%~nx1.bat" /Y ^>nul
echo "%%temp%%\%%~nx1.bat" %%*
) > c:\caller.bat
rem :: associating file extension
assoc .test=batps
ftype batps=c:\caller "%%1" %*
then try a simple .test file:
#echo off
for /l (1;1;10) do (
echo testing .TEST extension
)
In fact ASSOC and FTYPE both have immediate effect so you can start a .test file right after "installation". With direct editing of the registry eventually you can get more control -> How to create file extension that behaves as .cmd/.bat? . Check also drop handlers -> http://msdn.microsoft.com/en-us/library/windows/desktop/cc144165%28v=vs.85%29.aspx
Trying to print out the 8dot3 name of a file, it does works when I paste the line in the Command Prompt but not when I run it from a batch file.
Result when pasted :
D:\tmp>cmd /e:on /c for %A in (8088_othello.com) do #echo %~nxsA
8088_O~1.COM
Result from batch file :
D:\tmp>lfn.bat
D:\tmp>cmd /e:on /c for ~nxsA
~nxsA was unexpected at this time.
What else is needed to make it work inside a batch file ?
you need to escape % is batch files
just type
cmd /e:on /c for %%A in (8088_othello.com) do #echo %%~nxsA
I have the following code which doesn't seem to be working properly - is someone able to assist with how to run command-lines in batch files
#echo off
set changeFrom=321
set changeTo=123
set origFile=config.txt
set newFile=config1.txt
test.bat %changeFrom% %changeTo% %origFile%>%newFile%
del %origFile%
ren %newFile% %origFile%
::end
I have a file "test.bat" which has code to replace strings in a file - but I Don't get how it can work ?
You need to use call to execute the second bat file from the first like this:
call test.bat %changeFrom% %changeTo% %origFile%>%newFile%
without call the first batch script will exit when the second one exits.