1

First, sorry for my English, but I try my best to explain the situation. I'm no real pro about *.bat files, but I know the basics to run exe files with it.

bat-script:

setlocal
cd "%~dp0"
start "" "%~dp0\Lang\Language.exe"

I need to start "Language.exe" inside "%~dp0" (root), where the *.bat is saved. I read many different questions/answers on stackoverflow, but none worked. The "Language.exe" is saved in "%~dp0\Lang", but it need to be run in "%~dp0" or it won't work.

The *.exe will only work from root (%~dp0), nowhere else. And there can't be any real folder structures like C:\root\Lang\Language.exe, because it have to work for others as well.

  • *.bat location --> root
  • file to start at *.bat location --> root\Lang\Language.exe

The "Language.exe" converts a language to some other with a diff-patch. I mean the *.bat starts the *.exe (also with other command variations I tried), but it says, that it can't find the files to patch. Yeah, because the so called working directory is not right (need to be "root"). But all that without moving the *.exe or anything, it should only be started in "root" from "root\Lang\Language.exe", nothing else.

EDIT: As a workaround, I now simply move the "Language.exe", start it and move it back.

setlocal
cd "%~dp0"
move "Lang\Language.exe" "%~dp0" >nul
ECHO Starting patch...
timeout /t 1 >nul
start "" /wait "Language.exe"
move "%~dp0\Language.exe" "Lang" >nul
m3Zz2012
  • 13
  • 5
  • I assume you're using the `d` modifier so your code will be drive-letter agnostic? – user2752467 Dec 28 '14 at 20:27
  • It sounds like the problem is "Language.exe" is recognizing the _its_ working directory as `%~dp0\Lang` instead of just `%~dp0`. Is that correct? – Jason Faulkner Dec 28 '14 at 20:28
  • Tried: start "" /d "%~dp0\Lang\Language.exe" And: start "" "%~dp0\Lang\Language.exe" -wo %~dp0 And a couple more variations I forgot. Yes, Jason, that is exactly the problem. The so called "Language.exe" is just a placeholder, in real there are 11 patches, that's why I can't just run Language.exe. The bat is there to choose between them etc. The menu etc. is all working, but not the actual execution. – m3Zz2012 Dec 28 '14 at 20:33
  • `start /?` gives next hint for you: `START ["title"] [/D path] ...` where the `path` in the `/D` switch refers to _starting directory_ – JosefZ Dec 28 '14 at 20:35
  • 1
    start "" /D "%~dp0" "%~dp0\Lang\Language.exe" Doesn't work, if you talked about that, JosefZ. Same error, can't find files to patch, so still exec in "%~dp0\Lang". I can't use any drive letter or anything. – m3Zz2012 Dec 28 '14 at 20:41
  • @JosefZ - Not sure that will work b/c regardless of where the command prompt path is set, "Language.exe" will read _its_ path as the working directory. – Jason Faulkner Dec 28 '14 at 20:42
  • I'm not sure this possible without "Language.exe" accepting a parameter where you can specify the path. – Jason Faulkner Dec 28 '14 at 20:43
  • Maybe this helps, I think the commands that can be used: http://www.indigorose.com/webhelp/vp/Program_Reference/Command_Line_Options.htm I used "Visual Patch" to build all patches. But there seem to be only one option (/T:) for the temporary files. – m3Zz2012 Dec 28 '14 at 20:46
  • What if you add %dp0\Lang to the system path variable? Then you can call Language.exe from anywhere without specifying the folder. – SomethingDark Dec 28 '14 at 20:49
  • It needs to be portable, because others (without edited system path variables) have to use it. This is not for my local PC, it's for others/different windows setups. – m3Zz2012 Dec 28 '14 at 20:53
  • You can have the line `set path=%path%;%~dp0\Lang` and the path variable will only be changed for the duration of the script. – SomethingDark Dec 28 '14 at 20:58
  • Doesn't seem to work (patch can't find files to patch). Tried: `setlocal` `cd "%~dp0"` `set path=%path%;%~dp0\Lang` `start "" "%~dp0\Lang\Language.exe"` I'm not that familiar with all those commands, I mean where to place them etc. – m3Zz2012 Dec 28 '14 at 21:06
  • `cd "%~dp0"` `set path=%~dp0;%~dp0\Lang` `start "" "Language.exe"` Doesn't work as well. Patch doesn't find files for patching. But "Language.exe" works after moving it manually to root (%~dp0), just to let you know. The problem isn't the patch itself. – m3Zz2012 Dec 28 '14 at 21:12
  • You seem set on not moving Language.exe from Lang even though it would solve your problem. Could you _copy_ it to %~dp0, run it from there, and delete the copy when you're done? – SomethingDark Dec 28 '14 at 21:33
  • There are 11 so called "Language.exe"'s (name is just a placeholder) and I also thought about just copying the file for the patching process and delete it after finished. But those 11 Language.exe files are big and copying just one would take a long time (~1 min. on SSD), which is too much. – m3Zz2012 Dec 28 '14 at 21:40
  • Would it be more feasible to move the files to be patched to Lang and then move them back? – SomethingDark Dec 28 '14 at 22:02
  • That would take longer than copying the Language.exe. =) There are hundreds of files. So it seems, that there's no real command to run a *.exe in some other dir without moving. – m3Zz2012 Dec 28 '14 at 22:09
  • Correct. In order to run a command, you have to specify where the command is located, and the command always runs from there. I'm just amazed Language.exe doesn't take parameters. How does it know what files to update? It must be using relative paths instead of fully qualified paths for some unfathomable reason. – SomethingDark Dec 28 '14 at 22:18
  • There are plenty variables for Visual Patch (global & session), see here: http://www.indigorose.com/webhelp/vp/Program_Reference/Variables/Variables.htm This is a new link, not the one from comment #8. But I'm not really sure how to use them in a bat or if they can be used at all. Maybe those variables makes sense to someone. Some makes sense to me, to define specific windows folder etc., but I have a hard time with this stuff. – m3Zz2012 Dec 28 '14 at 22:40

1 Answers1

0

Set working directory

It is possible that you run into the issue discussed at In Batch file ~dp0 changes on changing directory.

I can think of 3 solutions.

Solution 1:

cd /D "%~dp0"
start "Language Patch" Lang\Language.exe

First the working directory is changed and then the EXE is called with a relative path. Parameter /D is necessary if current working directory on start is on a different drive than location of the batch file.

Solution 2:

setlocal
set "BatchPath=%~dp0"
cd /D "%BatchPath%"
start "Language Patch" "%BatchPath%Lang\Language.exe"
endlocal

The path of the folder containing the batch file is first assigned to an environment variable. This path ends with a backslash. Therefore the EXE can be called without a backslash before Lang.

Solution 3:

setlocal EnableExtensions
pushd "%~dp0"
start "Language Patch" Lang\Language.exe
popd
endlocal

push and popd are used in case of folder with batch file is not on a drive with a drive letter, but on a public network share. Read help of pushd and popd output by running in a command prompt window pushd /? and popd /? for details about those 2 commands.

Application directory used

But all those variants above do not help if Language.exe does not search for the files to patch in current working directory, but in its own application directory.

For example if Language.exe is written using Qt and uses inside the static function QCoreApplication::applicationDirPath() instead of searching for the files in current working directory, i.e. use QDir::currentPath() respectively search for the files without path and without changing working directory first.

Or if Language.exe is written using .Net and the application directory is used with one of the methods explained at How can I get the application's path in a .NET console application? instead of using current working directory.

In this case the best solution is copying Language.exe into the directory with the files to patch and delete the executable after it has terminated itself after patching all the files.

setlocal EnableExtensions
pushd "%~dp0"
copy /Y Lang\Language.exe .>nul
echo Starting patch...
Language.exe
del Language.exe
popd
endlocal
Community
  • 1
  • 1
Mofi
  • 46,139
  • 17
  • 80
  • 143
  • Cool, I will try some of your suggestions. Very good listed, Thank you! Right now, I just "move" the file, install, and move it back. So "copy" and "del" are "move" on my side and a bit different code (see my edit at post #1). But with your code, maybe I can optimize it. – m3Zz2012 Dec 30 '14 at 16:11
  • In the end only copy/move helped me, so it's the patch itself. Thank you again for your detailed description. – m3Zz2012 Dec 31 '14 at 13:26