第一個錯誤是路線:
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