我刚刚开始尝试学习编写批处理文件,我想我已经掌握了一些或大部分基础知识,但我似乎无法弄清楚这一点。
是否可以让批处理文件要求用户输入子文件夹名称,然后将当前目录更改为该子文件夹?
例如,假设我当前的目录是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%
提前感谢您提供的任何帮助!
答案 0 :(得分: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%"
答案 1 :(得分: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