Issue when calling the .exe file through batch script - windows

I have a problem when calling the .exe file through a batch script. Below is my batch script code:
#echo off
for /F "tokens=* delims=" %%A in (MyFile.csv) do start " "diffapicmdline.exe /lhscd "/d=localhost:9080 /h=localhost /u=user1 /p=Pwd123 %%A" /rhscd "/d=localhost:9080 /h=localhost /u=user1 /p=Pwd123 Prjct %%A" /t job /ot html /ol "C:\compare_output_H_S_Component.html""
So for each row in MyFile.csv file diffapicmdline.exe should be called every time.
But the problem is that it is not recognizing the parameters after .exe. Do we have to escape characters or find other ways to tell the batch script to run .exe successfully?
Error I am receiving here:
Windows can't find 'diffapicmdline.exe /lhscd "/d=localhost:9080'.
Make sure you typed the name correctly, and then try again.

Would this work better, calling the exe in-line instead of launching a new CMD process?
#echo off
for /F "tokens=* delims=" %%A in (MyFile.csv) do diffapicmdline.exe /lhscd "/d=localhost:9080 /h=localhost /u=user1 /p=Pwd123 %%A" /rhscd "/d=localhost:9080 /h=localhost /u=user1 /p=Pwd123 Prjct %%A" /t job /ot html /ol "C:\compare_output_H_S_Component.html"

Related

Block user from executing .bat files accidentally

We've large number of bat files in our server and they're scheduled using task scheduler. Sometimes, instead of right click to edit it, users execute them by double clicking it, triggering processes.
Can I able to change the double click properties of batch files only? Like triple click to execute, double click to do nothing.
Are there any batch commands to prompt user when they try to execute directly instead of command line?
I had similar cases and the issue solved when I added in the begin of the script TIMEOUT 10 the batch starts after 10 seconds, so if someone will accidentally start it will have time to close the window without running.
I do not recommend it but you can break the associated application for .bat files from registry changing the (Default) value from "%1" %* to nothing of the key: HKEY_CLASSES_ROOT\batfile\shell\open\command
I would definitely choose the first option.
#echo off &::in_con.bat filename and args
setlocal
set "_is.c=0"
for /f "tokens=*" %%# in ("%ComSpec%") do for /f "tokens=1-2 delims= " %%c in ('echo %CmdCmdLine%') do if /i "%%~nc"=="%%~n#" if /i "%%~d"=="/c" set "_is.c=1"
rem if running by click open in notepad
if "%_is.c%"=="1" if exist "%~1" notepad "%~1"
rem if running in console pass it throuth
endlocal & if "%_is.c%"=="0" cmd /c %*
goto :eof
always run .bat throuth cmd /c in_con.bat
Why not just rename them all to .txt and in your scheduler copy the file to a .bat|.cmd file and execute it. Best of all worlds.

Batch Script to generate multiple command-line instructions

I am writing a batch file Primary.bat which collects all the text file names from a particular directory and pipe the output to Generator.bat
The contents of Primary.bat currently:
#echo off
SETLOCAL=ENABLEDELAYEDEXPANSION
Rem Following command will write the names of all files in a text file
dir /b "C:\InputOutput\SourceFiles" > "C:\InputOutput\Generator.bat"
So, the contents of Generator.bat will be:
input1.txt
input2.txt.
input3.txt
unitedstates1.txt
unitedkingdomsales.txt
majorregion100.txt
Now I need to add code in Primary.bat so that all the above lines gets created in Generator.bat with some additional text as per below:
converter.java C:\InputOutput\SourceFiles\input1.txt C:\InputOutput\OutFiles\input1.rtg
converter.java C:\InputOutput\SourceFiles\input2.txt C:\InputOutput\OutFiles\input2.rtg
converter.java C:\InputOutput\SourceFiles\input3.txt C:\InputOutput\OutFiles\input3.rtg
converter.java C:\InputOutput\SourceFiles\unitedstates1.txt C:\InputOutput\OutFiles\unitedstates1.rtg
converter.java C:\InputOutput\SourceFiles\unitedkingdomsales.txt C:\InputOutput\OutFiles\unitedkingdomsales.rtg
converter.java C:\InputOutput\SourceFiles\majorregion100.txt C:\InputOutput\OutFiles\majorregion100.rtg
Once Primary.bat ran you should then just be able to double-click Generator.bat which will execute all of the commands.
Thanks in advance
There are still some points I don't understand about your code but as far as I understood your task, this should work:
#ECHO OFF
SETLOCAL EnableDelayedExpansion
TYPE NUL>Generator.bat
ECHO #ECHO OFF>>Generator.bat
FOR /F %%F IN ('DIR /B InputOutput\SourceFiles') DO (
ECHO START converter.java "C:\InputOutput\SourceFiles\%%F" "C:\InputOutput\OutFiles\%%~nF.rtg">>Generator.bat
)
As you only want to execute converter.java, you also can skip START. I've suggested START to be able to decide how you want to execute your application (eg. by adding /b or /p or /w or whatever as a parameter for START).
The following should work. Since converter.java is a source file, it can't be executed. I therefore assume you mean java Converter, which will work assuming you have written a class called Converter, placed it in Converter.java, and compiled it with javac Converter.java to create Converter.class.
#echo off
>C:\InputOutput\Generator.bat (for %%f in (C:\InputOutput\SourceFiles\*.txt) do (
echo java Converter "%%f" "C:\InputOutput\OutFiles\%%~nf.rtg"
))
NOTE!! This is assuming that all of the files in the Directory has an .txt extension as per your example
#echo off
if exist "C:\InputOutput\Generator.bat" move /Y "C:\InputOutput\Generator.bat" "C:\InputOutput\Generator.back"
echo #echo off > "C:\InputOutput\Generator.bat"
setlocal enabledelayedexpansion
for /F %%a in ('dir /b C:\InputOutput\OutFiles\') do (
set result=%%a
set renamed=!result:.txt=.rtg!
echo converter.java "C:\InputOutput\SourceFiles\!result!" "C:\InputOutput\OutFiles\!renamed!" >> "C:\InputOutput\Generator.bat"
)
endlocal

Why does batch file execution stop before processing the second "call" statement?

I've a fairly rudimentary knowledge of batch files and usually manage to get by but have hit a problem which I can't solve.
The batch files are run on Windows 7 Ultimate and Windows 10 Professional systems and are usually invoked by a Scheduler program, although sometimes I just click the relevant desktop icon. Essentially, the role of the batch files is to download specific files (TV programmes) which are listed in an external text file, in my case, located in my Dropbox account. For each item in the text file (TV.txt) there are two lines, one naming the file, the other listing its ID:
name1
ID1
name2
ID2
name3
ID3
The batch files successively work through the items listed in the text file, one file works on the "IDs", the second on the "names".
The "IDs" file (tv-pid.cmd) consists of the following:
set $textFile="D:\Dropbox\Get_iPlayer\0-TVdl\tv.txt"
for /f "delims=" %%a in ('type %$textFile%') do get_iplayer --pid %%a
The "names" file (tv-nopid.cmd) consists of the following:
set $textFile="D:\Dropbox\Get_iPlayer\0-TVdl\tv.txt"
for /f "delims=" %%a in ('type %$textFile%') do get_iplayer --get %%a
Each batch file works well on its own, the problem is when I try to combine the two into a single batch file.
If I create a "combined" batch file (tv.cmd):
call tv-pid.cmd
call tv-nopid.cmd
the first "call" is executed but the batch operation terminates before calling the second file.
Equally if I create a "combined" batch file (not using "call" commands)
set $textFile="D:\Dropbox\Get_iPlayer\0-TVdl\tv.txt"
for /f "delims=" %%a in ('type %$textFile%') do get_iplayer --pid %%a
set $textFile="D:\Dropbox\Get_iPlayer\0-TVdl\tv.txt"
for /f "delims=" %%a in ('type %$textFile%') do get_iplayer --get %%a
the same happens, the download activity on line 2 is executed after which the batch operation terminates.
Personally I would prefer a solution based on the "call" commands, but I don't mind.
set $textFile="D:\Dropbox\Get_iPlayer\0-TVdl\tv.txt"
set "idnames="
for /f "delims=" %%a in ('type %$textFile%') do (
if defined id_names (
set "id_names="
call get_iplayer --pid %%a
) else (
set "id_names=Y"
call get_iplayer --get %%a
)
This may work. I've no idea what get_iplayer is or does.
The idea here is that the line-content alternates, so toggling the variable id_names between set-to-a-value and set-to-no-value (=cleared) allows us to execute get_iplayer with the correct options.
Note that your code would execute get_iplayer with the option pid or get for each line of the input file - which may be causing the problem.

How to process 2 FOR loops after each other in batch?

My problem is that two FOR loops are working separately, but don't want to work one after another.
The goal is:
The first loop creates XML files and only when the creation has already been done the second loop starts and counts the size of created XML files and writes it into .txt file.
#echo off
Setlocal EnableDelayedExpansion
for /f %%a in ('dir /b /s C:\Users\NekhayenkoO\test\') do (
echo Verarbeite %%~na
jhove -m PDF-hul -h xml -o C:\Users\NekhayenkoO\outputxml\%%~na.xml %%a
)
for /f %%i in ('dir /b /s C:\Users\NekhayenkoO\outputxml\') do (
echo %%~ni %%~zi >> C:\Users\NekhayenkoO\outputxml\size.txt
)
pause
This question can be answered easily when knowing what jhove is.
So I searched in world wide web for jhove, found very quickly the homepage JHOVE | JSTOR/Harvard Object Validation Environment and downloaded also jhove-1_11.zip from SourceForge project page of JHOVE.
All this was done by me to find out that jhove is a Java application which is executed on Linux and perhaps also on Mac using the shell script jhove and on Windows the batch file jhove.bat for making it easier to use by users.
So Windows command interpreter searches in current directory and next in all directories specified in environment variable PATH for a file matching the file name pattern jhove.* having a file extension listed in environment variable PATHEXT because jhove.bat is specified without file extension and without path in the batch file.
But the execution of a batch file from within a batch file without usage of command CALL results in script execution of current batch file being continued in the other executed batch file without ever returning back to the current batch file.
For that reason Windows command interpreter runs into jhove.bat on first file found in directory C:\Users\NekhayenkoO\test and never comes back.
This behavior can be easily watched by using two simple batch files stored for example in C:\Temp.
Test1.bat:
#echo off
cd /D "%~dp0"
for %%I in (*.bat) do Test2.bat "%%I"
echo %~n0: Leaving %~f0
Test2.bat:
#echo %~n0: Arguments are: %*
#echo %~n0: Leaving %~f0
On running from within a command prompt window C:\Temp\Test1.bat the output is:
Test2: Arguments are: "Test1.bat"
Test2: Leaving C:\Temp\Test2.bat
The processing of Test1.bat was continued on Test2.bat without coming back to Test1.bat.
Now Test1.bat is modified to by inserting command CALL after do.
Test1.bat:
#echo off
cd /D "%~dp0"
for %%I in (*.bat) do call Test2.bat "%%I"
echo Leaving %~f0
The output on running Test1.bat from within command prompt window is now:
Test2: Arguments are: "Test1.bat"
Test2: Leaving C:\Temp\Test2.bat
Test2: Arguments are: "Test2.bat"
Test2: Leaving C:\Temp\Test2.bat
Test1: Leaving C:\Temp\Test1.bat
Batch file Test1.bat calls now batch file Test2.bat and therefore the FOR loop is really executed on all *.bat files found in directory of the two batch files.
Therefore the solution is using command CALL as suggested already by Squashman:
#echo off
setlocal EnableDelayedExpansion
for /f %%a in ('dir /b /s "%USERPROFILE%\test\" 2^>nul') do (
echo Verarbeite %%~na
call jhove.bat -m PDF-hul -h xml -o "%USERPROFILE%\outputxml\%%~na.xml" "%%a"
)
for /f %%i in ('dir /b /s "%USERPROFILE%\outputxml\" 2^>nul') do (
echo %%~ni %%~zi>>"%USERPROFILE%\outputxml\size.txt"
)
pause
endlocal
A reference to environment variable USERPROFILE is used instead of C:\Users\NekhayenkoO.
All file names are enclosed in double quotes in case of any file found in the directory contains a space character or any other special character which requires enclosing in double quotes.
And last 2>nul is added which redirects the error message output to handle STDERR by command DIR on not finding any file to device NUL to suppress it. The redirection operator > must be escaped here with ^ to be interpreted on execution of command DIR and not as wrong placed redirection operator on parsing already the command FOR.
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 /?
cd /?
dir /?
echo /?
for /?
And read also the Microsoft article Using command redirection operators.
You need to use the START command with the /WAIT flag when you launch an external application.
I believe it would look something like this:
START /WAIT jhove -m PDF-hul -h xml -o C:\Users\NekhayenkoO\outputxml\%%~na.xml %%a
That should cause the batch file to pause and wait for the external application to finish before proceeding.

for statement will not run in batch script [duplicate]

This question already has an answer here:
for loop working in CMD prompt but not in batch file - for loop was copy pasted
(1 answer)
Closed 3 years ago.
I'm kinda a bit of a noob to this stuff. I'm trying to make a batch script to convert Backup Exec 10 xml log files to text.
I got some great answers from here when searching, cooked up a script and have tried to run it. It bombs at the first "for" statement and I'm not sure why.
I can run every step of this script manually and it works great. But if I double click on the batch file and run it, after the second pause it bombs.
Anyone see anything out of place here? I'm at my wits end. I think the "for" statement might need tweaking. I've been messing around but haven't found the right combo for it to run successfully in the script.
#echo off
echo Starting Backup To Text SCript...
pause
cd c:\Program Files\Symantec\Backup Exec\Data
echo Get the latest BEX_TAPEBACKUP File...
pause
FOR /F "usebackq" %f in (`dir /Od /B`) do set "FILE=%f"
echo %FILE% will be converted to text
pause
cd c:\Program Files\Symantec\Backup Exec
REM This command will take the current Backup Exec XML log file and convert it to text.
pause
bemcmd -o31 -l"C:\Users\Administrator\Desktop\Backup Logs\backuplog.txt" s0 - f"C:\Program Files\Symantec\Backup Exec\Data\%FILE%"
pause
echo Done
Use the right syntax for your for loop:
FOR /F "delims=" %%f in ('dir /od /B') do set "FILE=%%~f"
You need to double your %'s in the For.
FOR /F "usebackq" %%f in (`dir /Od /B`) do set "FILE=%%f"

Resources