2017-12-29 26 views
1

"D : \ xx \ 1 \"디렉토리에 .bat 파일을 만들고 절대 경로 이름을 % ~ dp0으로 가져옵니다. "D : \ xx \ 2 \" "D : \ xx \ 3 \" "D : \ xx \ 4 \"디렉토리의 다른 파일을 배치 스크립트로 상대 경로로 조작 할 수 있습니까?Windows 배치 스크립트로 다른 폴더의 파일을 조작하는 방법

+1

'.. \ 2' 참조 http://stackoverflow.com/questions/41030190/command-to-run-a-bat-file/41049135#41049135 – ACatInLove

답변

1

D:\xx\1\에 정확히와 같이 동일한 이름을 가진, D:\xx\3\, D:\xx\4\, ...를, 등등 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\에 배치 파일이 있다고 가정 할 수있다 같은 내용. 그런 다음 배치 파일은 다음 코드로 시작해야합니다

@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\의 각 숨김이 아닌 하위 디렉토리의 D:\xx\1\에있는 배치 파일을 호출하는 또 다른 변형은 현재 하위 디렉토리를 현재 디렉토리로 만든 것입니다.

@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. 

배치 파일이 드라이브의 루트 디렉토리에 저장되어 있으면 두 배치 파일 모두 주 코드 만 실행합니다.

사용 된 명령과 그 작동 방법을 이해하려면 명령 프롬프트 창을 열고 다음 명령을 실행하고 각 명령에 대해 표시된 모든 도움말 페이지를 모두주의 깊게 읽어야합니다. 이름과 배치 파일의 확장자 - 배치 파일 자체
%~nx0입니다 드라이브와 인수 0의 경로 -

  • call /?은 ...
    %~dp0을 설명합니다.
  • echo /?
  • for /?
  • goto /?
  • if /?
  • popd /?
  • pushd /?
  • rem /?
  • set /?
012,351,