嗨,我正在将 shell 脚本代码映射到批处理脚本,所以我在 shell 脚本中遇到了这样的语句
BASEDIR=`cd ../;pwd`
但不知道该怎么做。我知道 %CD% 会给我当前目录,但我想在变量中设置一个目录路径。请帮忙。
比 shell 代码更复杂一些。
FOR /F "delims=" %%A in ("%CD%\..") do set "basedir=%%~fA"
编辑与您的评论相关的内容:
要将
\
替换为 \\
,只需添加以下行
set "basedir=%basedir:\=\\%"
另一种方式...
pushd ..
set "BaseDir=%CD%"
popd
for /f "delims=" %%a in ("%cd%") do set "upper_dir=%%~dpa"
考虑:
@echo off
set "dir=C:\Windows\System32"
call:up %dir%
:up
echo/%~dp1
这也很有帮助:
:: 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