而不是不打印任何批处理文件打印回声关闭

instead of not printing anything batch file prints echo is off

我有一个很小的问题,这让我很紧张。我写了一个锁定文件夹的程序,我给了它更改密码和 tipp 的选项,但是,如果没有输入 tipp,我希望它什么都不打印。相反,它说 "echo is off"

我猜是语法,但我不知道在哪里。有人可以帮我仔细检查一下吗?谢谢!

    @Echo Off
setlocal EnableDelayedExpansion
mode con cols=80 lines=25
set /a "tries=3"
color 5F
title Folder Locker by KBKOZLEV
:SETPASS
set "tipp="
set "password="
if exist "password.txt" (
    set /p password=<password.txt
    attrib +h +s "password.txt"
)
if exist "tipp.txt" (
    set /p tipp=<tipp.txt
    attrib +h +s "tipp.txt"
)
:START
if exist "Locked" goto :OPEN
if exist "Unlocked" goto :LOCK
if not exist "Unlocked" goto :MDLOCKER

:LOCK
ren "Unlocked" "Locked"
attrib +h +s "Locked"
echo.
echo Folder locked.
CHOICE /C X /T 1 /D X > nul
goto :END
exit

:MDLOCKER
md "Unlocked"
echo>password.txt 1234
echo>tipp.txt 1234
attrib +h +s "password.txt"
attrib +h +s "tipp.txt"
cls
echo.
echo Private folder created successfully.
CHOICE /C X /T 1 /D X > nul
goto :END

:OPEN
color 2F
cls
echo ********************************************************************************
echo                          Folder Locker by KBKOZLEV v.01                       
echo. 
echo ********************************************************************************
echo ---- Enter password to unlock folder, or enter "new" to set a new password. ----
echo                            You have %tries% attempts left.
echo --------------------------------------------------------------------------------
echo.
echo Password tipp: %tipp%
echo.
set "pass="
Set /P "=Password:" < Nul
set "psCommand=powershell -Command "$pword = read-host -AsSecureString ; ^
    $BSTR=[System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($pword); ^
        [System.Runtime.InteropServices.Marshal]::PtrToStringAuto($BSTR)""
for /f "usebackq delims=" %%p in (`%psCommand%`) do set pass=%%p

if /i "%pass%"=="new" goto :NEWPASS
if "%pass%"=="%password%" (
    attrib -h -s "Locked"
    ren "Locked" "Unlocked"
    echo.
    echo Folder unlocked successfully.
    goto :END
)
set /a tries=%tries -1
if %tries%==0 (
goto :FAIL2 
)
goto :FAIL
:FAIL
color 4F  
cls
echo.
echo Invalid password, please try again.
CHOICE /C X /T 1 /D X > nul
cls
goto :OPEN
:FAIL2
color 4F  
cls
echo.
echo Invalid password, program will now close.
CHOICE /C X /T 2 /D X > nul
cls
goto :END
:NEWPASS
color 8F
cls
echo.
set "oldpass="
Set /P "=Old Password:" < Nul
set "psCommand=powershell -Command "$pword = read-host -AsSecureString ; ^
    $BSTR=[System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($pword); ^
        [System.Runtime.InteropServices.Marshal]::PtrToStringAuto($BSTR)""
for /f "usebackq delims=" %%p in (`%psCommand%`) do set oldpass=%%p
if not "%oldpass%"=="%password%" goto :FAIL
:ENTERNEW
color 8F
cls
echo.
set "newpass=""
Set /P "=New Password:" < Nul
set "psCommand=powershell -Command "$pword = read-host -AsSecureString ; ^
    $BSTR=[System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($pword); ^
        [System.Runtime.InteropServices.Marshal]::PtrToStringAuto($BSTR)""
for /f "usebackq delims=" %%p in (`%psCommand%`) do set newpass=%%p
set newpass=%newpass:"=%
if "%newpass%"=="" (
    echo.
    echo Invalid new password, please enter new password again.
    CHOICE /C X /T 1 /D X > nul
    goto :ENTERNEW
)
if exist "password.txt" attrib -h -s "password.txt"
echo>password.txt %newpass%
echo.
set "passtipp=""
set /p "passtipp=New tipp:"
set passtipp=%passtipp:"=% 
if exist "tipp.txt" attrib -h -s "tipp.txt"
if not "%passtipp%"=="" (
    echo>tipp.txt %passtipp%
) else (
    del "tipp.txt" 
)
goto :SETPASS 

:END
color
EndLocal
exit

问题的根本原因是您将数据重定向到文件的语法错误。当echo本身就是运行时,输出echo是ON还是OFF。所以当你有像 echo>password.txt 1234 这样的行时,你说的是 "Take the output of echo and redirect it to password.txt 1234".

应该做的是echo 1234>password.txt,这会将字符串 1234 重定向到 password.txt。

然后对 tipp.txt 做同样的事情。

此外,我觉得有必要指出您要查找的词是 "tip," 而不是 "tipp."

要在回显空行时抑制 "ECHO is off" 文本,请将 echo 更改为带有尾随点的 echo.。您的脚本已经为控制台的空行执行了此操作。同样的事情也适用于重定向到文件。

例如,如果 %passtip% 变量为空,此行会将 "ECHO is off" 放入 tipp.txt:

echo>tipp.txt %passtipp%

此行将导致 tipp.txt 为空:

>tipp.txt echo.%passtipp%

echo 的所有实例后面附加标点符号通常是安全的。如果路径中有一个名为 "echo" 的文件,则 echo. 形式可能会出现问题;在这种情况下,更安全的选择是 echo(.

唯一的办法:

if "%passtipp%"=="" (echo(>tipp.txt) else (echo>tipp.txt %passtipp%)

说明:看到我对其他答案的所有评论(目前),请...