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

时间:2022-10-03 02:09:16

I have a very little problem, which gets on my nerves. I wrote a program for locking folders and I gave it the option to change the password as well as the tipp, however, if no tipp is typed, I want it to just print nothing. Instead it says "echo is off"

我有一个很小的问题,让我感到紧张。我写了一个用于锁定文件夹的程序,我给了它选项来更改密码以及tipp,但是,如果没有输入tipp,我希望它只打印任何内容。相反,它说“回声关闭”

I guess it's the syntax, but I do not know where. Can someone double check it for me. thank you!

我想这是语法,但我不知道在哪里。有人可以为我仔细检查。谢谢!

    @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

3 个解决方案

#1


To suppress the "ECHO is off" text when echoing a blank line, change echo to echo. with a trailing dot. Your script already does this for blank lines to the console. The same thing works for redirecting to files.

要在回显空白行时禁止“ECHO关闭”文本,请将回显更改为回显。带有一个尾点。您的脚本已经为控制台的空白行执行此操作。同样适用于重定向到文件。

For example, this line will put "ECHO is off" into tipp.txt if the %passtip% variable is empty:

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

echo>tipp.txt %passtipp%

This line will cause tipp.txt to be empty:

该行将导致tipp.txt为空:

>tipp.txt echo.%passtipp%

It's usually safe to append all instances of echo with trailing punctuation. The form echo. can be problem if there is a file named "echo" in the path; In that case, a safer alternative is echo(.

通过尾随标点符号附加回声的所有实例通常是安全的。形式回声。如果路径中有一个名为“echo”的文件,则可能会出现问题;在这种情况下,更安全的替代方案是回声(。

#2


The only approach:

唯一的方法:

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

Explanation: see all my comments to other answers (for the present), please...

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

#3


The root cause of your issue is that your syntax for redirecting data to a file is wrong. When echo is run by itself, it outputs whether echo is ON or OFF. So when you have lines like echo>password.txt 1234, you are saying "Take the output of echo and redirect it to password.txt 1234".

您的问题的根本原因是您将数据重定向到文件的语法是错误的。当echo自身运行时,它会输出echo是ON还是OFF。所以当你有像echo> password.txt 1234这样的行时,你会说“获取echo的输出并将其重定向到password.txt 1234”。

What you should be doing is echo 1234>password.txt, which will redirect the string 1234 to password.txt.

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

And then do the same thing for tipp.txt.

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

Also, I feel the need to point out that the word you're looking for is "tip," not "tipp."

另外,我觉得有必要指出你要找的那个词是“小费”,而不是“tipp”。

#1


To suppress the "ECHO is off" text when echoing a blank line, change echo to echo. with a trailing dot. Your script already does this for blank lines to the console. The same thing works for redirecting to files.

要在回显空白行时禁止“ECHO关闭”文本,请将回显更改为回显。带有一个尾点。您的脚本已经为控制台的空白行执行此操作。同样适用于重定向到文件。

For example, this line will put "ECHO is off" into tipp.txt if the %passtip% variable is empty:

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

echo>tipp.txt %passtipp%

This line will cause tipp.txt to be empty:

该行将导致tipp.txt为空:

>tipp.txt echo.%passtipp%

It's usually safe to append all instances of echo with trailing punctuation. The form echo. can be problem if there is a file named "echo" in the path; In that case, a safer alternative is echo(.

通过尾随标点符号附加回声的所有实例通常是安全的。形式回声。如果路径中有一个名为“echo”的文件,则可能会出现问题;在这种情况下,更安全的替代方案是回声(。

#2


The only approach:

唯一的方法:

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

Explanation: see all my comments to other answers (for the present), please...

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

#3


The root cause of your issue is that your syntax for redirecting data to a file is wrong. When echo is run by itself, it outputs whether echo is ON or OFF. So when you have lines like echo>password.txt 1234, you are saying "Take the output of echo and redirect it to password.txt 1234".

您的问题的根本原因是您将数据重定向到文件的语法是错误的。当echo自身运行时,它会输出echo是ON还是OFF。所以当你有像echo> password.txt 1234这样的行时,你会说“获取echo的输出并将其重定向到password.txt 1234”。

What you should be doing is echo 1234>password.txt, which will redirect the string 1234 to password.txt.

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

And then do the same thing for tipp.txt.

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

Also, I feel the need to point out that the word you're looking for is "tip," not "tipp."

另外,我觉得有必要指出你要找的那个词是“小费”,而不是“tipp”。