배치파일에서 현재의 드라이브와 경로를 판정하고 해당 경로로 이동하기
게시일: 2002/01/04 | 글쓴이: doa | 7959 번 | 프린트 | 메일
경로가 지정된 폴더에 다음과 같은 배치파일을 생성할 필요하가 있다.
CALL DoADP.BAT은 현재 세션에대해 다음과 같은 환경변수를 설정한다:
DoADP 다음과 같은 형식을 갖는 현재 드라이브와 경로명이다.
Drive:\Directory\SubDirectory1\SubDirectory2\SubDirectoryNDoADRIVE 현재의 드라이브이다. - Drive:DoAPATH 현재의 경로이다. -
\Directory\SubDirectory1\SubDirectory2\SubDirectoryN
현재 드라이브의 루트로 이동 - cd %DoAdrive%\
...Start....DoADP.BAT........................
@echo off
for /f "tokens=*" %%i in ('cd') do set DoADP=%%i
for /f "tokens=1 delims=:" %%i in ('cd') do set DoADRIVE=%%i:
for /f "tokens=2 delims=:" %%i in ('cd') do set DoAPATH=%%i
...End......DoADP.BAT........................
CALL DoATOP.BAT은 현재 세션에대한 DoATOP 환경변수를 현재 드라이브/경로의 최상위 디렉토리로 설정할 것이다. 만약 현재 경로가 드라이브의 루트라면 DoATOP는 Drive:\가 되며 그렇지 않으면 Drive:\RootDirectory가 될 것이다.
...Start....DoATOP.BAT........................
@echo off
for /f "tokens=1 delims=:" %%i in ('cd') do set DoATOP=%%i:\
for /f "tokens=2 delims=\" %%i in ('cd') do set DoATOP=%DoATOP%%%i
...End......DoATOP.BAT........................
CALL DoAUPLVL은 현재 세션에대한 DoAUP 환경변수를 현재 드라이브/경로의 친 디렉토리로 설정한다.
...Start....DoAUPLVL.BAT........................
@echo off
call DoAdp
call DoAtop
set DoAup=%DoAtop%
if "%DoAdp%"=="%DoAtop%" set DoAup=%DoAdrive%\
if "%DoAup%"=="%DoAdrive%\" goto end
call DoAPARSE 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
:end
...End......DoAUPLVL.BAT........................
...Start....DoAPARSE.BAT........................
:loop
set DoAupw=%DoAup%
if "%DoAdrive%\"=="%DoAup%" goto end
for /f "tokens=%1* delims=\" %%i in ('cd') do call DoAparm %%i %%j
shift
if "%DoAupw%"=="%DoAup%" goto end
if not "%1"=="" goto loop
:end
set DoAupw=
...End......DoAPARSE.BAT........................
...Start....DoAPARM.BAT........................
@echo off
if "%2"=="" goto end
set DoAup=%DoAup%\%1
:end
...End......DoAPARM.BAT........................
해당 디렉토리로 이동하기 위해 DoAWALK \Directory\YourBAT.BAT을 호출한다.
YourBAT.BAT은 원하는 것을 수행한다.
...Start....DoAWALK.BAT........................
@echo off
call DoAdp
:loop
call DoAuplvl
cd %DoAup%
call %1
if "%DoAup%"=="%DoAdrive%\" goto end
goto loop
:end
...End......DoAWALK.BAT........................