What is the correct batch file syntax? - windows

What is the correct batch file syntax to run command-line application with arguments on windows 7?
C:\KindleGen\kindlegen.exe Htmlpage.html -c2
The bat file is in one folder with page that should be to processed.

Windows uses %1, %2, etc for the argument substitution.
Batch file test.bat contains:
c:\KindleGen\kindlegen.exe %1 -c2
Assuming the -c2 should always be applied
Call it with:
test somefile.html
If you need to run from the GUI, you can drag the .bat file to your desktop and double click it.
If the file to be processed is always the same, then you don't need the command line args, just put the complete command line in the bat file:
c:\KindleGen\kindlegen.exe Htmlpage.html -c2
If you need to get the user's input for a file name, you could have the .bat ask for it like this:
echo off
set /p fileName=Enter file name:
c:\KindleGen\kindlegen.exe fileName -c2
set /p done=Finished. Press enter...
When you click on that it will open a command window and wait for input, run the command, then wait for enter before closing the command window. Take that last line out if you want it to just close when done.
If you need a script that runs the command for all .html files in the current folder use:
echo off
for %%c in (*.html) do c:\KindleGen\kindlegen.exe %%c -c2

Try:
start "" "C:\KindleGen\kindlegen.exe" "Htmlpage.html" -c2

Related

Running a batch file which requires inputs directly from folder

I have a batch file which requires 4 command line inputs. When I execute the batch file on the command prompt, it displays help message asking to input 4 values.
When I run this file directly from the folder, it opens cmd and closes immediately.
Is it possible to modify the batch file, so that when I run from folder it will open the cmd and then display the help message.?
Following is a mini version of my problem with 1 command input. The script is for a License file generation
#ECHO OFF
GOTO :continue
:continue
SETLOCAL
IF "%1" == "" GOTO :Help
::Set the Command Line Options
SET ARVERSION=%1
::Create Directory
SET OUT_PATH=%cd%
ECHO Initiating Generation...
if not exist %OUT_PATH% mkdir %OUT_PATH%
::Create License File - Calling 'Subs' will create the output with actual Version
Subs ARVERSION %ARVERSION% Input.txt 1>%OUT_PATH%\License.txt
ECHO Scripts are created # %OUT_PATH%
ECHO Generation Completed...
GOTO :End
:Help
ECHO Starting License File Generation...
ECHO Usage:
ECHO InstallerScriptGen.bat AR_VERSION
ECHO AR_VERSION - Version (3.2 or 4.0 or 4.2)
ECHO Example : InstallerScriptGen.bat 3.2.2
ECHO Please Note that input of incorrect values will result in wrong generation.
:End
ENDLOCAL
"Running directly from the folder" (by which I assume you mean "clicking on the icon from within Windows Explorer") causes Windows Explorer to execute the equivalent of CMD /C <<batchfilename>>. When invoked with /C, CMD exits (and the CMD window closes) as soon as the batch file ends. You can force the window to stay open long enough to read the output by ending the script with either the PAUSE command (which will cause it to wait for the user to press any key), or the TIMEOUT command (which will wait the indicated number of seconds before continuing, without a keypress). See SS64's help for the PAUSE and TIMEOUT commands for more information.

How to call a batch file with a flag from within a batch file

I have a batch script that needs to call other batch scripts that need a flag to run successfully.
:adobe_install
echo 1. Adobe Reader & echo 2. Adobe Flash Player & echo 3. Adobe Acrobat
set /p choice="What would you like to install[1-3]: "
if %choice%==1 call "%adobe_path%\adobe.bat"
rem if %choice%==2 call "%cd%\scripts/Adobe/adobe.bat /f"
rem if %choice%==3 call "%cd%\scripts/Adobe/adobe.bat /a"
The adobe batch script needs a flag to run successfully, one of the following flags must be given: /a,, /r, /f
How can I run a batch script and a flag within a batch script?
What I've tried:
if %choice%==1 call "%adobe_path%\adobe.bat"
# this works but only outputs the help menu of the script
if %choice%==1 call "%adobe_path%\adobe.bat /r"
# this will output "the syntax of the command is incorrect"
cd "%adobe_path%\scripts\Adobe"
if %choice%==1 call "\.adobe /r"
# same as above
if %choice%==1 start "%adobe_path%\adobe /r"
# launches another cmd and won't run the script
if %choice%==1 call "%adobe_path%\adobe.bat" /r
The executable (a batch file) is quoted to allow spaces in the name. The parameters are separated by spaces (or commas)

Handle Windows command prompt input from keyboard and display output and also redirecting it to a file?

Though I can find few relevant questions such as Displaying Windows command prompt output and redirecting it to a file and How do I echo and send console output to a file in a bat script?, I could not fix my problem. I am working on Windows XP and running a few scripts using a batch file which uses STDIN and STDOUT handles.
Basically, I want to save the log file of command prompt. This is possible with "echo >> log.txt" in a batch file. However, this is not saving the outputs generated as a result of the script file. So I tried at the cmd prompt itself as D:>file.bat >"dir_path/log.txt". Again, this command saves the STDOUT to log.txt at the specified location. Since I need to get a few user inputs i.e. to use the STDIN handle, how do I achieve this in addition to saving the outputs of command prompts in separate file too?
The answer is simple:
#echo off
set /p answ1=Get user input1
set /p answ2=Get User input2
echo %answ1% and %answ2%>>log.txt
notepad log.txt
set /p "var=<log.txt"
type log.txt
Even if you redirect your .bat file to a file, you can output to the screen with >con.
Execute the following script with test.bat >test.out. It will prompt you for an input on the screen.
timeout -t 2
>con set /p "var1=Prompt1: "
ipconfig
>con set /p "var2=Prompt2: "
echo %var1% %var2%
>con echo %var1% %var2%
exit /b

How to make Windows START command accept standard input via pipe and pass it on to the command it invokes?

See this script saved in a file called foo.cmd.
#echo off
more +5
This script may now be used in this manner.
dir C:\Windows | foo
It displays the output beginning from the 6th line, one screen at a time (i.e. as a pager). The current command prompt remains blocked until I quit more.
Now I modify the script as follows, so that the more output is displayed in a separate window.
#echo off
start "" more +5
Now if I run the following command, a new window is launched fine, but no output is displayed in it.
dir C:\Windows | foo
It appears that the output of the dir command that I have piped into foo.cmd is not being received by the start command.
What can I do to ensure that any data piped into the standard input of the start command is passed on to the program being invoked by start (which is more in this case)?
I don't think it is possible to pipe into another window. Your START command within your parent window is receiving the pipe input. The START command ignores the input. You want the MORE command in the new window to receive the input.
I believe the only way to achieve your goal is to use a temporary file:
#echo off
set "file=%temp%\%~nx0.temp"
findstr "^" >"%file%"
start "" cmd /c more +5 ^<"%file%" ^& del "%file%" ^& pause

How to redirect output to a file in a batch file?

In Windows 7, I have a batch file that runs an exe in the sub-directory of bat file, it first changes current directory to that folder, then runs the exe. But something goes wrong, I see a console window for a very short time and the program doesn't start. Since the output console is displayed for less than a second, I can't see the error message.
bat file is:
cd /d "%~dp0my_subfolder"
start "" myapplication.exe
How do I redirect the output error message to a text file (the text file will be in the same directory with bat file), so that I can read the error message? What command should I add to the bat file above?
use:
start "" myapplication.exe > mytextfile.txt 2>&1
instead you could run batch file from cmd to know what is the error
Try this, to see the error message:
cd /d "%~dp0my_subfolder"
start "" /b myapplication.exe
pause

Resources