How to set up root directory ?

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
goodywp
Posts: 265
Joined: 31 Jul 2017 09:57

How to set up root directory ?

#1 Post by goodywp » 27 Nov 2017 15:22

Hi all,

I have my tools located in C drive as below
I have all my code and source under the folder of auto_pkg_build
C:\auto_pkg_build\Scripts
C:\auto_pkg_build\Sources
C:\auto_pkg_build\Tools
C:\auto_pkg_build\Workspace

This is my local but if my colleague want to install somewhere in his/her PC under autopackager folder like this as below

C:\Users\alex\autopackager\Scripts
C:\Users\alex\autopackager\Sources
C:\Users\alex\autopackager\Tools
C:\Users\alex\autopackager\Workspace

I used a lot command in my code referring to my path as C:\auto_pkg_build\Scripts...or C:\Users\alex\autopackager\Sources

How Can I set up the relative path easy for everyone wherever he want to install the tool. He can still use this tool without any path issue...




Thanks

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: How to set up root directory ?

#2 Post by aGerman » 27 Nov 2017 16:25

Your explanation is incomplete because you didn't say if the script from where you want to access those folders is inside of this folder structure.
My assumption:
You have subfolders Scripts, Sources, Tools, and Workspace. Now you want to call a script in Scripts but your current working directory is e.g. Workspace. In that case you can use a relative path like

Code: Select all

call "..\Scripts\example.bat"
The .. points to the parent folder of the working directory. If the working directory is already auto_pkg_build or autopackager in your case then you can simply begin with the subfolder like

Code: Select all

call "Scripts\example.bat"
Steffen

goodywp
Posts: 265
Joined: 31 Jul 2017 09:57

Re: How to set up root directory ?

#3 Post by goodywp » 28 Nov 2017 09:30

Thanks Steffen!

I should explain more precisely. Since I have a lot commands to use the absolutely path in my script, for example, I might call one command which path looks like this
call C:\auto_pkg_build\Tools\PACKAGER\scheme_replace.cmd
or
cd C:\auto_pkg_build\Scripts\scheme_replace\pkg_data\temp
copy C:\auto_pkg_build\Scripts\scheme_replace\data_profile.txt .

Is any way I can use relative path in this case? Since I already use my absolutely path in the code...
In particular, if some one want to install the tools in his favorable folder rather following my structure....:-(

I once tried to run one script to start up like set_path.cmd

Code: Select all

SET Sc_PATH=C:\auto_pkg_build\Scripts
SET Sr_PATH=C:\auto_pkg_build\Sources
SET Tls_PATH=C:\auto_pkg_build\Tools
SET WK_PATH=C:\auto_pkg_build\Workspace
here auto_pkg_build can be replaced by any folder for user's preference.....

So later on whenever I wan to use these paths, just to call this script at the beginning like

call set_path.cmd (But here again it need its path)

How can I solve this problem....?

Thanks

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: How to set up root directory ?

#4 Post by aGerman » 28 Nov 2017 10:44

Still don't get it.
Please try this script in order to see how things are related.

Code: Select all

@echo off &setlocal
cd /d "%systemroot%\System32"

echo script folder:     "%~dp0"
echo working directory: "%cd%\"
echo ~~~~~~~~~~~~~
echo parent script folder: "%~dp0..\"
for %%i in ("%~dp0..") do echo that is:              "%%~fi\"
echo ~~~~~~~~~~~~~
echo parent working directory: "..\"
for %%i in ("..") do echo that is:                  "%%~fi\"
echo ~~~~~~~~~~~~~
pause
Steffen

goodywp
Posts: 265
Joined: 31 Jul 2017 09:57

Re: How to set up root directory ?

#5 Post by goodywp » 28 Nov 2017 14:10

Thanks a lot! Steffen

I think I know how to use %~dp0 now

But how I can use the sister folders as scripts folder, here is my code
the script is currently in ..\Scripts\scheme_replace\ folder
Here is the code

Code: Select all

@ECHO OFF
call "%~dp0\get_src.cmd"
call "%~dp0\app_var.cmd"

::sdrv is you network mapping drive for QADATA
set drv=Z:

:: rel is the path from QA release to Tetra Packages
set rel=QA_AUTOMATION\QA_RELEASE\Tetra_Applications\Tetra_Packages

:: app is a general application group under Tetra release
set app_var=%app_var: =%

:: pkg is the source pkg to download from. 
:: T500 moukup pkg
:: T582 QA pkg
:: T592 Unsigned pkg

set optn=%optn: =%
set nptn=%nptn: =%
set opkg=%opkg: =%
set npkg=%npkg: =%

set url="%drv%\%rel%\%app_var%\%opkg%\*"
echo D|xcopy %url% C:\auto_pkg_build\Sources\Source_pkg\%opkg% /e
echo D|xcopy %url% C:\auto_pkg_build\Workspace\Build_pkg\%opkg% /e
I used %~dp0 to replace the original absolute path from
call C:\auto_pkg_build\Scripts\scheme_replace\get_src.cmd
to
call "%~dp0\get_src.cmd"

Now how I can re-write these two in bold
echo D|xcopy %url% C:\auto_pkg_build\Sources\Source_pkg\%opkg% /e
echo D|xcopy %url% C:\auto_pkg_build\Workspace\Build_pkg\%opkg% /e

Thanks

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: How to set up root directory ?

#6 Post by aGerman » 28 Nov 2017 14:31

say currently "%~dp0" targets "root\Scripts\scheme_replace\" where the script is placed

using "%~dp0.." you target "root\Scripts"
using "%~dp0..\.." you target "root"
using "%~dp0..\..\Sources" you target "root\Sources"
using "%~dp0..\..\Sources\Source_pkg" you target "root\Sources\Source_pkg"

Steffen

goodywp
Posts: 265
Joined: 31 Jul 2017 09:57

Re: How to set up root directory ?

#7 Post by goodywp » 28 Nov 2017 14:55

Hi Steffen,

Thanks a lot! I tried a test using your suggested and
the test.cmd is currently in C:\auto_pkg_build\Scripts\scheme_replace\
Here is the code for test.cmd

Code: Select all

echo "%~dp0.."
echo "%~dp0..\.."
echo "%~dp0..\..\Sources"
echo "%~dp0..\..\Sources\Source_pkg"
here what I got:
"C:\auto_pkg_build\Scripts\scheme_replace\.."
"C:\auto_pkg_build\Scripts\scheme_replace\..\.."
"C:\auto_pkg_build\Scripts\scheme_replace\..\..\Sources"
"C:\auto_pkg_build\Scripts\scheme_replace\..\..\Sources\Source_pkg"

that is not looking right? Is anything I messed up?

To my understanding based upon your explanation, it should looks like these after I ran the test.cmd

"C:\auto_pkg_build\Scripts\"
"C:\auto_pkg_build\"
"C:\auto_pkg_build\Sources"
"C:\auto_pkg_build\Sources\Source_pkg"

Thanks

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: How to set up root directory ?

#8 Post by aGerman » 28 Nov 2017 15:05

I already wrote what it targets. If you want to evaluate the paths you have to use the FOR loop and ~f modifier as in my other example. But that only makes it clear for the ECHO output. It wouldn't be necessary for your CALL, XCOPY, or other commands.

Steffen

Squashman
Expert
Posts: 4484
Joined: 23 Dec 2011 13:59

Re: How to set up root directory ?

#9 Post by Squashman » 28 Nov 2017 21:56

As Steffen has commented, the xcopy command will properly use the dotted paths and expand them correctly. You can test this very easily with the /F and /L switches using XCOPY.

I created a file named file.txt in the folder C:\auto_pkg_build\Scripts\scheme_replace. Using this xcopy command we can test to see how relative paths work.

Code: Select all

C:\auto_pkg_build\Scripts\scheme_replace>xcopy /f /l *.txt ..\
C:\auto_pkg_build\Scripts\scheme_replace\file.txt -> C:\auto_pkg_build\Scripts\file.txt
1 File(s)

C:\auto_pkg_build\Scripts\scheme_replace>xcopy /f /l *.txt ..\..\
C:\auto_pkg_build\Scripts\scheme_replace\file.txt -> C:\auto_pkg_build\file.txt
1 File(s)

C:\auto_pkg_build\Scripts\scheme_replace>xcopy /f /l *.txt ..\..\tools
C:\auto_pkg_build\Scripts\scheme_replace\file.txt -> C:\auto_pkg_build\tools\file.txt
1 File(s)

C:\auto_pkg_build\Scripts\scheme_replace>xcopy /f /l *.txt ..\..\tools\packager
C:\auto_pkg_build\Scripts\scheme_replace\file.txt -> C:\auto_pkg_build\tools\packager\file.txt
1 File(s)

C:\auto_pkg_build\Scripts\scheme_replace>

goodywp
Posts: 265
Joined: 31 Jul 2017 09:57

Re: How to set up root directory ?

#10 Post by goodywp » 29 Nov 2017 15:28

Thanks Squashman!!
I had many commands using the root directory. For now, they can compromise my structure. Maybe later as enhancement....

Thanks

goodywp

Post Reply