Raulp
Raulp

Reputation: 8146

Setting the path of an exe to a variable in batch script

I am trying to set the location of an exe(exe_sources/build/exefile.exe) to a variable by doing this ;

set script_path1=%~p0
set exe_path=script_path1\..\build\
echo exe_path

directory structure looks like this :

exe_sources
  --build
     --exefile.exe
  --runtest
    --testscript.bat

but I never get the correct path to the exefile.exe in exe_path.

How can this be achieved.

Upvotes: 0

Views: 1143

Answers (1)

Riccardo Volpe
Riccardo Volpe

Reputation: 1623

  • In Linux, for a BASH (testscript.sh) script:

      #!/bin/bash
    
      <<TREE
      exe_sources
      --build
        --exefile.exe
      --runtest
        --testscript.sh
        --testscript.bat
      TREE
    
      script_path1=$(dirname "$(readlink -f ./testscript.sh)"); # absolute path: /c/Users/Riccardo/Desktop/exe_sources/runtest
      B=$(basename "$(readlink -f ./testscript.sh)"); # testscript.sh
      ls "${script_path1}/../build" # exefile.exe
      exe_path="${script_path1}/../build";
      echo ${exe_path} # /c/Users/Riccardo/Desktop/exe_sources/runtest/../build
      ls ${exe_path} # exefile.exe
    

    You can run it in Windows too, by using the Git Bash:

      sh ./testscript.sh
    
  • Always in Windows, for a DOS Batch (testscript.bat) script:

      @echo off
    
      goto TREE
      exe_sources
      --build
        --exefile.exe
      --runtest
        --testscript.sh
        --testscript.bat
      :TREE
    
      set script_path1="%~dp0"
      echo %script_path1%
      ::"C:\Users\Riccardo\Desktop\exe_sources\runtest\"
      set script_name=%0
      echo %script_name%
      ::"C:\Users\Riccardo\Desktop\exe_sources\runtest\testscript.bat"
      set exe_path="%~dp0..\build"
      echo %exe_path%
      ::"C:\Users\Riccardo\Desktop\exe_sources\runtest\..\build"
      dir %exe_path%
    

Upvotes: 1

Related Questions