1

I am doing a batch scripting assignment where I have to call one script from inside another. I need the script to run the second script no matter where my lecturer saves these scripts. How would I do this. Is there some way to find the path of script inside the script and use that to execute the file. Any help would be great. I think I need to use %'s but i'm not sure. The name of the script is Hello World.bat.

How would I copy Hello World.bat to the C:\ if I don't know which directory the lecturer has placed it in. what command/s would I use so that the copy would work regardless of the scripts location.

batsta13
  • 549
  • 2
  • 12
  • 26

3 Answers3

1

I don't see the "DOS" tag, but I'll assume that it is for now. If you want the entire path, you can get it by doing this:

echo %cd%

If you want just the last folder, this works (inside a .bat file):

for %%* in (.) do @echo %%~n*

Note that from the command line, the above command will work with single %'s:

for %* in (.) do @echo %~n*
Aaron
  • 55,518
  • 11
  • 116
  • 132
0

If the script you are executing is calling other scripts in the SAME folder location, you can prefix the path statement with "%~dp0" or "%~dps0" but do not put a backslash between that and the name of the script you are calling. In other words, if script1.bat is calling script2.bat in the same folder, the statement in script1.bat would refer to "%~dp0script2.bat"

Skatterbrainz
  • 1,077
  • 5
  • 23
  • 31
  • can you explain what exactly does this do %~dp0 – batsta13 Mar 23 '12 at 09:41
  • This question has a good explanation of %~dp0: http://stackoverflow.com/questions/1645843/batch-file-resolve-absolute-path-from-relative-path-and-or-file-name – Aaron Mar 23 '12 at 15:57
0

sorry about batch files, am not familiar, but in nix shell, there is the locate command which can return the path of the file , if you know the filename exactly and the name is unique.

like

name=$(locate filname)

Xander
  • 1,652
  • 2
  • 15
  • 20