when writing to a file cmd just crashes - batch-file

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

Related

Update Currently Running Batch Script

Playing around with my Batch script again. It was brought to my attention that my update routine was not working correctly. Iteration 1 would result in the running script being deleted and not copying the new version from a folder I had selected. Iteration 2 now deletes the old script and copies the new script but with each line of code I change I end up with the currently running script closing, and the new updated script not launching.
Here's a snippet of the Update routine:
:Options
REM I'll set the variable here to what is set elsewhere in the script.
SET VERSION=Version 1.7
CLS
ECHO.
ECHO.
ECHO %VERSION%
ECHO.
ECHO.
ECHO ==================== OPTIONS =====================
ECHO.
ECHO 1. Update Script WIP
ECHO 2. Install entire script to Statup Folder
ECHO 3. Install individual items for auto-launch
ECHO 4. Install Windows Logout feature
ECHO 5. Uninstall
ECHO 6. Change Colors
ECHO 7. Changelog
ECHO.
ECHO =======PRESS 'Q' TO QUIT OR 'B' TO GO BACK========
ECHO.
SET OPTION=
SET /P OPTION=Select an Option:
IF /I '%OPTION%'=='1' GOTO Update
IF /I '%OPTION%'=='2' GOTO Install
IF /I '%OPTION%'=='3' GOTO I_Install
IF /I '%OPTION%'=='4' GOTO I_Logout
IF /I '%OPTION%'=='5' GOTO Uinstall
IF /I '%OPTION%'=='6' GOTO Colors
IF /I '%OPTION%'=='7' GOTO Changelog
IF /I '%OPTION%'=='q' GOTO quit
IF /I '%OPTION%'=='quit' GOTO quit
IF /I '%OPTION%'=='b' GOTO menu
IF /I '%OPTION%'=='back' GOTO menu
ECHO.
ECHO ============INVALID INPUT============
ECHO Please select a number
ECHO or select 'Q' or 'B'.
ECHO ======PRESS ANY KEY TO CONTINUE======
PAUSE > NUL
GOTO Options
:Update
ECHO.
IF EXIST "%appdata%\Microsoft\Windows\Start Menu\Programs\Startup\Shortcut Browser *.bat" (
GOTO Update_1
) ELSE (
ECHO You have not installed the script yet. Please install the script first.
)
PAUSE
GOTO Options
:Update_1
IF EXIST "%userprofile%\AppData\Local\Temp\Update.bat" (
GOTO Update_2
) ELSE (
REM Here the main script writes an update script to a batch file
ECHO DEL "%appdata%\Microsoft\Windows\Start Menu\Programs\Startup\Shortcut Browser *.bat" > "%userprofile%\AppData\Local\Temp\Update.bat"
REM I replaced the original file location with one on the desktop. The original location was on a network drive, location is confidential.
ECHO COPY "%userprofile\Desktop\Shortcut Browser *.bat" "%appdata%\Microsoft\Windows\Start Menu\Programs\Startup" >> "%userprofile%\AppData\Local\Temp\Update.bat"
ECHO CALL "%appdata%\Microsoft\Windows\Start Menu\Programs\Startup\Shortcut Browser *.bat" >> "%userprofile%\AppData\Local\Temp\Update.bat"
ECHO EXIT >> "%userprofile%\AppData\Local\Temp\Update.bat"
)
GOTO Update_2
REM Here the main script calls upon the update script
:Update_2
CALL "%userprofile%\AppData\Local\Temp\Update.bat"
REM The script seems to hang here. Either both the scripts will remain open or close or a combination depending on where CALL and START were used.
PAUSE
GOTO Options
I saw elsewhere I could have used the FC command. I didn't know about it until troubleshooting this problem. I've gotten this far and I feel I'm just missing some small trick to get the new updated script to launch.
If you need any more info let me know. I think I covered everything.
#ECHO OFF
SETLOCAL
TITLE Super Script 5000
:Options
REM I'll set the variable here to what is set elsewhere in the script.
SET VERSION=Version 1.7
CLS
ECHO.
ECHO.
ECHO %VERSION%
ECHO.
ECHO.
ECHO ==================== OPTIONS =====================
ECHO.
ECHO 1. Update Script WIP
ECHO 2. Install entire script to Statup Folder
ECHO 3. Install individual items for auto-launch
ECHO 4. Install Windows Logout feature
ECHO 5. Uninstall
ECHO 6. Change Colors
ECHO 7. Changelog
ECHO.
ECHO =======PRESS 'Q' TO QUIT OR 'B' TO GO BACK========
ECHO.
SET "OPTION="
SET /P "OPTION=Select an Option: "
IF /I "%OPTION%"=="1" GOTO Update
IF /I "%OPTION%"=="2" GOTO Install
IF /I "%OPTION%"=="3" GOTO I_Install
IF /I "%OPTION%"=="4" GOTO I_Logout
IF /I "%OPTION%"=="5" GOTO Uinstall
IF /I "%OPTION%"=="6" GOTO Colors
IF /I "%OPTION%"=="7" GOTO Changelog
IF /I "%OPTION%"=="q" GOTO quit
IF /I "%OPTION%"=="quit" GOTO quit
IF /I "%OPTION%"=="b" GOTO menu
IF /I "%OPTION%"=="back" GOTO menu
ECHO.
ECHO ============INVALID INPUT============
ECHO Please select a number
ECHO or select 'Q' or 'B'.
ECHO ======PRESS ANY KEY TO CONTINUE======
PAUSE > NUL
GOTO Options
:Update
ECHO.
IF EXIST "%appdata%\Microsoft\Windows\Start Menu\Programs\Startup\Shortcut Browser *.bat" (
GOTO Update_1
) ELSE (
ECHO You have not installed the script yet. Please install the script first.
)
PAUSE
GOTO Options
:Update_1
IF EXIST "%tmp%\update.bat" (
2>NUL DEL "%tmp%\update.bat" || GOTO Options
)
SETLOCAL
FOR %%A IN (
"%appdata%\Microsoft\Windows\Start Menu\Programs\Startup\Shortcut Browser *.bat"
) DO (
SET "old_version=%%~A"
SET "old_dir=%%~dpA"
SET "old_file=%%~nxA"
)
FOR %%A IN (
"%userprofile%\Desktop\Shortcut Browser *.bat"
) DO (
SET "new_version=%%~A"
SET "new_dir=%%~dpA"
SET "new_file=%%~nxA"
)
IF NOT DEFINED old_version (
ENDLOCAL
GOTO Options
) ELSE IF NOT DEFINED new_version (
ENDLOCAL
GOTO Options
)
(
ECHO #ECHO OFF
ECHO SETLOCAL
ECHO.
ECHO ECHO Update and restarting...
ECHO ^>NUL PING localhost -n 3
ECHO.
ECHO SET "old_version=%old_version%"
ECHO SET "new_version=%new_version%"
ECHO SET "updated_version=%old_dir%\%new_file%"
ECHO.
ECHO CALL :log /clear_log
ECHO.
ECHO IF NOT EXIST "%%new_version%%" (
ECHO CALL :log ERROR: Not exist "%%new_version%%".
ECHO EXIT /B 1
ECHO ^)
ECHO.
ECHO CALL :log Move old_version to old_version.bak.
ECHO MOVE "%%old_version%%" "%%old_version%%.bak" ^|^| (
ECHO CALL :log ERROR: Move failed to backup old version.
ECHO EXIT /B 2
ECHO ^)
ECHO.
ECHO CALL :log Copy new_version to old_version.
ECHO COPY "%%new_version%%" "%%appdata%%\Microsoft\Windows\Start Menu\Programs\Startup\" ^|^| (
ECHO CALL :log ERROR: Copy failed. Restore old version.
ECHO MOVE "%%old_version%%.bak" "%%old_version%%" ^|^| (
ECHO CALL :log ERROR: Restore old version failed.
ECHO EXIT /B 3
ECHO ^)
ECHO EXIT /B 4
ECHO ^)
ECHO.
ECHO CALL :log Delete old_version.bak.
ECHO DEL "%%old_version%%.bak" ^|^| (
ECHO CALL :log WARNING: Delete backup file failed.
ECHO ^)
ECHO.
ECHO IF NOT EXIST "%%updated_version%%" (
ECHO CALL :log ERROR: Not exist: "%%updated_version%%"
ECHO EXIT /B 5
ECHO ^)
ECHO.
ECHO ENDLOCAL
ECHO.
ECHO ECHO Ready to restart updated version
ECHO PAUSE
ECHO CALL "%old_dir%\%new_file%"
ECHO EXIT /B
ECHO.
ECHO :log
ECHO SETLOCAL
ECHO SET "log=%%tmp%%\update.log"
ECHO IF /I "%%~1"=="/clear_log" TYPE NUL ^> "%%log%%" ^& EXIT /B
ECHO IF /I "%%~1"=="/delete_log" 2^>NUL DEL "%%log%%" ^& EXIT /B
ECHO SET args=%%*
ECHO IF NOT DEFINED args EXIT /B
ECHO ECHO %%*
ECHO ^>^> "%%log%%" ECHO %%*
ECHO EXIT /B
) > "%tmp%\update.bat"
ENDLOCAL
START "Super Script 5000" "cmd /c "%tmp%\update.bat""
EXIT
:Quit
Tested on a Win7 VM. The script needs to restart on update else
weird issues may occur. CMD does not like scripts changing as it
is reading and interpreting them. Even reusing the same console
using START with /B may cause key input issues.
update.bat is overwritten every update as it stores absolute paths
so each update will be different paths with the filenames changing.
Using a wildcard with CALL may not work too well otherwise.
update.bat moves the old version to a .bak file, copies the new
version and if successful, deletes the .bak file, else moves the
.bak file to the old version. This is just to ensure a current
version always exists even if the update fails.
Changed quotes in :Options label from ' to ". Quotes are not
interchangeable like some other languages. Example: "%OPTION%"
will work if contains a space though '%OPTION%' may cause error.
I notice no SETLOCAL at the top of your full script. Advise you add
it unless you have a good reason not to.
I merged labels :Update_1 and :Update_2 into one.
Perhaps adjust Ping localhost -n number if more or less time is
needed.
Look at the %tmp%\update.log if a problem happens. Though expect
the errors to show in the console as the label :log in
update.bat echoes to console and writes to the log.

CMD - Input from another cmd

I have one cmd where you do a choice (1 for login, 2 for register, ...) and then you enter the user, and password (set /p user="User: " , set /p password="Password: ") and what I want to do is enter the choice, username and password with another batch file that I make...
What I tried:
#echo off
title Wizzard-Cracker
color a
cd C:\Users\Wolfy\Documents\Wolfy\Subory .BAT\Wizzard
:A
start wizzard.cmd /K set choice=1 & set user=wolfy & set password=sombest
Login Batch:
#echo off
title Login and Register
color F
setlocal enableDelayedExpansion
:A
echo [Login and Register]
echo [1.][Login]
echo [2.][Register]
echo [3.][Exit]
set /p input="Choose: "
if "%input%"=="1" (
cls
goto login
)
if "%input%"=="2" (
cls
goto register
)
if "%input%"=="3" (
cls
exit
)
:login
echo [Login]
set /p user=User:
echo.
set /p password=Password:
echo.
if NOT EXIST data/%user%.userfile (
echo User %user% isn't registered.
ping localhost -n 2 >nul
cls
goto login
)
set /p correct=<data/%user%.userfile
set chars=0123456789abcdefghijklmnopqrstuvwxyz
for /L %%N in (10 1 36) do (
for /F %%C in ("!chars:~%%N,1!") do (
set "correct=!correct:-%%N=%%C!"
)
)
if "%password% "=="!correct!" (
echo Login successful.
ping localhost -n 2 >nul
cls
goto console
) else (
echo Incorrect password entered.
ping localhost -n 2 >nul
cls
goto login
)
:register
echo [Register]
set /p rUser=Register User:
echo.
set /p rPassword=Register Password:
echo.
if EXIST data/%rUser%.userfile (
echo User is already registered.
goto register.choice
)
set chars=0123456789abcdefghijklmnopqrstuvwxyz
for /L %%N in (10 1 36) do (
for /F %%C in ("!chars:~%%N,1!") do (
set "rPassword=!rPassword:%%C=-%%N!"
)
)
echo !rPassword! > data/%rUser%.userfile
echo Registration was successful!
ping localhost -n 2 >nul
cls
goto A
:register.choice
set /p choice="Go to Login [Y/N]: "
if %choice%==y (
cls
goto login
)
if %choice%==Y (
cls
goto login
)
if %choice%==n (
cls
goto register
)
if %choice%==N (
cls
goto register
)
How do I do that?
Here's a quick rewrite, there may be errors or things you would prefer not to change but it should show you a way of doing it.
#ECHO OFF
TITLE Login and Register
COLOR F
SETLOCAL ENABLEDELAYEDEXPANSION
IF "%~1" NEQ "1" (GOTO :A) ELSE (IF "%2" EQU "" (GOTO :A) ELSE (SET "user=%~2"
IF "%~3" EQU "" (SET password=)
GOTO :vlog))
:A
CLS
ECHO [Login and Register]
ECHO [1.][Login]
ECHO [2.][Register]
ECHO [3.][Exit]
CHOICE /C 123 /N
IF ERRORLEVEL 3 EXIT/B
IF ERRORLEVEL 2 GOTO :register
:login
CLS
ECHO [Login]
SET /P user=User:
IF NOT DEFINED user GOTO :login
:vlog
IF "%user:*=%" NEQ "%user%" GOTO :login
IF NOT EXIST data/%user%.userfile (
ECHO User %user% isn't registered.
TIMEOUT 2 >NUL
GOTO :login
)
:pass
SET /P correct=<data/%user%.userfile
ECHO.
IF NOT DEFINED password (
SET /P password=Password:
)
IF NOT DEFINED password GOTO :pass
SET chars=0123456789abcdefghijklmnopqrstuvwxyz
FOR /L %%N IN (10 1 36) DO (
FOR %%C IN (!chars:~%%N,1!) DO (
SET "correct=!correct:-%%N=%%C!"
)
)
IF "%password%"=="%correct%" (
ECHO.
ECHO Login successful.
TIMEOUT 2 >NUL
GOTO :console
)
ECHO Incorrect password entered.
TIMEOUT 2 >NUL
GOTO :pass
:register
CLS
ECHO [Register]
SET /P rUser=Register User:
IF NOT DEFINED rUser GOTO :register
IF "%rUser:*=%" NEQ "%rUser%" GOTO :register
IF EXIST data/%rUser%.userfile (
ECHO.
ECHO User is already registered.
GOTO :register.choice
)
:rpass
SET /P rPassword=Register Password:
IF NOT DEFINED rPassword GOTO :rpass
SET chars=0123456789abcdefghijklmnopqrstuvwxyz
FOR /L %%N IN (10 1 36) DO (
FOR %%C IN (!chars:~%%N,1!) DO (
SET "rPassword=!rPassword:%%C=-%%N=!"
)
)
>data/%rUser%.userfile ECHO %rPassword%
ECHO.
ECHO Registration was successful!
TIMEOUT 2 >NUL
GOTO :A
:register.choice
ECHO.
CHOICE /C YN /M "Go to Login"
IF ERRORLEVEL 2 GOTO :register
GOTO :login
:console
REM Section is Missing
You should be able to run it in one of these ways, depending upon your requirements:
"X:\SomePathTo\wizzard.cmd" 1 wolfy sombest
start "" "X:\SomePathTo\wizzard.cmd" 1 wolfy sombest
call "X:\SomePathTo\wizzard.cmd" 1 wolfy sombest
Of course if you cannot change the actual wizzard.cmd script then I'm afraid that you have no way of bypassing the prompts' which should, after all, only waste a couple of seconds of your precious time.
instead of set /p correct = <C:\file , get rid of the p and type set correct =<C:\file
Why would you use a different batch file? But however, if you want to parse data between to batch instances, make use of command line arguments. A simple google to that gives you a clear explanation. But be carefull. Batch files are not safe to use passwords. You have to learn programming for that. VB.net is an example language which is easy to learn

Batch file need info

I am combining multiple Batch files I made into 1 Batch file.
I have a few questions.
I am going through thousands of pictures and when I come across 1 that I need I copy it to my documents to review later, that's what the "copy" in the script is for. I have it set up to loop so I can just keep entering files to send to my documents.
First. I would like to be able to jump back to the menu when I type Menu, so it would leave the loop and goto :MENU.
Second. I have no idea if this is possible... but going through so many pictures that I would like to be able to save where I left off. Thats where the GOTO :Set comes in to play. I would like to be able to type in the number of the picture and have the Batch file save it, so that when I enter GOTO :OPEN it will then open the picture I left off on.
Sorry if this sounds confusing, any help would be great. If you have any questions feel free to ask
Thank you
ECHO OFF
CLS
:MENU
CLS
ECHO.
ECHO ...............................................
ECHO Welcome to the sub-menu
ECHO ...............................................
ECHO.
ECHO 1 - Rename files in folder.
ECHO 2 - Copy files to My Documents.
ECHO 3 - Set file to you left off on.
ECHO 4 - Open file you left off on.
ECHO 5 - Exit.
ECHO.
SET /P M=Type 1, 2, 3, or 4 then press ENTER:
IF %M%==1 GOTO Rename
IF %M%==2 GOTO COPY
IF %M%==3 GOTO SET
IF %M%==4 GOTO OPEN
:Rename
setlocal EnableDelayedExpansion
set i=0
for %%a in (*.jpg) do (
set /a i+=1
ren "%%a" "!i!.new"
)
ren *.new *.jpg
GOTO MENU
:COPY
cls
SET /P filename=Enter the file which should be moved:
xcopy %filename%.* C:\Users\USERNAME\Documents
if not exist %filename%.* goto :Failure
if exist %filename%.* goto :data
GOTO MENU
:SET
GOTO MENU
:OPEN
GOTO Me
:Failure
echo Failure
pause
goto :COPY
:data
timeout /t 3
goto :COPY
I Figured out another way to leave the loop, now I just need to enter anything that does not match a name in the folder.
This is what I have now and is working Great.
ECHO OFF
CLS
:MENU
CLS
ECHO.
ECHO ...............................................
ECHO Welcome to the sub-menu
ECHO ...............................................
ECHO.
ECHO 1 - Rename files in folder.
ECHO 2 - Copy files to My Documents.
ECHO 3 - Set file to you left off on.
ECHO 4 - Open file you left off on.
ECHO 5 - Exit.
ECHO.
SET /P M=Type 1, 2, 3, or 4 then press ENTER:
IF %M%==1 GOTO Rename
IF %M%==2 GOTO COPY
IF %M%==3 GOTO SET
IF %M%==4 GOTO OPEN
:Rename
setlocal EnableDelayedExpansion
set /p i=Enter Starting Number:
for %%a in (*.JPG) do (
set /a i+=1
ren "%%a" "!i!.new"
)
ren *.new *.JPG
GOTO MENU
:COPY
cls
SET /P filename=Enter the file which should be moved:
xcopy %filename%.* C:\Users\USERNAME\Documents
if not exist %filename%.* goto :Failure
if exist %filename%.* goto :data
:Failure
goto :menu
:data
timeout /t 3
goto :copy
GOTO MENU
:SET
cls
del temp.txt
set INPUT=
set /P INPUT=Type input: %=%
echo Your input was: %INPUT%
pause
echo %INPUT%.JPG >>temp.txt
GOTO MENU
:OPEN
set /p texte=< temp.txt
echo %texte%
pause
set q=%texte%
start %texte%
GOTO menu
:Failure
echo Failure
pause
goto :COPY
:data
timeout /t 3
goto :COPY

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.

instead of not printing anything batch file prints echo is off

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

Resources