批处理文件设置命令在if语句中不起作用 [英] Batch file set command isn't working in if statement

查看:184
本文介绍了批处理文件设置命令在if语句中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于我一生,我无法弄清楚为什么设置提示在 if <中时不起作用的原因/ code>语句:

For the life of me, I can't figure out why the below set prompt won't work when it is in the if statement:

@echo off

REM :askdeletecsvs
if exist *.csv (
    echo Warning! All files in the scripts folder that have the "CSV" extension will be deleted!
    echo Answering "n" will continue the script without deleting the CSVs.
    set /p ASKDELETE=Delete CSVs? (y/n): 
REM     
REM     if ( /i %ASKDELETE% equ "y" goto :deletecsvs )
REM     if ( /i %ASKDELETE% equ "n" goto :runscripts )
REM     goto :askdeletecsvs
)

当我运行cmd窗口上方的批处理文件时,该窗口会打开,然后迅速关闭。如果将 set 行移到 if 语句之外,则提示符将按预期显示。 (从中运行bat文件的文件夹中有 csvs)

When I run the batch file as it is above the cmd window opens and then shuts quickly. If I move the set line outside of the if statement then the prompt shows as expected. (There are csvs in the folder the bat file is running from)

我丢失了什么?

推荐答案

首先,您使用了右括号,该括号过早地结束了 If 括号。

To start with you had used a closing parenthesis which was prematurely ending your opening If parenthesis.

我建议反转思路:

If Not Exist *.csv GoTo runscripts

Echo Warning!
Echo All files in the scripts folder that have the "CSV" extension will be deleted!
Echo Answering "N" will continue the script without deleting the CSVs.
Choice /M "Delete CSVs" 
If ErrorLevel 2 GoTo runscripts

:deletecsvs
Del /F /Q /A "PathTo\scripts\*.csv"
GoTo :EOF

:runscripts

您可以根据需要将 GoTo:EOF 更改为相关的有效标签,或者如果要继续使用:runscripts 。如果批处理文件是从脚本目录运行的,则还可以将 PathTodscripts\ 替换为%〜dp0 ,或删除 PathTo\scripts\ (如果当前目录包含这些文件)。 (请注意,当前目录和批处理文件路径可能不一定相同)

You can change GoTo :EOF to a relevant valid label as necessary or remove it if you want to continue on to :runscripts. You can also replace PathTo\scripts\ with %~dp0 if the batch file is running from the scripts directory, or remove PathTo\scripts\ if the current directory holds those files. (note that the current directory and batch file path may not necessarily be the same)

这篇关于批处理文件设置命令在if语句中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆