.bat 文件覆盖答案

.bat 文件覆盖答案

当我制作这个批处理文件时,第一次输入 y 和 n 时,它工作正常,但是只要我选择 n,每次我尝试输入某些内容时,它都会打开 7000 首歌曲 wpl 列表,然后关闭它并将其替换为 Rick Astley...

帮助!

@echo off
:Ask
echo Would you like to listen to the best songs out of the 7000 I have?(Y/N)
set INPUT=
set /P INPUT=Type input: %=%
If /I "%INPUT%"=="y" goto yes 
If /I "%INPUT%"=="n" goto lolno
echo Incorrect input & goto Ask
:yes
start c:/Users/MyName/Music/Playlists/"The Best of the 7000 songs that I have.wpl"
:lolno
start c:/Users/MyName/Music/Downloads/Music/"Various Artists"/"The Number One 80's Album Disc 2"/"06 Never Gonna Give You Up.mp3"

答案1

start使用遇到的第一个引用参数作为窗口标题创建新cmd会话。如果您想要start任何包含空格的内容,则必须按如下方式进行:

start "" "%UserProfile%\Music\Playlists\The Best of the 7000 songs that I have.wpl"

此外,cmd在将路径作为内置命令的参数处理时,请专门使用反斜杠。Windows API 足够智能,可以为您翻译斜杠,但cmd用于/开关和命名参数时,其解析器有时会感到困惑,因此最好避免使用它。

答案2

标签的工作方式是先转到该标签,然后继续向下。以您的示例为例:

:yes
start c:/Users/MyName/Music/Playlists/"The Best of the 7000 songs that I have.wpl"
:lolno
start c:/Users/MyName/Music/Downloads/Music/"Various Artists"/"The Number One 80's Album Disc 2"/"06 Never Gonna Give You Up.mp3"

goto yes将运行(跳至:yes并继续向下):

:yes
start c:/Users/MyName/Music/Playlists/"The Best of the 7000 songs that I have.wpl"
:lolno
start c:/Users/MyName/Music/Downloads/Music/"Various Artists"/"The Number One 80's Album Disc 2"/"06 Never Gonna Give You Up.mp3"

goto lolno将运行(跳至:lolno并继续向下):

:lolno
start c:/Users/MyName/Music/Downloads/Music/"Various Artists"/"The Number One 80's Album Disc 2"/"06 Never Gonna Give You Up.mp3"

您需要做的是在您不想遇到的任何标签段末尾添加goto :eof(eof 表示“文件结束”)或:exit /b

:yes
start c:/Users/MyName/Music/Playlists/"The Best of the 7000 songs that I have.wpl"
goto :eof

:lolno
start c:/Users/MyName/Music/Downloads/Music/"Various Artists"/"The Number One 80's Album Disc 2"/"06 Never Gonna Give You Up.mp3"
goto :eof

任何命令都会在那里结束脚本。如果您愿意,您还可以在它们下面定义一个不同的标签来跳转到该标签。

相关内容