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

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
)

Related

Running ant commands using batch file

Currently this is how I have written the code in the batch file:
C:\ cd C:\abc\xyz\build-scripts-master
call setEnv.cmd
cmd ant do-clean
cmd ant do-dist
This is not working. It just executes the setEnv and breaks out. It does not run the remaining commands
Manually this is how it works:
I first go to the folder C:\abc\xyz\build-scripts-master through the Command Prompt
Then I type in setEnv, which is a windows command script, and hit return.
Then I type in ant do-clean
And then ant do-dist
I want to automate this process and hence was trying to achieve this using batch file.
Try the following:
#CD /D "C:\abc\xyz\build-scripts-master"
#Call setEnv.cmd
#Call ant.bat do-clean
#Call ant.bat do-dist
The latter two lines assume that ant.bat is located somewhere in the current working directory or %PATH%
It is not imperative that the directory path is doublequoted in this case, just good practice.You could continue not to use the .bat extension with ant. I've included it just to make it clear that it is a batch file, and should be Called in the same way as the setEnv batch file.
it didn't run the bat files because you didn't specify the files' location in the code. As it stands right now, the script expects the .bats to exist in the working directory or it has been placed in the folder. The only way it will run the files arbitrarily is if you had placed your working location in the system variables or set the Path to the folders location. I don't know if the cmd and call are needed. I have never used them in my scripts.

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)

Command runs in commandline but not through BAT file

I am running the following command in cmd for which I am able to get the log successfully.
cd C:\Users\sriram\AppData\Local\Temp\license1.1.4
C:\Users\sriram\AppData\Local\Temp\license1.1.4>lsmon.exe testprovilic.muc.company> C:\Users\sriram\AppData\Local\Temp\license1.1.4\usage.log
Above command will generate the logs in the usage log file. But I created a batch file as below for which it is giving me an error testprovilic.muc.company not found
#ECHO off
SET variable=C:\Users\s.d.vaidyanathan\AppData\Local\Temp\usage.log
START "C:\Users\s.d.vaidyanathan\AppData\Local\Temp\license1.1.4"lsmon.exe testprovilic.muc.company> "%variable%"
Could you please help me solve this issue.
Thanks and Regards,
Sriram
You need to add the "cd C:\Users\sriram\AppData\Local\Temp\license1.1.4" to the batch file. Or use the full path to the file like:
C:\Users\sriram\AppData\Local\Temp\license1.1.4\testprovilic.muc.company
So you would have ..
#ECHO off
cd C:\Users\sriram\AppData\Local\Temp\license1.1.4
SET variable=C:\Users\s.d.vaidyanathan\AppData\Local\Temp\usage.log
START "C:\Users\s.d.vaidyanathan\AppData\Local\Temp\license1.1.4"lsmon.exe testprovilic.muc.company> "%variable%"
or you could
#ECHO off
SET variable=C:\Users\s.d.vaidyanathan\AppData\Local\Temp\usage.log
START "C:\Users\s.d.vaidyanathan\AppData\Local\Temp\license1.1.4"lsmon.exe C:\Users\sriram\AppData\Local\Temp\license1.1.4\testprovilic.muc.company> "%variable%"
This doesn't work:
"C:\Users\s.d.vaidyanathan\AppData\Local\Temp\license1.1.4"lsmon.exe
The whole path needs to be in quotes and there needs to be a backslash after the last folder name.
Change it to this:
"C:\Users\s.d.vaidyanathan\AppData\Local\Temp\license1.1.4\lsmon.exe"
Open a command prompt window, type set and press key RETURN or ENTER to run this command. You get displayed all predefined environment variables with their current values. You can see TEMP, USERPROFILE and USERNAME.
Windows command line has a help. The command to get help is help, yes really. Try it out! You need help on command CD, enter in command prompt window help cd or alternatively cd /?. You need help on command SET and START, run help set or set /? and help start or start /?. All internal commands of cmd.exe and nearly all console applications support the parameter /? and output 1 or more help pages on running the command with this parameter.
You need a better overview of standard Windows commands? See Microsoft's command-line reference and SS64's command line reference.
I assume that sriram is your user account and s.d.vaidyanathan is the user account of someone else. By default a standard user has no permissions to access folders and files in a different user's profile than the own profile since Windows Vista. That means, you logged in as sriram can't access the files and folders of C:\Users\s.d.vaidyanathan because of missing permissions to do so. It would be necessary to use command Runas to run the batch file with account s.d.vaidyanathan.
#echo off
rem Execute lsmon.exe from license1.1.4 in my folder for temporary
rem files and folders and write the log file also into this folder.
"%TEMP%\license1.1.4\lsmon.exe" testprovilic.muc.company >"%TEMP%\license1.1.4\usage.log"
Note: Double quotes must be used around path AND file name. Just double quoting parts of a file name with path may or may not work depending on error correction and how the application is written. For details see answer on set environment variables with spaces.
See also the Microsoft article Using command redirection operators.
And regarding right usage of command START not really needed here see for example answer on How to call a batch file in the parent folder of current batch file?

Why won't a batch file run when being called from within another batch file?

I have a batch file that first creates another batch file containing a ClearCase cleartool command and second, runs it:
ECHO cleartool lsactivity -long "%ACTIVITY%"^>"%OUTPUTFILE%">FILETORUN.bat
CALL FILETORUN.bat
When running the batch, FILETORUN.bat is generated with the correct format, but the CALL to it is completely ignored.
If I ECHO output after the CALL to a log file, I can see that the script just skips over it.
What could it be?
I have tried removing CALL but it makes no difference.
EDIT: SOLUTION
Thank you all for the input. I found the problem. Before the write to batch and batch call in the script there was a command that read information into a variable from a file:
SET /p FILETODELETE=<rmname_%CLEARCASE_USER%.tmp
It reads only the first line. For some reason this created a conflict with temporary batch file, and I have no idea why. I used a different solution for reading the first line from a file and the conflict doesn't happen anymore:
(set FILETODELETE=)
for /f "delims=" %%q in (rmname_%CLEARCASE_USER%.tmp) do if not defined FILETODELETE set FILETODELETE=%%q
If anyone can shed some light it would be great!
SET /P waits for user input, so it actually will finish the command with what you are trying to execute after that and consume the input buffer, which might produce different results on each machine.
See set command reference for more details

Passing parameter to windows command script

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.

Resources