简体   繁体   中英

%a and %%a suddenly don't work in Win 10 cmd and batch file respectively?

I was trying to batch convert a folder of .flac to .mp3 using FFMPEG, but when i ran my batch file I got the "%F is unexpected at this time" error, even though i used "%%F" in the batch file. To test what was really the problem, i proceeded to type variations of %F into cmd directly, but could not get it to work either. Did something change in windows 10 regarding the use of variable names in the for loop?

cmd commands I tried:

C:\Users\yt\Music\Joe Hisaishi (Classical Guitar) - Copy>for "%F" in     (*.flac) echo %F
"%F" was unexpected at this time.

C:\Users\yt\Music\Joe Hisaishi (Classical Guitar) - Copy>for %F in     (*.flac) echo %F
echo was unexpected at this time.

C:\Users\yt\Music\Joe Hisaishi (Classical Guitar) - Copy>for "%%F" in     (*.flac) echo %%F
"%%F" was unexpected at this time.

C:\Users\yt\Music\Joe Hisaishi (Classical Guitar) - Copy>for "%f" in     (*.flac) echo %f
"%f" was unexpected at this time.

C:\Users\yt\Music\Joe Hisaishi (Classical Guitar) - Copy>for "%g" in     (*.flac) echo %g
"%g" was unexpected at this time.

C:\Users\yt\Music\Joe Hisaishi (Classical Guitar) - Copy>for "%g" in     ("*.*") echo %g
"%g" was unexpected at this time.

C:\Users\yt\Music\Joe Hisaishi (Classical Guitar) - Copy>for "%g" in     ("*.*") do echo %g
"%g" was unexpected at this time.

original batch file for FFMPEG

cd "\Users\yt\Music\Joe Hisaishi (Classical Guitar) - Copy"
echo in directory "%cd%"
pause
for "%%F" in ("*.flac") do (
echo converting "%%F"
"C:\Users\yt\Downloads\OtherApps\FFMPEG\bin\ffmpeg.exe" -i "%%F"     -codec:a libmp3lame -qscale:a 2 "%%~nF.mp3"
echo del "%%F"
)
cd "%~dp0"

Don't use

FOR "%F" ...

on the command line but

FOR %F ...

In a script you have to use %% instead of % :

FOR %%F ...

but not

FOR "%%F" ..

Same principle for "%G" ( "%%G" ), use %G ( %%G ) instead

Use the following batch code:

pushd "%USERPROFILE%\Music\Joe Hisaishi (Classical Guitar) - Copy"
echo In directory "%cd%"
pause
for %%F in ("*.flac") do (
    echo Converting "%%F" ...
    "%USERPROFILE%\Downloads\OtherApps\FFMPEG\bin\ffmpeg.exe" -i "%%F" -codec:a libmp3lame -qscale:a 2 "%%~nF.mp3"
    echo del "%%F"
)
popd

The main mistake is the definition of the loop variable F in double quotes by using "%%F" which is not right. The loop variable must be always defined without enclosing it in double quotes. Just on referencing the value of the loop variable in command or command block executed on each found file the double quotes should be used in case of space or &()[]{}^=;!'+,`~ in file name.

It is better to use value of environment variable USERPROFILE instead of hard coding the folder paths with your user name. See Wikipedia article about Windows Environment Variables .

The usage of the commands pushd and popd are better to switch current directory and later restore it than using command cd twice.

The deletion of the converted *.flac file is not done by the batch file above because of command echo before for testing purposes.

But it would be possible to write this batch code without switching the current directory at all.

for %%F in ("%USERPROFILE%\Music\Joe Hisaishi (Classical Guitar) - Copy\*.flac") do (
    echo Converting "%%F" ...
    "%USERPROFILE%\Downloads\OtherApps\FFMPEG\bin\ffmpeg.exe" -i "%%F" -codec:a libmp3lame -qscale:a 2 "%%~dpnF.mp3"
    if not errorlevel 1 del "%%F"
)

This batch code deletes now the *.flac file after successful conversion of the file to MP3 format.

Note: I don't have ffmpeg.exe installed and therefore don't really know if it exits with 0 on success and with a value greater 0 on an error.

For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.

  • del /?
  • echo /?
  • for /?
  • if /?
  • popd /?
  • pushd /?

And read also the Microsoft support article Testing for a Specific Error Level in Batch Files .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM