需要ENABLEDELAYEDEXPANSION的批处理脚本,但在哪里以及如何?

时间:2015-05-03 17:15:06

标签: batch-file

以下是我正在测试的批处理脚本的一部分,但是底部的第二个IF语句不起作用。我有信心我需要ENABLEDELAYEDEXPANSION,但我不知道在哪里以及如何。

FOR /F "delims==" %%B IN ("Filename_example.m4v") DO (
    FOR /F "tokens=1 delims=." %%C IN ("%%B") DO SET name=%%C
    FOR /F "tokens=2 delims=." %%D IN ("%%B") DO (
        IF "%%D" EQU "m4v" SET ext=mp4
        IF "%%D" EQU "m3u8" SET ext=mp4
        IF "%%D" EQU "wmv" SET ext=wmv
    )
)

ECHO Filename: %name%.%ext%
SET /P name1=Rename file (Y/N) {Y}?
IF /I "%name1%" EQU "N" SET filename=%name%.%ext%
IF /I "%name1%" NEQ "N" (
    ECHO New filename:
    SET /P name2=
    SET filename=%name2%.%ext%
)

ECHO %filename%

4 个答案:

答案 0 :(得分:2)

要启用延迟扩展,请使用setlocal命令。

setlocal enableDelayedExpansion
FOR /F "delims==" %%B IN ("Filename_example.m4v") DO (
    FOR /F "tokens=1 delims=." %%C IN ("%%B") DO SET name=%%C
    FOR /F "tokens=2 delims=." %%D IN ("%%B") DO (
        IF "%%D" EQU "m4v" SET ext=mp4
        IF "%%D" EQU "m3u8" SET ext=mp4
        IF "%%D" EQU "wmv" SET ext=wmv
    )
)

ECHO Filename: %name%.%ext%
SET /P name1=Rename file (Y/N) {Y}?
IF /I "%name1%" EQU "N" SET filename=%name%.%ext%
IF /I "%name1%" NEQ "N" (
    ECHO New filename:
    SET /P name2=
    SET filename=!name2!.!ext!
)

ECHO %filename%

据我所知,您只需要在最后SET上使用delayedExpansion。要使用延迟展开,您需要!而不是%

答案 1 :(得分:2)

您可以使用延迟扩展(请参阅npocmaka的回答)或保持相同的行为

for %%B in ("Filename_example.m4v") do (
    set "name=%%~nB"
    set "ext=%%~xB"
    if /i "%%~xB"==".m4v"  set "ext=.mp4"
    if /i "%%~xB"==".m3u8" set "ext=.mp4"
)

echo Filename: %name%%ext%
set /p "rename=Renamefile (Y/N) {Y}?"
if /i not "%rename%"=="N" (
    set /p "filename=New filename"
) else (
    set "filename=%name%%ext%
)

echo %filename%

答案 2 :(得分:1)

下一个脚本显示

此外,最小化用户对唯一set /P的输入。

@ECHO OFF >NUL
SETLOCAL enableextensions

FOR %%B IN (
    "FN_example.m4v"
    "fn_example2.WMV"
    "fn_example3.xyz"
    ) DO (
  set "name=%%~nB"
  set "ext=%%~xB"
  IF /I "%%~xB" EQU ".m4v"  SET "ext=.mp4"
  IF /I "%%~xB" EQU ".m3u8" SET "ext=.mp4"
  IF /I "%%~xB" EQU ".wmv"  SET "ext=.wmv"

  SETLOCAL enabledelayedexpansion
  ECHO orig. filename: !name!!ext!
  SET /P "name=Type new filename (or hit <Enter> to keep '!name!'): "
  SET "filename=!name!!ext!"
  IF /I "!name!" EQU "%%~nB" (
      ECHO name unchanged: !filename!
  ) else (
      ECHO new file name: !filename!
  )
  ENDLOCAL
  echo(
)

答案 3 :(得分:0)

到目前为止,感谢您的所有帮助。我有一个工作脚本。我确信事情可能更有效率,但至少它起作用了! :)对于荷兰语的使用,很抱歉,代码可能仍然有用:

@ECHO off
CLS

REM BatchGemist geschreven door Reino Wijnsma, 2015 (reino@degeelebosch.nl)

SET batchname=BatchGemist
SET version=1.0
TITLE %batchname% %version%

IF NOT EXIST youtube-dl.exe (
    ECHO 'youtube-dl.exe' niet gevonden.
    ECHO.
    GOTO Help
)
IF NOT EXIST ffmpeg.exe (
    ECHO 'ffmpeg.exe' niet gevonden.
    ECHO.
    GOTO Help
)

FOR /F %%A IN ('youtube-dl.exe --version') DO ECHO (youtube-dl %%A)
ECHO Voer "?" in voor Hulp
ECHO.

:Input
ECHO Voer npo.nl, rtlxl.nl, kijk.nl, of youtube programma-link in:
SET url=
SET /P url=
IF "%url%"=="?" GOTO Help
IF "%url%"=="U" GOTO Update
:: stop script bij geen Input (enter-toets)
IF "%url%"=="" GOTO:eof
:: spaties in Input niet toestaan
IF NOT "%url: =%"=="%url%" (
    ECHO.
    ECHO Spaties in programma-link niet toegestaan.
    ECHO.
    GOTO Input
)
:: alleen bepaalde websites als input toestaan
IF NOT "%url:http://www.npo.nl=%"=="%url%" GOTO Input2
IF NOT "%url:http://www.rtlxl.nl=%"=="%url%" GOTO Input2
IF NOT "%url:http://www.kijk.nl=%"=="%url%" GOTO Input2
IF NOT "%url:http://www.youtube.com=%"=="%url%" GOTO Input2
IF NOT "%url:https://www.youtube.com=%"=="%url%" GOTO Input2
IF NOT "%url:http://youtu.be=%"=="%url%" GOTO Input2
IF NOT "%url:https://youtu.be=%"=="%url%" GOTO Input2
ECHO.
ECHO Ongeldige programma-link.
ECHO.
GOTO Input

:Input2
ECHO.
youtube-dl.exe --no-warnings -F "%url%"
ECHO.
SET "format="
SET /P "format=Voer gewenst formaat in {best}: "
IF "%format%"=="" SET "format=best"

ECHO.
SET /P "task=Video-link achterhalen (J), of Downloaden (N) {J}? "
IF /I "%task%"=="N" GOTO :Download

:: voer youtube-dl uit en kopiëer video-link zonder eol-character naar het klembord
FOR /F %%B IN ('youtube-dl.exe --no-warnings -gf "%format%" "%url%"') DO (
    ECHO.
    ECHO Video-link:
    ECHO %%B
    ECHO|SET /P ="%%B"|clip.exe
    ECHO.
    ECHO Video-link gekopi‰erd naar het klembord.
    ECHO.
    ECHO.
)
    :: ‰ (Alt+0137, ANSI CP-1252) wordt in cmd.exe als ë (Alt+137, OEM CP-437 en CP-850) weergegeven
    :: https://en.wikipedia.org/wiki/Code_page_437
    :: https://en.wikipedia.org/wiki/Code_page_850
    :: https://en.wikipedia.org/wiki/Windows-1252
GOTO Input

:Download
ECHO.
ECHO Doelmap: %~dp0
SET /P "remap=Wijzigen (J/N) {J}? "
IF /I "%remap%"=="N" (
    SET "map=%~dp0"
) ELSE (
    SET /P "map=Opslaan in: "
)
IF NOT "%map:~-1%"=="\" SET "map=%map%\"

FOR /F "delims==" %%C IN ('youtube-dl.exe --no-warnings --get-filename -f "%format%" "%url%"') DO (
    SET "name=%%~nC"
    IF /I "%%~xC"==".m4a"  SET "ext=.m4a"
    IF /I "%%~xC"==".m4v"  SET "ext=.mp4"
    IF /I "%%~xC"==".m3u8" SET "ext=.mp4"
    IF /I "%%~xC"==".wmv"  SET "ext=.wmv"
)
ECHO.
ECHO Bestandsnaam: %name%%ext%
SETLOCAL ENABLEDELAYEDEXPANSION
SET /P "rename=Wijzigen (J/N) {J}? "
IF /I "%rename%"=="N" (
    SET "filename=%name%%ext%"
) ELSE (
    SET /P "rename2=Nieuwe bestandsnaam: "
    SET "filename=!rename2!%ext%
)

ECHO.
SET /P "part=Fragment downloaden (J/N) {N}? "
IF /I "%part%"=="J" (
    SET /P "ss=Voer begintijd in (in seconden, of in uu:mm:ss[.xxx] vorm): "
    SET /P "t=Voer tijdsduur in (in seconden, of in uu:mm:ss[.xxx] vorm): "
    ECHO.
    FOR /F %%D IN ('youtube-dl.exe --no-warnings -gf "%format%" "%url%"') DO (
        ffmpeg.exe -hide_banner -ss "!ss!" -i "%%D" -c copy -bsf:a aac_adtstoasc -t "!t!" "%map%%filename%"
    )
) ELSE (
    ECHO.
    FOR /F %%E IN ('youtube-dl.exe --no-warnings -gf "%format%" "%url%"') DO (
        ffmpeg.exe -hide_banner -i "%%E" -c copy -bsf:a aac_adtstoasc "%map%%filename%"
    )
)
ENDLOCAL
ECHO.
ECHO.
GOTO Input

:Update
youtube-dl.exe -U
ECHO.
ECHO.
GOTO Input

:Help
ECHO.
ECHO   H U L P
ECHO.
GOTO Input