Call another .bat file from within the same folder? - windows

I have my primary Batch file in a folder with a second batch file in the same folder. How do I make it so that the second batch file can be called from within the first. This must be able to work with any user's Windows computer.
I figured it was something like this (in the primary file):
call C:\%UserProfile%\#Hashtag\gameData\second.bat
But it says my syntax or path is incorrect.

call "%~dp0second.bat"
%~dp0 gives you the path (including a trailing backslash) of the location of your currently running batchfile.

Agreeing violently with #Stephan, there's a sort of invisible change to the code besides using the %~dp0, in that the path now is also in " double quotes. That's most likely the issue with the first one because of expansion of the %UserProfile% variable... my guess is there's a space in that string.
There's no issue using the # character in a filename.

Maybe due to the folder name #hashtag it is throwing an error. Add the folder path to PATH variable and then try calling the batch file name directly.

Related

How do I pass a folder-path, which may or may not have spaces in it, to a batch file and append it the PATH environment variable?

Explanation
I have a simple little .bat file that looks like this. I'd like to run it anywhere from Windows 7 to 10. It doesn't do much just now but I'll later expand it.
#REM first argument includes path to folder which contains pdflatex.exe
set PATH=%PATH%;%1
where pdflatex #allows me to see if it can see the exe
The problem arises when I need to send a path that has spaces in it.
If I pass a folder path with no spaces, and no quotemarks it works fine.
If I pass a folder path with no spaces, and quotemarks it doesn't work.
If I pass a folder with spaces, quotemarks or not, it doesn't work.
I believe this is because Windows does not work well with quotemarks in the PATH variable, what with it being semi-colon delimited. %1 still contains the quotemarks by the time we're in the .bat file.
However quotemarks seem necessary from the command line perspective, to ensure it's treated as one argument.
The only method I can think of around this is to always include quotemarks around the path (ensuring it's always treated as one argument) and then cut off the first and last character.
This doesn't seem like an elegant solution, though, and I was wondering if there was a better way to do this.
In Short
How do I remove the quotemarks from a batch file's arguments?

How to run an executable that contains a space in path from command line on Windows 10?

H:\>"H:\Program Files\R\R-3.4.0beta\bin\R.exe"
'H:\Program' is not recognized as an internal or external command,
operable program or batch file.
H:\>"H:\Progra~1\R\R-3.4.0beta\bin\R.exe"
The system cannot find the path specified.
H:\>H:\Progra~1\R\R-3.4.0beta\bin\R.exe
The system cannot find the path specified.
I tried using "..." and Progra~1 and both are not working on Windows 10.
What I'm doing wrong?
Short answer: Use & 'C:\path with spaces\app.exe'
Explanation: Just type your path into powershell and use TAB for auto completion when you choose any directory containing spaces. Powershell will automatically insert single quotes 'bla bla' and it will also put an & in front which is needed to treat the string as something that should be executed. Continue completing your path like usual.
The way to do this - and I can't believe I'm just now figuring this out - is to use Windows short names generated for files with non-8dot3 names. To get the path or program name in question, type dir /x <path to program>. It will spit out something like PROGRA~1 for Program Files folder. Of course you have to do that directory by directory, and if you have multiple files/folders with spaces in the name, it's cumbersome. If you want the full path formatted with short names, you can do:
for %I in (*) do echo %~sI
For example, if the file I want to access is C:\Program Files\Microsoft SQL Server\Client SDK\ODBC\170\License Terms\License_msodbcsql_ENU.txt, I could type:
for %I in ("C:\Program Files\Microsoft SQL Server\Client SDK\ODBC\170\License Terms\License_msodbcsql_ENU.txt") do echo %~sI
And what I get back is the much easier C:\PROGRA~1\MICROS~2\CLIENT~1\ODBC\170\LICENS~1\LICENS~1.TXT.
Annoying that you can't query the whole directory path without using a loop, but it is what it is. Good for aliases.
There may be an easier way with powershell, but I'm pretty sure there isn't from the cmd prompt.
you must be doing something wrong as the double quotes encapsulates the path to the executable including spaces.
To ensure you are doing it correctly, start typing the path to the command and use TAB after F:\Program until you see the correct path, in your case it will automatically do this
"F:\Program Files"
Use your arrow key to go back behind the end quotation and continue the path and use tab until you have reached.
"H:\Program Files\R\R-3.4.0beta\bin\R.exe"
You can also try and issue it with Start
start "H:\Program Files\R\R-3.4.0beta\bin\R.exe"
If Your cmd windows is open on the actual System drive where "Program Files" are located, you can run this instead of adding the drive letter as well:
".\Program Files\R\R-3.4.0beta\bin\R.exe"
Or add it to your environment variables with the path, then it should execute with just:
R.exe

Using CMD to open an .exe file, from the same directory?

Im trying to make this launcher for my game.
But I can't seem to find anywhere, how to open a file. Without specifying the location.
Like I want it to run a file, from the same folder as the .cmd file is in. (The one I created).
Been searching for ages, without finding out how.
Reasoning: The user is able to change where the game is going to be installed. So I cant specify a location..
Simply open Command Prompt. Then Drag and Drop your .exe file to the console. (This will copy the location of the .exe file to the console) Then just press "Enter". Then your .exe file should be running.
References http://www.howtogeek.com/209694/running-an-.exe-file-via-command-prompt/
Hope it helps
The argument %0 of a batch file references the name of that batch file. So to run an exe located in the same folder where your batch file is, use:
%~dp0program.exe
The ~dp expands the argument %0 to a full qualified path name - including a trailing \ that's why there is no \ between %~dp0 and program.exe
Details about expanding variables can be found by typing help for on the commandline
Turned out, it was just me being foolish.
Forgot that I could just type in the name of the file.
Instead of doing something big out of it.
Thanks #ByteHamster for refreshing my memory :)
"Quote" ByteHamster: If you are in the same folder, just type in the name of the exe file.

trying to run a .bat that runs an .exe from a mapped drive or share and have file not found

I'm not sure what I'm doing wrong.
I have a file share called fileser01. I placed my exe which is hp.exe in \\fileser01\\software. I can map a drive whith this command:
net use f: \\fileser01\\software /user:domain\myaccount
So the drive shows as f:.
In the .bat file I have start "f:\" hp.exe but it says it can't find the file. What am I doing wrong? I even try to run a vbs and it says it can't find the file
I tried "f:" I tried putting it in another folder in software like media so "f:\media" and still same problem.
Your question says your batch files contains this:
start "f:\" hp.exe
No, that will not work. It needs to be this:
start "Window Title" "f:\hp.exe"
Or this:
start f:\hp.exe
The entire file name with no added spaces needs to be contained between the quotation marks. And in this case you could even omit the quotation marks since the file name and path contain no spaces, in which case you can also omit the window title.

decompiling chm file: hh.exe requires only the filename. no full path is allowed

I noticed that hh.exe is capable of decompiling a .chm file to a set of .html files. But in order to work it requires the path to the file be current directory and the filename be the filename itself, apparently without allowing a full path.
For example, this will work (in cmd):
c:\MyDir>hh -decompile extracted MyFile.chm
This won't work:
c:\>hh -decompile extracted c:\MyDir\MyFile.chm
This is relevant because I want to use a shell command from the registry to decompile the .chm file, and the parameter "%1" gives me the full path to the file only, and I don't know of a way to receive only the filename without the path
Try passing forward slash eg:
c:\>hh -decompile extracted c:/MyDir/MyFile.chm
You could also try to open command line in the folder containing your .chm file,
try 'shift' key and 'right click' mouse button and "Open command window here" option.
Make sure no file is sellected.
regards, joe
It won't work with paths quoted or otherwise.
It's easiest to just move the output folder and input *.chm into the root of the drive, cd there, and then simply run the hh.exe -decompile outputfolder input.chm command.

Resources