我在目录" D:\ xx \ 1 \"中创建一个.bat文件并通过%~dp0获取其绝对路径名。我可以在目录中操作一些其他文件" D:\ xx \ 2 \"" D:\ xx \ 3 \"" D:\ xx \ 4 \& #34; ...通过我的批处理脚本的相对路径?
答案 0 :(得分:1)
可以使用call "%~dp0..\2\other.bat"
和call "%~dp0..\3\other.bat"
等,请参阅How to call a batch file that is one level up from the current directory?
但是,我们假设D:\xx\2\
,D:\xx\3\
,D:\xx\4\
,...中有一个批处理文件,其名称与D:\xx\1\
中的名称相同,内容完全相同。然后批处理文件应该以下面的代码开始:
@echo off
if "%CalledByBatch%" == "1" goto MainCode
rem Determine path to parent directory of the batch file by getting path
rem of the batch file with a backslash at end and removing this backslash.
set "ParentDirectory=%~dp0"
set "ParentDirectory=%ParentDirectory:~0,-1%"
rem The batch file is stored in root of a drive if the path to this batch
rem file ends now with a colon. In this case just execute the main code.
if "%ParentDirectory:~-1%" == ":" set "ParentDirectory=" & goto MainCode
rem Get path to parent directory of the batch file with a backslash at end.
for /F "delims=" %%I in ("%ParentDirectory%") do set "ParentDirectory=%%~dpI"
rem Define marker that this batch file is called by itself from same or
rem a different subdirectory of the parent directory of the batch file.
set "CalledByBatch=1"
rem For each non hidden subdirectory in parent directory of this batch file
rem check if the subdirectory contains also a batch file with same name as
rem this batch file. Make this directory the current directory if this
rem condition is true, call the batch file now executing main code and
rem restore initial current directory.
for /D %%I in ("%ParentDirectory%*") do if exist "%%I\%~nx0" (
pushd "%%I"
call "%~nx0"
popd
)
rem Delete the environment variable and exit processing of this batch file.
set "CalledByBatch="
goto :EOF
:MainCode
echo Running %~nx0 in "%CD%" ...
rem Here is the main code executed on each directory.
我不知道为什么每个子目录应该有相同的批处理文件,因为在D:\xx\
中有一个处理所有子目录中的所有文件的批处理文件会更有意义。但是,如果不知道已经使用过的批处理文件的代码来处理D:\xx\2\
中的文件,那么就不可能建议如何重写代码。
另一个变种,它在将当前子目录作为当前目录后,在D:\xx\1\
中的每个非隐藏子目录中调用D:\xx\
中的批处理文件。
@echo off
if not "%BatchFile%" == "" goto MainCode
rem Determine path to parent directory of the batch file by getting path
rem of the batch file with a backslash at end and removing this backslash.
set "ParentDirectory=%~dp0"
set "ParentDirectory=%ParentDirectory:~0,-1%"
rem The batch file is stored in root of a drive if the path to this batch
rem file ends now with a colon. In this case just execute the main code.
if "%ParentDirectory:~-1%" == ":" set "ParentDirectory=" & goto MainCode
rem Get path to parent directory of the batch file with a backslash at end.
for /F "delims=" %%I in ("%ParentDirectory%") do set "ParentDirectory=%%~dpI"
rem Define marker that this batch file is called by itself from same or
rem a different subdirectory of the parent directory of the batch file.
set "BatchFile=%~f0"
rem For each non hidden subdirectory in parent directory of this batch
rem file call exactly this batch file for executing the main code.
for /D %%I in ("%ParentDirectory%*") do if exist "%%I\%~nx0" (
pushd "%%I"
call "%BatchFile%"
popd
)
rem Delete the environment variable and exit processing of this batch file.
set "BatchFile="
goto :EOF
:MainCode
echo Running %~nx0 in %CD% ...
rem Here is the main code executed on each directory.
如果批处理文件存储在驱动器的根目录中,则两个批处理文件都只运行主代码。
要了解使用的命令及其工作原理,请打开命令提示符窗口,执行以下命令,并完全阅读为每个命令显示的所有帮助页面。
call /?
...解释了
%~dp0
- 参数0的驱动器和路径,它是批处理文件本身和%~nx0
- 批处理文件的名称和扩展名。echo /?
for /?
goto /?
if /?
popd /?
pushd /?
rem /?
set /?