在批处理脚本上添加“是”或“否”以删除选项

时间:2011-12-01 23:49:30

标签: batch-file

我已经写过这个脚本,如果我可以让它像Y一样删除,而N则不删除文件但是我不知道怎么去添加它。

@ECHO OFF
SETLOCAL EnableDelayedExpansion

REM Set variables
SET SOURCE=C:\My Recordings
SET DEST=\\XXXXXXXX\c$\VideoArchive
SET 7ZIP=C:\Program Files\7-Zip\7z.exe

REM Compress local files with 7zip
ECHO ---------------------------------------------------------
ECHO    BEGINNING VIDEO COMPRESSION OPERATIONS
ECHO ---------------------------------------------------------
CD /D "%SOURCE%"
FOR %%f in ("*.wrf") DO (
    SET FILENAME=%%~nf
    ECHO Compressing !FILENAME!
    "!7ZIP!" a -t7z -aoa "!FILENAME!.7z" "%%f"
)

REM Copy compressed files
ECHO ---------------------------------------------------------
ECHO    COMPRESSION COMPLETE - BEGINNING COPY OPERATIONS
ECHO ---------------------------------------------------------
for /F "usebackq delims=" %%a in (`dir /b "%SOURCE%\*.7z"`) do (
    SET CURRENT_FILE=%%a
    SET CURRENT_FILE_NO_EXTENSION=%%~na

    REM Extract the month.
    for /F "usebackq tokens=3 delims=-" %%i in ('!CURRENT_FILE!') do (
        SET CURRENT_FILE_DATE=%%i
        SET FILE_MONTH=!CURRENT_FILE_DATE:~5,2!
        SET MONTH_DEST=!DEST!\!FILE_MONTH!
    )

    XCOPY "%SOURCE%\!CURRENT_FILE!" "!MONTH_DEST!\" /Y /V /I /R

    REM Confirm successful copy, then delete original
    IF !ERRORLEVEL! EQU 0 (
        ECHO Copy Operation Successful. Removing Original...
        DEL /Q "%SOURCE%\!CURRENT_FILE_NO_EXTENSION!.wrf"
        DEL /Q "%SOURCE%\!CURRENT_FILE!"
    )ELSE (
        ECHO Error while copying "%SOURCE%\!CURRENT_FILE!.
    )
)

这是我的代码,如果有人可以帮助那将是伟大的。

2 个答案:

答案 0 :(得分:4)

使用choice,并根据回复采取行动。

要为用户提供“删除文件?”选项,请使用

choice /c yn /n /m "Delete files?"

ERRORLEVEL将包含所选值的索引(例如,在上面的示例中,如果按下1则为Y2N'). Your batch file can then check ERRORLEVEL`并做出相应的反应。

虽然其他答案提到/y的{​​{1}}参数,但del是通用的,可以出于任何原因与任何批处理文件一起使用。

答案 1 :(得分:0)

set /p DELETE=Delete [y/n]?
if !DELETE! == y (
    ...
)