instead of not printing anything batch file prints echo is off - batch-file

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"
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

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...

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.
For example, this line will put "ECHO is off" into tipp.txt if the %passtip% variable is empty:
echo>tipp.txt %passtipp%
This line will cause tipp.txt to be empty:
>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(.

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".
What you should be doing is echo 1234>password.txt, which will redirect the string 1234 to password.txt.
And then do the same thing for tipp.txt.
Also, I feel the need to point out that the word you're looking for is "tip," not "tipp."

Related

when writing to a file cmd just crashes

I'm making a game in batch, but when i try to write to a file, cmd just crashes. I'm writing to a file to save data. So that the user can just come back and load it up.
Create User Code Block:
:cruser1
cls
set /a created=1
echo(
set /p name=Enter The Name of The User::
goto save
Load User Code Block:
:loaduser
cls
set /a created=0
echo(
set /p loaduser=Enter The Name of The User to Load::
if exist "%loaduser%.dll" (
< %loaduser%.dll (
set /p MHP=
set /p HP=
set /p DMG=
set /p GOLD=
set /p EXP=
set /p LVL=
set /p MEXP=
)
goto menu
)
if not exist "%loaduser%.dll" (
echo File not found.
timeout /t 3 /NOBREAK >nul
goto loaduser
)
Save Data Code Block:
:save
cls
echo(
if %created% equ 1(
(
echo %MHP%
echo %HP%
echo %DMG%
echo %GOLD%
echo %EXP%
echo %LVL%
echo %MEXP%
) > %name%.dll
attrib +h "%name%.dll"
goto menu
)
if %created% equ 0(
(
echo %MHP%
echo %HP%
echo %DMG%
echo %GOLD%
echo %EXP%
echo %LVL%
echo %MEXP%
) > %loaduser%.dll
attrib +h "%loaduser%.dll"
goto menu
)
Does anyone know how to fix this and why it's happening?
Edit: I debugged it, after saving echoing it worked, after i killed the enemy, It did say it worked, but also Access is denied.
Hopefully this helps.
You have errant parentheses, Issue one. issue 2 failure to test existance for existing hidden file
If exist "%loaduser%.dll" attrib -h "%loaduser%.dll"
if %created% equ 0 (
echo %MHP%
echo %HP%
echo %DMG%
echo %GOLD%
echo %EXP%
echo %LVL%
echo %MEXP%
) > %loaduser%.dll
attrib +h "%loaduser%.dll"
goto menu

Changing multiple folder icons by dropping folder as input to a batch script

I have a batch script which is used to change a folder's icon.
If [%1] == [] goto :eof
ECHO [.ShellClassInfo] >%1\desktop.in
ECHO IconResource=Example.ico,0 >>%1\desktop.in
move %1\desktop.in %1\desktop.ini
attrib +S +H %1\desktop.ini
attrib +R %1
The problem is that currently the batch file only accepts one folder dropped onto it.
Is there a way for it to accept multiple dropped folders?
if you drop more than one folder, they are received. %1 is just the first of them. Next would be %2 etc. There is a shift command, which shifts the parameters to the left (%1 is discarded, %2 becomes the new %1 etc.):
#echo off
:loop
if "%~1"=="" pause & goto :eof
echo %~1
shift
goto :loop
Notes: use doublequoutes instead of [ and ] to correctly process folders with spaces (and avoid syntax errors with the if command.
use %~1 to remove any surrounding quotes (will be added automatically, if the folder name contains space(s).
There is a line length limitation of about 8200 chars. If you drop too many folders (exceeding the character limit), it will be cut off.
You can try something like that :
#echo off
Color 0A & Mode 75,3
set "ScriptName=%~nx0"
Title Drag and Drop a folder or multi folders over "%ScriptName%"
if "%~1"=="" goto error
:loop
set "$Folder=" & pushd "%~1" 2>nul && ( popd & set "$Folder=%~1"
) || (
set "$Folder=" && echo "%~1" is not a folder & pause
)
If Defined $Folder Call :WriteDesktopIni %$Folder%
shift
if not "%~1"=="" goto loop
echo(
echo End of the script "%ScriptName%"
Timeout /T 3 /nobreak>nul & Exit
::***************************************************************************
:WriteDesktopIni [Folder]
if exist "%~1\desktop.ini" ( attrib -h -s -a "%~1\desktop.ini" >nul 2>&1 )
(
ECHO [.ShellClassInfo]
ECHO IconResource=%systemroot%\system32\shell32.dll,47
)>"%~1\desktop.ini"
attrib +S +H +A "%~1\desktop.ini"
attrib +R "%~1"
goto :eof
::***************************************************************************
:Error
echo(
echo You should drag and drop a folder or multi folders over "%ScriptName%"
Timeout /T 3 /nobreak>nul & exit
::***************************************************************************
Edit : CustomIconFolder.bat
In this script, you can select a folder or multi folders and your custom icon by drag and drop over the script
#echo off & Setlocal EnableDelayedExpansion
Color 0A & Mode 78,5
set "ScriptName=%~nx0"
set /a "count=0"
Title Drag and Drop a folder or multi folders over "%ScriptName%"
if "%~1"=="" goto error
for %%a in (%*) do (
set /a "count+=1"
set "$Folder=" & pushd "%%~a" 2>nul && ( popd & set "$Folder[!count!]=%%~a"
) || (
set "$Folder="
Setlocal DisableDelayedExpansion
color 0C & echo(
echo "%%~a"
echo ====^> is not a folder !
echo Exiting the script . . .
endlocal
Timeout /T 3 /nobreak>nul & exit
)
)
Rem Dispaly selected folders
Mode 75,10
Setlocal EnableDelayedExpansion
for /L %%i in (1,1,%count%) do (
If [%count%] EQU [1] (
echo You have chosen this folder :
echo [%%i] - "!$Folder[%%i]!"
) else (
echo [%%i] - "!$Folder[%%i]!"
)
)
Timeout /T 2 /nobreak>nul
Mode 78,8 & Cls & echo(
echo Please drag and drop your custom icon to be set to your folder over here
echo and press enter...
echo(
echo Or just write the whole path and press enter ...
Set /p "Icon="
If [!Icon!] EQU [] (
cls & echo(
echo The selected icon is : "%systemroot%\system32\shell32.dll,47"
Timeout /T 3 /nobreak>nul
for /L %%i in (1,1,%count%) do (
echo !$Folder[%%i]!
Call :WriteDesktopIni !$Folder[%%i]! "%systemroot%\system32\shell32.dll,47"
)
) Else (
for %%a in (!Icon!) do ( set "Icon_Name=%%~nxa" & set "Ext=%%~xa" )
If /I [!Ext!] EQU [.ICO] (
cls & echo(
echo The selected icon is : "!Icon_Name!"
echo From this path : !Icon!
Timeout /T 3 /nobreak>nul
for /L %%i in (1,1,%count%) do (
echo "!$Folder[%%i]!"
Copy /y !Icon! "!$Folder[%%i]!\!Icon_Name!">nul 2>&1
Attrib +H "!$Folder[%%i]!\!Icon_Name!">nul 2>&1
Call :WriteDesktopIni !$Folder[%%i]! "!Icon_Name!"
)
) else (
Cls & Color 0C & echo(
echo The extension : [*!Ext!] is not allowed
Timeout /T 3 /nobreak>nul
)
)
cls
echo(
echo End of the script "%ScriptName%"
Timeout /T 2 /nobreak>nul & Exit
::***************************************************************************
:WriteDesktopIni [Folder] [Icon]
if exist "%~1\desktop.ini" ( attrib -h -s -a "%~1\desktop.ini" >nul 2>&1 )
(
ECHO [.ShellClassInfo]
ECHO IconResource=%~2
)>"%~1\desktop.ini"
attrib +S +H +A "%~1\desktop.ini">nul 2>&1
attrib +R %~1>nul 2>&1
goto :eof
::***************************************************************************
:Error
Mode 86,10 & color 0B
echo( & echo(
echo You should drag and drop a folder or multi folders over "%ScriptName%"
echo(
echo Or Usage in command line like this syntax :
echo(
echo %~nx0 "FolderPath1" "FolderPath2" "FolderPath3" "FolderPath4"
Timeout /T 10 /nobreak>nul & exit
::***************************************************************************

Class: ECHO is off from batbox

I wanted to make a little game using batbox. I wanted it to display which class you used but it didn't work. It showed "Class: ECHO is off." And I don't know what's wrong. Here's the code I used:
:charectarselect
cd gamedata
color 0f
cls
batbox /g 0 0
echo Select class:
echo.
echo %mage%
echo %worrior%
set /p classes=Class:
echo %classes%>pclass.gm
goto login
:login
color 0f
cd gamedata
set /p pclass=<pclass.gm
set /p user=<username.gm
set /p pass=<password.gm
if "%pclass%" == "1" echo Mage>pclass.gm
cls
title Login
echo ======================LOGIN======================
set /p login=Username :
if %login% == %user% goto password
goto login
:password
cls
echo ======================LOGIN======================
set /p passw=Password :
if %passw% == %pass% goto game
goto password
:game
cls
batbox /c 0xc9 /d "Health: %health%"
batbox /g 11 0 /c 0x9c /d "Strength: %strength%"
batbox /g 23 0 /c 0x9a /d "Class: %pclass%"
pause >nul
The problem is caused by you entering 1 for class. Specifically, it's caused by the line echo %classes%>pclass.gm
When you entered 1 in character selection, it expands that command to echo 1>pclass.gm. Unfortunately, 1> is used to redirect output from STDOUT to a file, and so batch is interpreting this as a command to send the output of echo to pclass.gm. By itself, echo displays whether echo is on or off.
You can get around this by adding parentheses to the echo line:
(echo %classes%)>pclass.gm

after compiling a batch file to exe it stops working

I'm writing a small folder locking software. Recently a masking function was integrated, so that the password input is masked. When in a bat file the program works perfectly, however after compiling the program stops working. The problem ,I think, lies in the "masking" code, as it starts an endless loop and masks every input, even the "Enter" stroke, thus preventing the program from further executing. I even tried iexpress, but it also gives an Error, namely:
Error creating process Command.com/c
C:\Users...\AppData\Local\Temp\IXP000.TMP\Folder~1.BAT
Can someone please double check my code and tell me what is wrong, as I am still learning and could not figure out how to fix it.
Thanks in advance.
#Echo Off
SetLocal EnableExtensions DisableDelayedExpansion
mode con cols=80 lines=25
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 --------------------------------------------------------------------------------
echo.
echo Password tipp: %tipp%
echo(
set "pass="
rem set /p "pass=Password: "
Set /P "=Password:" < Nul
Call :PasswordInput pass
if /i "%pass%"=="new" goto :NEWPASS
if "%pass%"=="%password%" (
attrib -h -s "Locked"
ren "Locked" "Unlocked"
echo(
echo Folder unlocked successfully.
goto :END
)
goto :FAIL
:FAIL
color 4F
cls
echo(
echo Invalid password, please try again.
CHOICE /C X /T 1 /D X > nul
cls
goto :OPEN
:NEWPASS
color 8F
cls
echo(
set "oldpass="
rem set /p "oldpass=Old password: "
Set /P "=Old Password:" < Nul
Call :PasswordInput oldpass
if not "%oldpass%"=="%password%" goto :FAIL
:ENTERNEW
color 8F
cls
echo(
set "newpass=""
rem set /p "newpass=New password: "
Set /P "=New Password:" < Nul
Call :PasswordInput newpass
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
Goto :Eof
:PasswordInput
::Author: Carlos Montiers Aguilera
::Last updated: 20150401. Created: 20150401.
::Set in variable Line a input password
::
::Update 20150503: http://stackoverflow.com/users/3439404/josefz?tab=profile
::Changes made in next lines:
:: SetLocal EnableDelayedExpansion
:: If !CHR!==!CR! Echo(&EndLocal&set "%1=%Line%"&Goto :Eof
::Usage:
:: Call :PasswordInput variableName
::where variableName is a name of output variable (by reference call)
::
SetLocal EnableDelayedExpansion
For /F skip^=1^ delims^=^ eol^= %%# in (
'"Echo(|Replace.exe "%~f0" . /U /W"') Do Set "CR=%%#"
For /F %%# In (
'"Prompt $H &For %%_ In (_) Do Rem"') Do Set "BS=%%#"
Set "Line="
:_PasswordInput_Kbd
Set "CHR=" & For /F skip^=1^ delims^=^ eol^= %%# in (
'Replace.exe "%~f0" . /U /W') Do Set "CHR=%%#"
If !CHR!==!CR! Echo(&EndLocal&set "%1=%Line%"&Goto :Eof
If !CHR!==!BS! (If Defined Line (Set /P "=!BS! !BS!" <Nul
Set "Line=!Line:~0,-1!"
)
) Else (Set /P "=*" <Nul
If !CHR!==! (Set "Line=!Line!^!"
) Else Set "Line=!Line!!CHR!"
)
Goto :_PasswordInput_Kbd
You are executing the 16 bit command.com (only in Win 32) not the 32 or 64 bit cmd.exe. It doesn't suport brackets, long filenames, or set /p.
It also doesn't support long file names.

using wildcards to copy directories in .bat files

I have created a .bat file where i input a job number. I want to copy the contents of one folder using the input value with wildcards to another with only the input value. It will not copy the contents of this folder using wildcards. Any suggestions?
sample:
Input = 19000
c:\test2\19000 project\test.doc
c:\test\19000
.bat file below
COLOR 0a
:USERID
CLS
ECHO Enter User Name:
ECHO > NUL
SET /p USERID=
IF %USERID% EQU c GOTO PASSWORD
IF %USERID% LSS c GOTO INVALID
IF %USERID% GTR c GOTO INVALID
:PASSWORD
CLS
ECHO Enter Password:
SET /p PASSWORD=
IF %PASSWORD% EQU c GOTO :job number
IF %PASSWORD% LSS c GOTO INVALID
IF %PASSWORD% GTR c GOTO INVALID
CLS
:INVALID
ECHO INAVLID LOGIN.....
PING -n 3 127.0.0.1 > NUL
ECHO Please try again...
PING -n 3 127.0.0.1 > NUL
GOTO USERID
:Job Number
Set input1=
set /p input1= Job Number:
cls
ECHO OFF
c:
cd\
xcopy "c:\test2\%input1% "*" " "c:\test\%input1%" /e
cd\
c:
cls
EOF
exit
This should xcopy any folder that matches the query with a wildcard after it, to the c:\test\ folder with the same folder structure.
#echo off
COLOR 0a
CLS
:USERID
SET /p "USERID=Enter User Name: "
IF "%USERID%" NEQ "c" GOTO INVALID
ECHO(
SET /p "PASSWORD=Enter Password: "
IF "%PASSWORD%" NEQ "c" GOTO INVALID
ECHO(
Set "input1="
set /p "input1=Job Number: "
for /d /r "c:\test2\" %%a in ("%input1%*") do (
xcopy "%%a\*.*" "c:\test\%%~nxa\" /s/h/e/k/f/c
)
cls
exit
goto :EOF
:INVALID
ECHO INVALID LOGIN.....
PING -n 3 127.0.0.1 > NUL
ECHO Please try again...
PING -n 3 127.0.0.1 > NUL
goto :userid

Resources