.BAT = Change the path of %~dpnx0

micromega -  
 micromega -
Bonjour,

I use this code
set currentpath=%~dpnx0
to get the path of the .bat file.

However, I would like to move from this path to use another path.

Example: the ".bat" file is in F:\Mon\Dossier 1\fichier.bat
the command set currentpath=%~dpnx0 gives me this path, but I would like to go to F:\Mon\Dossier 2\Dossier 3\fichier.exe

How can I change directories from there?

Thank you

4 answers

  1. codeurh24 Posted messages 760 Registration date   Status Member Last intervention   123
     
    Hello.

    currentPath is a variable created by set.
    currentPath is a variable assigned by another ms-dos variable
    that returns the path and its file.
    d represents the drive letter
    p represents the file path
    n represents the file name
    x represents the file extension.

    To modify this path, you need to retrieve the path without the file.
    ~dp0

    Once the path is obtained through the ~dp0 variable, you add two dots to go up one folder.
    ~dp0..\
    In the upper folder, you specify the folder in which you want to place yourself, followed by the file name if desired. In my case, an html file.
    %~dp0..\mars-radio-dnb-2\index.html

    To keep the currentPath variable, I created another variable
    called cheminAuxiliare and assigned it the path I wanted.

    You just need to put echo in front of this variable to display it, or remove echo to launch the program. Here, in my case, it launches an html page.

    @echo off
    set currentpath=%~dp0
    set cheminAuxiliare=%~dp0..\mars-radio-dnb-2\index.html
    %cheminAuxiliare%
    pause
    1
    1. Mars Radio DNB Posted messages 13761 Registration date   Status Contributor Last intervention   1 439
       
      Thank you, my friend^^ for coming to the rescue!
      Unity is strength^^ or "the nions make strength" lol I can't remember lol lol..........
      0
  2. Mars Radio DNB Posted messages 13761 Registration date   Status Contributor Last intervention   1 439
     
    Hello,
    I don't remember, it's been a long time
    but from this path, you create a file containing the variables of the paths you call with the call command I think (I need to check tonight... if no one has answered you in the meantime).

    I don't remember, but if I look through my code, I should find it...

    --
    I think I'm smart enough to say that I know nothing...
    0
  3. micromega
     
    Hello and thank you for your help!

    Indeed, the solution from codeurh24 seems to work. Thank you.

    However, you mention "once the path obtained by the variable ~dp0, we add the two dots to move up one folder." >> 2 dots to move up one folder, but if we want to go up 3 or 4 folders for example, does that work or is there another technique?
    0
    1. codeurh24 Posted messages 760 Registration date   Status Member Last intervention   123
       
      go up one directory ..
      go up two directories ..\..
      go up three directories ..\..\..
      go up four directories ..\..\..\..

      you can play with this using cd in command line
      cd ..
      0
    2. dubcek Posted messages 18627 Registration date   Status Contributor Last intervention   5 659
       
      hello
      to go up 3 levels, add :\..\..\..
      0
  4. micromega
     
    Understood!

    Thank you for your quick and effective responses.
    0