Reputation: 289
Hi i am mapping a shell script code to batch script so i came across a statement in shell script like this
BASEDIR=`cd ../;pwd`
But have no idea how to do that. I know that %CD% will give me current directory but i want to set one directory up path in variable . Please help.
Upvotes: 2
Views: 2564
Reputation: 380
This can be also helpful:
:: set script dir to dir from where script is called (with drive letter)
:: for example D:\projects\test\scripts\
set SCRIPT_DIR=%~dp0
:: remove last slash
set SCRIPT_DIR=%SCRIPT_DIR:~0,-1%
:: set PROJECT_DIR as one level up directory (in this example it will be D:\projects\test\)
for %%I in ("%SCRIPT_DIR%") do set PROJECT_DIR=%%~dpI
:: remove last slash from PROJECT_DIR
set PROJECT_DIR=%PROJECT_DIR:~0,-1%
:: after this PROJECT_DIR will be D:\projects\test
Upvotes: 0
Reputation: 82418
A bit more complex as in shell code.
FOR /F "delims=" %%A in ("%CD%\..") do set "basedir=%%~fA"
Edit related to your comment:
To replace \
with \\
just add the line
set "basedir=%basedir:\=\\%"
Upvotes: 4
Reputation: 3122
Consider:
@echo off
set "dir=C:\Windows\System32"
call:up %dir%
:up
echo/%~dp1
Upvotes: 0