0

I have a batch script that I'm running from one folder, that is calling a .NET executable in another folder, and then passing in folder parameters to it that exist in another folder. It looks like this:

SET start_dir=%cd%
cd..
cd apps
SET apps_dir=%cd%
SET my_dir=%apps_dir%\MY_DIR

cd C:\projects\ConsoleApplication1\src\ConsoleApplication1\bin\Debug

ConsoleApplication1.exe -i  -f %my_dir%

cd %start_dir%

Is this the proper way to pass in a directory to an application in a batch script? My app doesn't seem to be able to find the directory and I didn't know if there was a way to troubleshoot these things in a script or if this is the proper way or not since it's the first time I've done a Windows batch script.

1 Answer 1

0

Should be, but batch is sensitive to spaces in a SET statement. SET FLAG = N sets a variable named "FLAGSpace" to a value of "SpaceN"

The syntax SET "var=value" (where value may be empty) is used to ensure that any stray trailing spaces are NOT included in the value assigned. set /a can safely be used "quoteless".

This is important whenver there are spaces in file or directory names.

I'd suggest some minor changes:

@echo off
setlocal
SET "start_dir=%cd%"
cd ..
cd apps
SET "apps_dir=%cd%"
SET "my_dir=%apps_dir%\MY_DIR"

cd "C:\projects\ConsoleApplication1\src\ConsoleApplication1\bin\Debug"

ConsoleApplication1.exe -i  -f "%my_dir%"

cd "%start_dir%"

Noting

  • @echo off turns off command-echoing for the duration of the batch

  • setlocal ensures any environment changes made are backed-out when the batch ends so you don't get problems with stale data from changes made by prior runs

  • cd "destinationdir" - the quotes are not usually required, even with "spaces in pathnames" but may be required for strange pathnames (like those containing &)

  • "%my_dir%" as an argument for consoleapplication1 to ensure it's treated as a single token

  • if consoleapplication1.exe is in a directory on the path, then there's no need to cd to that directory; the command consoleapplication1 .... will access it. Naturally, if the application is provided with an unqualified filename, then it will assume that the file is in the current directory, not the executable's directory, so there may be a need to switch directories...

  • if consoleapplication1.exe is in a directory which is not on the path, then "C:\projects\ConsoleApplication1\src\ConsoleApplication1\bin\Debug\consoleapplication1" .... will access it. The quotes are harmless if included but required in the case of spaces in the full-executablename.

  • path from the prompt will show the execution path; each directory separated by semicolons.

  • it's possible to use

@echo off
setlocal
SET "start_dir=%cd%"
pushd ..\apps
SET "apps_dir=%cd%"
SET "my_dir=%apps_dir%\MY_DIR"
pushd "C:\projects\ConsoleApplication1\src\ConsoleApplication1\bin\Debug"

ConsoleApplication1.exe -i  -f "%my_dir%"
popd
popd

where pushd changes to the specified destination directory the current saved on a simple stack. popd pops the directories back from the stack.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.