1

Ok, the the question looks freakish, but this is sort of a continuation of the question I posted here.

So, I create a .bat file in Visual Studio with certain lines and launch it, but it basically doesn't find the files it needs, but if I launch the .bat file it created manually, it works.

The problem, as far as I see it, is that the .bat file the program launches isn't the same as the one that is created in the folder?

The .bat files use the command line interface of Asesprite found here, e.g. :

@set ASEPRITE="C:\Program Files\Aseprite\aseprite.exe"
%ASEPRITE% --batch animation.ase --scale 2 --save-as animation-x2.gif

I'm not sure which part of the VB code I'd need to share, so ask if needed.

The error in .bat goes something like:

C:\Users\User\Desktop\aseConverter\aseConverter\bin\Debug>"E\Asesprite\asesprite.exe" --batch skeleton2_gib3.ase --scale 1 --save-as skeleton2_gib3.gif
File not found: "skeleton2_gib3.ase"
Error loading file "skeleton2_gib3.ase"
A document is needed before --save-as arguement

The first line should not be the Debug folder, but the location the .bat file was created in. I've no idea how to fix it.

It SHOULD be

C:\Users\User\Desktop\skeleton>"E\Asesprite\asesprite.exe" --batch skeleton2_gib3.ase --scale 1 --save-as skeleton2_gib3.gif
Community
  • 1
  • 1
lagxbag
  • 71
  • 6
  • This question is kind of all over the place. Can you please clarify what it is you're asking? – rory.ap Jan 31 '15 at 12:28
  • @roryap The question basically is how can I launch the .bat file from the program made without the .bat file thinking it's the location where the .bat file is? For an example, if I move the program in the folder the .bat file is created with the .ase files, everything works. – lagxbag Jan 31 '15 at 12:34
  • *"File not found"* is a pretty good hint, that the application failed to find a file in the place it was looking. To prevent the application from guessing where to look, provide fully qualified path names. – IInspectable Jan 31 '15 at 12:36
  • @IInspectable The thing is, I am sure I am specifying the right paths. The problem I think is that the program launches a .bat file from memory, not where I created it. – lagxbag Jan 31 '15 at 12:39
  • I don't see a path in *skeleton2_gib3.ase*. I don't understand how you come to the conclusion that you are specifying the right path. – IInspectable Jan 31 '15 at 12:44
  • 1
    See [What does %~dp0 mean, and how does it work?](http://stackoverflow.com/questions/5034076/) and make use of it in your batch file for example by using as first command `cd /D "%~dp0"` to set current working directory to directory of the batch file. Or use at top of batch file `pushd "%~dp0"` and at bottom of batch file `popd`. Of course you could set also the working directory for started `cmd.exe` process already in VB.net application. – Mofi Jan 31 '15 at 12:45
  • @Mofi Setting the working directory fixed it, thank you very much! The %~dp0 is extremely confusing for me, and I avoided it. I've no experience in this. But thank you! – lagxbag Jan 31 '15 at 12:57
  • @IInspectable the path of the .ase files do not have to be specified, it checks the folder of these files, that's why the location of the .bat file is important. But setting the working directory fixed it, so no more problem. – lagxbag Jan 31 '15 at 12:57
  • Setting the working directory may have cured the immediate symptoms. It hasn't solved the cause: Using relative paths is almost always an error. Relying on an environment outside your control is frivolous. The issue will surface again, for example, if an updated EXE will change the current working directory itself, or if you add applications to your .bat-file that do. – IInspectable Jan 31 '15 at 13:12
  • @IInspectable I see what you're saying, but I doubt I will add anything else to the program - I just needed to create a better animation workflow to quickly export multiple .ase files in a certain folder as either .gif or .png sheets for a game. Now that it does everything I need it to do, I can go back to doing what I should be - animating. – lagxbag Jan 31 '15 at 13:31

1 Answers1

1

The problem here is that the batch file references files without path. Therefore the files must be in current working directory of the batch file.

But the batch file respectively command line interpreter cmd.exe is called by the VB.net without setting the working directory. Therefore the current working directory set by Windows for the batch file is the same as of the starting VB.net application.

But the starting application creates the batch file and the other files in a different directory, not in its own current working directory.

One solution is changing current working directory inside batch file to the directory the batch file is stored. This can be done by referencing argument 0 of the batch file which contains name of batch file with complete path.

What does %~dp0 mean, and how does it work? explains how to get drive and path of batch file.

Therefore one solution is to use a batch file like below:

@echo off
cd /D "%~dp0"
set "ASEPRITE=%ProgramFiles%\Aseprite\aseprite.exe"
"%ASEPRITE%" --batch animation.ase --scale 2 --save-as animation-x2.gif

See help output after executing cd /? in a command prompt window for meaning of parameter /D (change also drive if necessary).

An explanation for %~dp0 can be read on running call /? or for /? in a command prompt window.

Another possibility would be the usage of following batch code:

@echo off
pushd "%~dp0"
set "ASEPRITE=%ProgramFiles%\Aseprite\aseprite.exe"
"%ASEPRITE%" --batch animation.ase --scale 2 --save-as animation-x2.gif
popd

The difference to command cd is explained in help which is output in a command prompt window after executing pushd /?.

Best would be to create the batch file with all files referencing with complete path, name and file extension.

Community
  • 1
  • 1
Mofi
  • 46,139
  • 17
  • 80
  • 143