-2

Task

I'm trying to make a launcher (in batch) for my (almost) all batch program in the Program Files (x86) folder. I also need it to be compatible with any one's Windows 10 machine.

Problem

The cd command does not evaluate the path correctly. It just says The directory name is invalid.

Code

@echo off
color 02
cls
cd "%~dp0batchfile.bat"
start /max /realtime %cd%
exit

Debug

I've moved the file around, adjusted the code, and even put the raw path and it all worked! But it still gives the error and causes the program to malfunction. I'm worried it can cause damage to someone's computer and to others' as well. (It's pretty overcomplicated for batch lol) I've also tried to echo the cd before, the path it's supposed to be set to, and the cd after. The cd before and what it's to be set to is fine, but it still says The directory name is invalid. And "doesn't" get set. Here is the code I tried:

@echo off
color 02
cls
cd "%~dp0batchfile.bat"

rem Debug:

echo %cd%
pause

rem real code:

start /max /realtime %cd%
exit

And it's fine. It gives the error, yet it works without interruption and causes less lag and glitches. I haven't a clue why because for my smol phatt brain this shouldn't even be possible. Bless you. Maybe it's just a bug that Microsoft needs to fix, but whatever it is, it is so annoying and it ruins the cleanliness of my (almost) all batch program.

Conclusion (That you made)

You think: This imbecile doesn't even know where to START when asking questions on StackOverflow.

Me reading your thoughts: This is the first question I ask on StackOverflow. So yes, I mean no, no yes, wait wait no, I don't know where to start. What? Bless you. Oop Bless you again.

  • 2
    If you open up Windows Command Prompt, `cmd.exe`, type `cd /?`, and press the `[ENTER]` key, you'll see that it is for changing **directory**. It isn't for changing file, so do not give it a file name when that's clearly not a valid directory! – Compo Aug 08 '20 at 11:47

2 Answers2

0
cd "%~dp0batchfile.bat"

Why are you trying to cd into a batch file in the same directory as your script? That's obviously not going to work, as per the transcript you'll see idf you let cmd echo the commands before executing them (by commenting out the echo off, along with other stuff not needed for debugging):

rem @echo off
rem color 2
rem cls
cd "%~dp0batchfile.bat"
rem start /max /realtime %cd%
rem exit

If you run that, you'll see:

C:\Users\Allan>rem @echo off
C:\Users\Allan>rem color 02
C:\Users\Allan>rem cls
C:\Users\Allan>cd "C:\Users\Allan\batchfile.bat"
The system cannot find the path specified.
C:\Users\Allan>rem start /max /realtime C:\Users\Allan
C:\Users\Allan>rem exit

In other words, you probably should just be using %~dp0 in the cd command.

paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
  • Oh my lord. What on earth is wrong with me!?! Even if it changes the directory before it launches I can store that environment variable in another variable. I also forgot to mention that I was testing the launcher in the same folder as the program. I do not understand how I forgot to mention that, but as I said, I'm new to this. – Watch and Learn yeah Aug 09 '20 at 02:42
0

batchfile.bat is a FILE and not a directory. So it is not possible to change current directory to file batchfile.bat.

One solution is cd /D "%~dp0" to change the current directory to directory containing the currently executed batch file. This works as long as the batch file is stored on a storage media with a drive letter assigned. So the code would be:

@echo off
cd /D "%~dp0"
color 02
cls
start "Window Title" /MAX "FileName.exe"
color

But a batch file could be stored also on a network resource started with using UNC path. Windows command processor cmd.exe does not allow by default that a network resource path is set as current directory. The following code would be necessary for such use cases.

@echo off
setlocal EnableExtensions DisableDelayedExpansion
pushd "%~dp0" || exit /B
color 02
cls
start "Window Title" /MAX "FileName.exe"
color
popd

But a launcher batch file should not make the directory of itself the current directory, but the directory of the batch file to launch of which file name is passed to the launcher batch file as first argument. So a launcher batch file for other batch files could be:

@echo off
setlocal EnableExtensions DisableDelayedExpansion
if "%~1" == "" (
    color 02
    cls
    echo INFO: "%~nx0" must be started with a batch file name.
    echo(
    pause
    color
) else start "%~n1" /MAX %SystemRoot%\System32\cmd.exe /C "pushd "%~dp1" && (color 02 & cls & "%~nx1" & popd)"
endlocal

Do not use command exit to exit cmd.exe independent on calling hierarchy. That is not necessary here and makes it only more difficult to debug the batch file on executing it from within a command prompt window.

Do not use option /REALTIME of command START because this process priority class is mainly for drivers and should never be used for other executables and definitely not for a batch file processed by cmd.exe.

To understand the commands used and how they work, open a command prompt window, execute there the following commands, and read the displayed help pages for each command, entirely and carefully.

  • call /? explaining:
    %~dp0 ... drive and path of argument 0, the full batch file path always ending with a backslash,
    %~1 ... first argument with surrounding double quotes removed,
    %~n1 ... file name of first argument,
    %~dp1 ... drive and path of first argument,
    %~nx1 ... file name and extension of first argument.
  • cd /?
  • cls /?
  • cmd /?
  • color /?
  • echo /?
  • endlocal /?
  • goto /?
  • if /?
  • pause /?
  • popd /?
  • pushd /?
  • setlocal /?
  • start /?

See also single line with multiple commands using Windows batch file.

Mofi
  • 46,139
  • 17
  • 80
  • 143