Batch file invoked file details - batch-file

I invoke a batch file from my own extension file type.In batch file i will show the details of invoking file. I know by passing parameters when invokation we will pass it and we will get by "%~sn1" OR "%1" OR "%~nx1". But i need without passing parameters.
Sample Example
My batch file code looks like this(main.bat)
#ECHO on
set modelname=(here i want help)
java -Djava.library.path="C:\Program Files\Internet Explorer" -Xms1024m -Xmx1024m -jar dist/XYZ.jar -models %modelname%
exit
If i click "Kitchen.xyz" then it'll invoke my batch file "main.bat". Now i want set "modelname" as "Kitchen.xyz". If i click "LivingRoom.xyz" ,:modelname" set as "LivingRoom.xyz".
Can anyone help please...
Thanks

You will need to change the Windows Registry associated with the file type of .xyz
[HKEY_CLASSES_ROOT\.xyz\shell]
[HKEY_CLASSES_ROOT\.xyz\shell\Name Of Your Command]
#="&Name Of Your Command"
[HKEY_CLASSES_ROOT\.xyz\shell\Name Of Your Command\command]
#="\"C:\\Path\\To\\Your\\Batch\\File\\main.bat\" \"%1\""
Save that text in a .reg file and execute it to apply the registry changes. Then when you right click .xyz files, "Name Of Your Command" will be one of the options and possibly the default option which would run when double clicked.
Then your main.bat still needs to accept one parameter and assign it to modelname.
You could also do that programatically with the REG command, but regardless of which method you use to set things up, something needs to be run to setup each machine you want to do this on.

Related

Batch File To Get It's Own Directory Or The Directory Defined In The "Start In" Property Of A Shortcut

I am writing a batch file on my Windows 8.1 machine. In one section of my batch file I need to start a command prompt in the "current working directory".
So far, this is what my batch file looks like:
#echo OFF
set WORKING=%cwd%
start cmd.exe /K pushd %WORKING%
exit
Let's say the batch file is located in the folder C:\Temp\Utilities. If I open an explorer window and double click the batch file to run it everything works great. A new command prompt is created in the directory C:\Temp\Utilities. However, if I right-click the batch file and select Run as administrator the working directory is no longer the location of the batch file, it's C:\Windows\System32.
Similarly, if I create a shortcut to the batch file in a different folder (for example. C:\Temp) and repeat the two steps above the results are the same. If I double click the shortcut and run it as a normal user the working directory is what I would expect. (Note, the working directory for the shortcut it's whatever is set for "Start in" of the shortcut properties, not the location of the batch file.) If I right click the shortcut and run it as administrator I again get a command prompt opened to the folder C:\Windows\System32.
I assume this is a "bug" or "feature" (if you want to call it that) in Windows 8.1 and it probably happens because execution environments for programs run as administrator are forced to run in the System32 folder? (I remember with Windows 7 this did not happen so it must be a new feature to Windows 8.)
I found one way to fix the issue and stop the command prompt from starting in C:\Windows\System32. I did this by modifying the following line in the batch file:
set WORKING=%~dp0
Doing it this way sets the working directory to the location of the batch file. With this change, no matter how I run the batch file or the shortcut (administrator or normal) the working directory ends up being the same, C:\Temp\Utilities.
The problem with this solution is I don't want the working directory to always be the location of the batch file. If the batch file is run directly then it's okay but if I run it from a shortcut I need the working directory to be whatever is set in the "Start in" property of that shortcut. For example, if the batch file is located in the folder D:\Temp\Utilities this is what I need to happen regardless of whether I run as administrator or not:
Shortcut Location Start In Property Command Prompt Working Directory
-------------------- ------------------- ------------------------------------------
C:\Temp <undefined> D:\Temp\Utilities
C:\Data\bin C:\Data\bin C:\Data\bin
C:\Data\bin D:\Temp\Utilities D:\Temp\Utilities
What this means is I can't always use %~dp0 to set the working directory in my batch file. What I need is some way for the batch file to know if it was run either directly or by a shortcut. If the batch file is run directly then the working directory is easy to get, it's just the value of %cwd%. If the batch file is run by using a shortcut, I don't know how to get the "Start in" property inside the batch file.
Does anyone know how I can do these two things inside my batch file:
1. Check whether it was run directly or by a shortcut.
2. If run by a shortcut, get the "Start in" property of the shortcut that started it.
Thank you,
Orangu
UPDATE
I found sort-of a "hackish" way to fix the issue. For the shortcut I edited the "Target" field and changed it to the following:
cmd.exe /k pushd "C:\Temp" && "D:\Temp\Utilities\batchfile.bat"
Now the working directory can be obtained by calling %CD% in the batch file and this works for both administrator and normal users. It does not, however, work for the case when I run the batch file directly. I still need to use %~dp0 in that case.
I don't like this solution, however, because it requires me to manually change all shortcuts I make and it also makes the icon look like a cmd prompt icon rather than a batch file.
Have you already considered to not use shortcuts at all?
You could e.g. create a batchfile_exec.bat containing your call
REM optionally do
REM cd /D working_directory
REM if you want to force a special working directory
D:\Temp\Utilities\batchfile.bat
and replace all the shortcuts with batchfile_exec.bat. If you double-click batchfile_exec.bat, the working directory will be the one containing batchfile_exec.bat.
I personally don't like Windows shortcuts that much, because they are hard to handle within a revision control system. As you also noticed, it is very time consuming if you want to modify a lot of them.
By the way: If batchfile.bat was designed/written to be always run from the direcory where it is located, you might also consider to modify batchfile.bat to force that behaviour:
setlocal
cd /D %0\..
REM your original content
endlocal
In %0 the path to the batchfile is stored.
The trick is to assume that %0 is a directory and then to change one level lower based on that directory. With /D also the drive letter is changed correctly.
The cd command doesn't care if %0 is really a directory. In fact %d doesn't even have to exist (%0\dummy\..\.. would also work).
The setlocal command is to have the working directory being restored when batchfile.bat has finished (this would be good if batchfile.bat was called form another batch file).
I noticed that the endlocal command is not really necessary in this context since it is applied implicitly when batchfile.bat finishes.

Installing exe through batch file

I am trying to install through batch file..
ECHO OFF
ECHO Installing MySoftware . . .
"%~dp0\MySoftware.exe" /S /v/qn"UPGRADEADD=link goes here"
pause
but it fails to install.
Not much info to go on. What you have will not work if executed from a UNC drive and may not work if you 'Run as administrator' because the current directory gets changed. Try this. Of course that may not fix it and further details would be nice.
#ECHO OFF
PUSHD "%~dp0"
ECHO Installing MySoftware . . .
"MySoftware.exe" /S /v/qn"UPGRADEADD=link goes here"
Adding to my answer based on comments provided.
Presumably your bat file is in the same folder as MySoftware.exe. If it takes that long, it sounds like the install is working. Try doing
"MySoftware.exe" /?
That may give you a help screen to tell you more about the arguments beng passed. Also, try what you are now doing without the /S (which probably specifies a "silent" install... which is why you don't see anything.
PART 1 - If you want to create a "Setup" File in batch.
Maybe it works, but this is will be very hard to you for done this program.
Let's call the EXE File "Game1:
I will recommend you to take all the Game1 file's code (Maybe you can use the program Notepad++ for do this) after you taked Game1's code do this like i writing here
Let's say that the code of Game1 is:
ABC
Copy the code, then go to the batch file.
The "Setup" file of Game1 HAVE to come with a empty EXE file.
You can make a empty EXE file with notepad - just save the file as:
Name.exe
Then you doing at the batch file script this thing:
set %something%=ABC
After you done this you adding this to the batch script:
Echo %something% >> Name.exe
Don't forget to name the EXE file at the name of the program / game.
And now, if this message didn't help to you, maybe you need to make a EXE from batch file.
PART 2 - If you want to make an EXE file of batch file.
Open the start menu of Windows and search this:
IExpress
Don't let the computer search for you the full name, its working only if you wtiting the full name.
After you search IExpress, click on "Activate Command".
Click on Next, Don't change the first options.
Click on "Extract files only" and click on Next.
Name the EXE program and click Next.
Stay on "No prompt." and continue.
Now you can display a program License. if you want do a txt file and choose the display option.
Add batch files and click Next.
click on the option you want and click Next.
If you want a finish message, click on display message and write the message.
Here browse where the EXE will be and choose your options, click Next.
click Next.
Wow that's was super-long! Hope I helped you!

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
)

Change open file with... regedit

I have a user, which couldn’t get along with AutoCAD so he switched back to InterCAD. He’s not too computer literate so now when he tries’s to open a DWG file (AutoCAD native file extension) by double clicking it he’s register settings look for the AutoCAD program to open it.
I know that we can tweak the register settings for a .dwg file to open the file automatically with InterCAD rather then AutoCAD.
I’m not too un-familiar with tweaking the registry keys and when I do I like to automate this using batch script.
What is the best procedure to do this, I'm namely worried I will miss a key or is the following the only key I need to tweak
HKEY_CLASSES_ROOT\.dwg\OpenWithprogids
The extension to execute Intercad in Intercad.exe
How do I successfully achieve my desired result?
The key you need to edit will be this one:
HKEY_CLASSES_ROOT\dwgfile\shell\open\command
That controls the program the file opens with.
To do this in a batch file use this:
reg add HKCR\dwgfile\shell\open\command /v "" /d "programpath.exe" /f
Hope this helps.

Run batch file in the background

I have a batch file, this batch file will not start automatically, it will only run when i double click on it.
Can I run the batch file in the background when I double click on it.
Well, you can start it minimized with start, if that is enough. Really hiding it is difficult (although I can think of an option right now).
Basically you need to determine whether the batch has been started by double-clicking it. You can do this by defining a special variable and look for it:
#echo off
if not defined FOO (
set FOO=1
start /min "" %~0
exit /b
)
rem here whatever you wanted to do originally in the batch
As long as the FOO variable isn't defined (which is probably the default almost everywhere), this batch will launch itself minimized again, but with the variable defined first. Environments are passed to subprocesses, which is why this works.
you would generally need something else to run the script in that manor
i.e.
Create a shortcut, and set the “Run” field for the shortcut to “Minimized’.
Once you click or tab away from the cmd.exe window that the batch file is running it, it's "in the background" -- I'm not really sure what you want but it sounds like you might be asking how to run the batch file without displaying the cmd.exe window.
If so I can think of two ways: first, you can create a shortcut to the batch file, right click it, and in the properties there set the shortcut to run minimized (should be a drop down option next to Run).
You can also wrap invocation of the batch file in a VBScript file using Windows Script Host's shell object (calling the Run method) to run the batch file invisibly. Passing 0 as the intWindowStyle parameter will suppress display of a window or anything.
#Ghyath Serhal
I have used cmdow to do this on another program, it is an external application that can be used to modify the command prompt. To use it, you will need to either enter this code (see below) into it's own batch file, or into the command prompt, where it will run 'BatchFile.bat' with a hidden terminal window. I haven't found a way to use this in a single batch file, but I only found out about this today.
cmdow /run /hid 'BatchFile.bat'
Hope this helps.

Resources