我的第一个问题是关于一个简单的批处理文件,这让我有点尴尬,但是我对这个主题的了解非常有限。
我正在编写一个简单的批处理脚本来将一些数据从a复制到b。出于这个原因,我希望根据当前的月份创建目标文件夹,并检查该文件夹是否已经存在。
我无法确定为什么我的代码在第14行不按键后抛出任何东西。我还尝试用批处理代码验证工具(BatCodeCheck“相当过时”)验证代码。但是,它不会抛出与我的问题有关的错误或警告。
有关守则:
@echo off
echo Getting current month...
set month=%date:~3,2%
if %month:~0,1% == 0 (
set /A month=%month:~1,2%-1
) ELSE (
set /A month=%month%-1
)
if [%month:~1,2%] == [] (
set month=0%month%
)
echo "%month% is the month before"
echo Checking for monthly folder...
pause
if exist %~dp0%month%\ (
echo "Folder already exists. Press y to overwrite"
pause
set /p Input=Overwrite? (y/n):
if /I "%Input%"NEQ"y" (
EXIT 0
)
) ELSE (
echo "Folder doesn't exist already. Creating..."
mkdir %~dp0%month%\
)
支票日志:
Time : 2022-05-12 19:10:08
Program : BatCodeCheck, Version 0.38
Arguments : D:\RunBackup.bat /L /W
File name : "D:\RunBackup.bat"
File date : 2022-05-12 19:10:02
File encoding : ASCII
Tests : ABELMSUV
A = command line Arguments for batch commands
B = Best practice tips
E = Environment variables
L = Labels
M = common Mistakes
S = verbose Summary (variables, labels, subroutines)
U = Undefined environment variables
V = Vulnerabilities
RISKY CODE:
Line 18: SET /P could make your code vulnerable to exploits (see http://www.robvanderwoude.com/battech_inputvalidation.php#SetP)
SUMMARY:
1 line generated a warning and should be examined
Note that some warnings are only displayed once. Correct the errors and run this test again.
希望这不是语法错误我错过了..。
发布于 2022-05-12 18:11:37
当您使用执行批处理的点击率和咯咯笑方法时,如果找到语法错误或脚本运行到完成,批处理窗口将关闭。您可以在语句之后放置一个pause
,然后返回错误,但最好是打开“命令提示符”,然后从那里运行批处理,以便窗口保持打开状态,并显示任何(错误)消息。
我怀疑if exist %~dp0%month%\ (
应该引用if exist "%~dp0%month%\" (
-路径可能包含空格。
mkdir
也是如此--如果没有引号,每个空格分隔的字符串将被视为要创建的目录名称。
您的比较语句"%Input%"NEQ"y"
需要neq
两边的空格。
请阅读斯蒂芬斯的脱衣舞环节,了解为什么这不能按原样工作。关于如何在这种情况下使用choice
,有很多这样的文章。
https://stackoverflow.com/questions/72220041
复制相似问题