Swapnil1988
Swapnil1988

Reputation: 289

Batch: Set one directory up path in variable

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

Answers (5)

Evgeny Cheryomushkin
Evgeny Cheryomushkin

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

jeb
jeb

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

npocmaka
npocmaka

Reputation: 57332

for /f "delims=" %%a in ("%cd%") do set "upper_dir=%%~dpa"

Upvotes: 2

RGuggisberg
RGuggisberg

Reputation: 4750

Another way...

pushd ..
set "BaseDir=%CD%"
popd

Upvotes: 2

Rafael
Rafael

Reputation: 3122

Consider:

@echo off
set "dir=C:\Windows\System32"
call:up %dir%
:up
echo/%~dp1

Upvotes: 0

Related Questions