xiaohei
xiaohei

Reputation: 97

Could someone help to understand this batch script?

I'm reading a batch file, but I do not understand it, can someone help to explain?

As I understand %0 is is the name of batch file, can we iterate on it? or is it a convenient way to represent a folder?

I can not find the variable %BatchPath% in the file, where do you think it's defined? And it seems APATH is defined in the two loops?

for %%x in (%0) do set APATH=%%~dpsx
for %%x in (%BatchPath%) do set APATH=%%~dpsx
pushd %APATH%

Upvotes: 1

Views: 660

Answers (2)

GolezTrol
GolezTrol

Reputation: 116110

You can iterate over a single value. It just means the set statement is executed once. The ~dps then strips the file name, so that only the directory remains.

The first line performs this action on %0, indeed the path and name of the current script.

The second line performs the same action on a given variable, Now that is the fun part, because if %BatchPath% is empty, nothing gets iterated, so the set statement on that line is not executed at all.

So effectively, it stores a directory, which is the directory of the script by default, but can be overridden by explicitly assigning a path to %BatchPath% before calling this script.

pushd allows you to save a directory, so you can return to it later using popd. It allows the script to jump to another directory an be able to restore the shell to the original directory before it terminates.

Upvotes: 3

%0 is the current batch file.
%%~dpsx gives the current batch file's short path here its giving the Drive name for eg "D:\" Pushd Stores the name of the current directory for use by the popd command before changing the current directory to the specified directory. APATH is some variable used to store the path.

so basically the script is fetching details about the script file name , its drive location and storing it to be used as location from which last batch file ran or something like that.

Upvotes: 0

Related Questions