第一个错误是路线:
start %user%\Downloads\Songs\"Song name 1.mp3"
一般与路径的整个文件名应该用双引号,而不仅仅是像只有文件名部分。在命令提示符窗口cmd /?
中运行,并仔细阅读所有输出帮助页。
而且因为它可以通过在命令提示符窗口中start /?
运行读取,在双引号中的第一个字符串是通过命令START作为可选标题这往往需要显式地指定一个标题解释,哪怕只是一个空标题,即只指定""
。因此,正确的命令行是:
start "" "%user%\Downloads\Songs\Song name 1.mp3"
下一页不使用的算术表达式的数指定给一个环境变量,这意味着不使用set /a x=248
。最好使用set "x=248"
。
环境变量始终为字符串类型。无法创建整数类型的环境变量。对于行set /a x=248
,Windows命令处理器首先将字符串248
转换为使用C函数strtol的整数,然后将该数字转换回字符串以将其分配给环境变量x
。将数字字符串直接作为字符串分配给环境变量当然更有效。
此外,建议将指令SET的参数括起来,即variable=string
也总是用双引号括起来。有关说明见这意味着在批处理代码片段上How to set environment variables with spaces?和Why is no string output with 'echo %var%' after using 'set var = text' on command line?
的答案:在命令提示符窗口set /?
set "song1=Song name 1"
set "song2=Song name 2"
set "song3=Song name 3"
set "song4=Song name 4"
set "song5=Song name 5"
set /p "songID=Enter song ID: "
运行和读取所有输出帮助页面。你可以阅读关于delayed expansion的东西。当在命令块中定义或修改环境变量时始终需要延迟环境变量扩展,该命令块从(
开始,并以匹配)
结尾,并且环境变量的值在同一个命令块中被引用。
的原因是,与%VariableName%
从第一(
匹配)
所有环境变量的引用之前甚至在执行该命令通常是任一命令FOR或IF立即被参考环境变量的当前字符串值代替。并且这个预处理的命令块不会被进一步更新。
此变量扩展行为可以通过
- 除去批处理文件,所有
@echo off
或echo off
观看注释他们与命令REM或修改off
到on
,
- 打开命令提示窗口,
- 从命令提示符窗口内运行批处理文件,无论是从包含批处理文件的目录还是通过输入具有完整路径的批处理文件的文件名并且st也使用双引号括起来的文件扩展名。
现在与回声模式命令提示符窗口中运行该批处理文件中启用,而不是双击它与回声模式下禁用,可以看出什么预处理每个命令后Windows命令处理器真正执行行分别命令块。并且在出现错误时也可以看到错误消息,因为即使Windows命令处理器遇到语法错误,控制台窗口仍保持打开状态。
这是甚至高级批处理文件编码器在开发过程中用于调试批处理代码的方法。
虽然有问题的代码段也没有什么帮助重现该问题,我认为没有什么是输出由echo Now playing %song1%
作为线路set song1=Song name 1
是在上面(
开始的任意位置和结束下面的任何地方)
相同的命令块,并因此延迟扩展是需要。
在命令提示符窗口中运行setlocal /?
和endlocal /?
以获得有关使用延迟扩展时所需的这两个命令的帮助。
由于在阅读本答案时可以看到,要获得批处理文件中使用的命令的帮助,只需在命令提示符窗口中使用参数/?
运行该命令即可。建议使用内置的帮助来开发批处理文件。
最后,我会建议将文件名放入一个文本文件(播放列表文件),并使用命令FOR来处理它们(输出,选择一个播放等)。通过在命令提示符窗口for /?
中运行,详细了解命令FOR。
使用歌曲列表文件对播放器进行编码的演示代码。
@echo off
setlocal EnableExtensions EnableDelayedExpansion
set "SongsListFile=C:\Temp\Song List.txt"
set "SongFileMissing=0"
if not exist "%SongsListFile%" goto ListFileError
cls
echo Pick a song to start it off.
echo.
echo 0 ... play all songs
rem Output all songs from song list without path and file extension.
rem The song number is right aligned with 3 characters width which
rem means working for song numbers 1 to 999.
set "SongNumber=0"
for /F "usebackq delims=" %%I in ("%SongsListFile%") do (
set /A SongNumber+=1
set "SongOption= !SongNumber!"
set "SongOption=!SongOption:~-3!"
echo !SongOption! ... %%~nI
)
echo.
rem Define as default value 0 for playing all songs in case of
rem user just hits key RETURN or ENTER and prompt user for number.
set "SongSelected=0"
set /P "SongSelected=Enter song number (default: %SongSelected%): "
echo.
rem Play all songs if a string was entered by the user not consisting
rem of only digits, i.e. the entered string is not a number.
for /F "delims=" %%I in ("!SongSelected!") do goto PlayAll
rem Numbers with leading 0s could be interpreted as octal numbers by
rem Windows command processor and therefore better make sure that the
rem number string does not have leading zeros. On a number like 0, 00,
rem 000, ... play all songs.
:RemoveLeadingZeros
if not "%SongSelected:~0,1%" == "0" goto CheckMaxNumber
set "SongSelected=%SongSelected:~1%"
if not "%SongSelected%" == "" goto RemoveLeadingZeros
goto PlayAll
rem Windows command processor interprets numbers as signed 32-bit integers
rem which limits the range from -2147483648 to 2147483647 whereby only the
rem positive range is of interest here at all. The entered number string
rem could be longer than what Windows command processor supports. Every
rem number with more than 9 digits is interpreted as invalid which reduces
rem the valid number range here from 1 to 999999999 which should be always
rem enough. And of course the entered number should not be greater than
rem the number of song file names in songs list.
:CheckMaxNumber
if not "%SongSelected:~9,1%" == "" goto PlayAll
if %SongSelected% GTR %SongNumber% goto PlayAll
rem For playing a single song skip all lines above the line with the
rem song file name to play, play the selected song file and then exit
rem the loop without processing the other lines in songs list file.
rem skip=0 results in a syntax error and therefore it is necessary to
rem define the entire skip option as an environment variable if needed
rem because any other than first song in songs list file should be played.
set "SkipOption="
set /A SongSelected-=1
if not "%SongSelected%" == "0" set "SkipOption= skip=%SongSelected%"
if not exist "%SongsListFile%" goto ListFileError
for /F "usebackq%SkipOption% delims=" %%I in ("%SongsListFile%") do (
call :PlaySong "%%~fI"
goto EndPlayer
)
rem Output error message if songs list file does not exist
rem checked every time before reading the lines from this file.
:ListFileError
echo Error: The songs list file "%SongsListFile%" does not exist.
set "SongFileMissing=1"
goto EndPlayer
rem This is a subroutine embedded in the player's batch file used
rem to play the song passed to the subroutine with first argument.
:PlaySong
if not exist "%~1" (
echo Error: Song file "%~1" does not exist.
set "SongFileMissing=1"
goto :EOF
)
echo Playing now song %~n1 ...
rem Insert here the code used to play the song.
rem This command results in exiting subroutine and processing of batch
rem file continues on the line below the line calling PlaySong.
goto :EOF
:PlayAll
if not exist "%SongsListFile%" goto ListFileError
for /F "usebackq delims=" %%I in ("%SongsListFile%") do call :PlaySong "%%~fI"
:EndPlayer
echo.
if not "%SongFileMissing%" == "0" (
endlocal
pause
) else endlocal
阅读这是从1开始的命令REM行的意见,尤其是行
rem Insert here the code used to play the song.
此行必须通过代码来播放歌曲文件"%~1"
所取代。
歌曲列表文件C:\Temp\Song List.txt
可以包含具有或不具有路径的歌曲文件的名称。例如:
First song.mp3
C:\Temp\Another song.mp3
Album 1 - Last song .mp3
对于理解使用的命令以及它们如何工作,打开命令提示符窗口中,执行有下面的命令,并阅读完全针对每个命令非常仔细地显示所有帮助页面。
call /?
cls /?
echo /?
endlocal /?
for /?
goto /?
if /?
pause /?
rem /?
set /?
setlocal /?
感谢** ** ** _你_ **对我的代码,你的批判视野。正如你所提到的,我提供的代码不够充足,那么我会说这已经足够了,我在发布之前测试了我的自我,以确保我得到了同样的问题。我将在命令提示符下运行thoose命令,并使用/?并仔细阅读**一切**。我知道这可能有很多要问,但是请你回答一下如何解释你解释的最后一部分?关于文件名和txt文件和** FOR **命令的一个!如果你这样做,那么谢谢你一千次:) @Mofi -_tsgsOFFICIAL_ – tsgsOFFICIAL