2012-03-02 3 views
0

방금 ​​배치 파일을 작성하는 방법을 배우기 시작했으며, 기본 사항 중 일부 또는 대부분을 가지고 있다고 생각하지만,이 점을 이해하지 못하는 것 같습니다.대화 형 배치 파일 문제

사용자가 하위 폴더 이름을 입력하고 현재 디렉터리를 해당 하위 폴더로 변경하도록 배치 파일을 요청할 수 있습니까?

예를 들어, 현재 디렉토리가 C : \ Folder라고 가정하십시오. 이 폴더에는 Sub 1, Sub 2 및 Sub 3 하위 폴더가 있습니다. 사용자가 이러한 폴더 중 하나를 탐색해야하지만 파일을 실행할 때마다 다를 수있는 경우 사용자가 하위 폴더 이름을 선택한 다음 배치 파일에서 현재 디렉토리를 해당 폴더로 변경합니까?

아마 같은 :

CD C:\Folder 

SET /p desiredFolder = Enter the name of the sub-folder you would like to go to: 
Sub 1, Sub 2, or Sub 3 

---User enters Sub 1 and hits enter--- 

CD C:\Folder\%desiredFolder% 

제공 사전에 어떤 도움을 주셔서 감사합니다!

답변

2

당신은 거의 단지 몇 세부 사항이 누락했을 :

1 사용 SET 명령의 형태는 등호 (=) 앞 공백을 포함하지 않는 경우; 폴더 이름에 공백이 포함될 수 있습니다 경우

SET VAR = VALUE 
ECHO %VAR%    show nothing 
ECHO %VAR %    show " VALUE" 

2, 당신이 따옴표로 전체 폴더 이름을 로 묶어야합니다 :

CD C:\Folder 

SET /p desiredFolder=Enter the name of the sub-folder you would like to go to: 
Sub 1, Sub 2, or Sub 3 

---User enters Sub 1 and hits enter--- 

CD "C:\Folder\%desiredFolder%" 
+0

그것은 일했다! 고마워요! – Josh

+0

사실, 'CD'는 경로를 큰 따옴표로 묶을 필요가 없지만 그렇게하는 습관을 가지면 피해를주지는 않습니다. –

2

당신은 시도조차 할 수 그렇지 않으면 변수 이름 에게 공간을 포함 :

@echo off 
:start 
set desiredFolder= 
dir /AD /W /B 
echo.--------------------------------------------------------- 
set /p desiredFolder=Enter the name of the folder you would like to go to: 
echo %CD%\%desiredFolder% 
IF NOT EXIST %CD%\%desiredFolder% echo..\%desiredFolder% does not exist, try again. 
IF NOT EXIST %CD%\%desiredFolder% echo.--------------------------------------------------------- & goto start 

그런 다음 위의 줄 다음 중 하나를 사용하십시오. 하고 싶다

start cmd.exe /K chdir %CD%\%desiredFolder% 
REM if you want to start your command window in a new directory 

cmd.exe /K chdir %CD%\%desiredFolder% 
REM if you want to change directories in your current command window 

start %CD%\%desiredFolder% 
REM if you want to open the folder in windows explorer