How to use call echo in a string - batch-file

I am using a menu and want to write to a file with selected name in dropdown. It's working fine except the file name is incorrect.
It's naming it " ws-%Name[2]% " and not putting the correct value in.
How can I fix this this? thanks for any help
ECHO OFF
CLS
:MENU
ECHO.
ECHO ...............................................
ECHO Select Postroom staff:-
ECHO ...............................................
ECHO.
set "Name[1]=Diana Mckinley"
set "Name[2]=Lenka Smolkova"
set "Name[3]=Louise Smith"
set "Name[4]=Sally Baldwin"
set "Name[5]=Sally Faulkner"
set "Name[6]=Sarah Kendle"
set "Name[7]=Sophie Jennings"
set "Name[8]=Steven Caylor"
:MenuLoop
set /a "x+=1"
if defined Name[%x%] (
call echo %x%. %%Name[%x%]%%
goto MenuLoop
)
ECHO.
SET /P index=Type KEY then press ENTER to register:
call echo %%Name[%index%]%% is now registered
call echo %%Name[%index%]%% > "C:\Users\karim ali\Documents\Test\ws-%%Name[%index%]%%.txt"
GOTO MenuLoop

All you'll need to do is to set that name and index as a variable and use that.
Here is an example with a few changes for demonstation purposes:
#ECHO OFF
SET "Name[1]=Diana Mckinley"
SET "Name[2]=Lenka Smolkova"
SET "Name[3]=Louise Smith"
SET "Name[4]=Sally Baldwin"
SET "Name[5]=Sally Faulkner"
SET "Name[6]=Sarah Kendle"
SET "Name[7]=Sophie Jennings"
SET "Name[8]=Steven Caylor"
:MENU
CLS
ECHO.
ECHO ...............................................
ECHO Select Postroom staff:-
ECHO ...............................................
ECHO.
:MenuLoop
FOR /F "TOKENS=2-3 DELIMS==[]" %%A IN ('SET Name[') DO (
CALL ECHO %%A. %%Name[%%A]%%)
ECHO.
SET /P index=Type KEY then press ENTER to register:
CALL SET "PRSName=%%Name[%index%]%%"
ECHO.
ECHO %PRSName% is now registered
TIMEOUT 2 /NOBREAK 1>NUL
ECHO %PRSName%>"C:\Users\karim ali\Documents\Test\ws-%PRSName%.txt"
SET "Name[%index%]="
SET Name[>NUL 2>&1&&(GOTO :MENU)

expansion of nested variables can be confusing...
instead of ws-%%Name[%index%]%%.txt, write ws-%Name[%%index%%]%.txt

Related

I want to have multiple echo and echo. below a pause and below a set /p variable= befor pressing any key in a batch file

I want the line of code below to work or something that will do the same and i want the echo and echo. to show befor i press any key
set /p variable=
echo.
echo test
echo.
echo test
::ect
and I want the line of code below to work or something that will do the same and i want the echo and echo. to show befor i press any key
pause
echo.
echo test
echo.
echo test
::ect
I tried this (I have not tried it with pause)
#echo off
for /f %%A in ('echo prompt $E^| cmd') do set "esc=%%A"
cls
echo *other text*%ESC%[2A
set /p variable=
echo %ESC%[1B
I was only able to have one echo or one echo. after set /p variable=, but I expected to be able to have multiple echo below set /p variable= and multiple echo. below set /p variable=
If i try to have more then one line of text then it takes the line of text above set /p variable= and moves it down below set /p variable= and combines it with the text i want below set /p variable=
So i want the set /p variable= and pause to pause after all the echo and echo.
I want it to look like for example this when i open the .bat file
Type the number of your choosing:
*other text*
Or this
Press any key to continue . . .
*other text*
sorry for posting the same thing so many times i promis that this is the last time
You are using ANSI escape codes. Just use them at the right place and with the proper parameters (numbers):
#echo off
for /f %%A in ('echo prompt $E^| cmd') do set "esc=%%A"
cls
echo/
echo/
echo *other text*
echo *additional text*
echo *some more text*
echo %ESC%[5A
set /p "variable=Enter var: "
echo %ESC%[2B
echo Thank you for your input.
Output on screen (where Hello is the manual input:
Enter var: Hello
*other text*
*additional text*
*some more text*
Thank you for your input.
You need enough space "above" set /p. It doesn't work at the very first line (after CLS) because you can't move the cursor to "line zero", so you need (at least) two (empty) lines (echo/).

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

display a variable like %%i in for statements in variable? batch

How do I use a variable like %%i in a permanent variable?
(I don't really know the correct terms, so I hope anyone can figure out what I mean)
This is the code I am using:
#echo off
color 0f
goto number
:number
title number
cls
echo number of options?
set /p num=
goto option
:option
for /l %%i in (1,1,%num%) do (
cls
echo Name nr. %%i
echo Enter a option
set /p n%%i=
echo %%i = %n%%i% >> log.txt
)
goto select
:select
cls
echo %n2%
pause >nul
the "%n2%" works for whatever you put in second, but when I try to print it into a file ( echo %%i = %n%%i% >> log.txt ) it doesn't work.
I know the "%n%%i%" is not correct, But I don't really know what to actually put there.
:option
for /l %%i in (1,1,%num%) do (
cls
echo Name nr. %%i
echo Enter an option
set /p option%%i=
)
goto select
:select
set option>log.txt
cls
echo %option2%
You may like to consider this.
The command
set option
will show every environment variable that starts option in the format option1=Gido, which is why I changed n to option (there are other variables set that start n)

Make a random string in batch

I am trying to make a random string in batch, and I have no idea what to do. I have called a random string of numbers with the %random% function, but I have no idea how to permanently replace certain characters in my variable:
#echo off
set subkey1=%random%%random%%random%%random%%random%%random%
set subkey2=%random%%random%%random%%random%%random%%random%
set subkey3=%random%%random%%random%%random%%random%%random%
ECHO %subkey1:0=a%
ECHO %subkey1:1=b%
ECHO %subkey1:2=c%
ECHO %subkey1:3=d%
ECHO %subkey1:4=e%
ECHO %subkey1:5=f%
ECHO %subkey1:6=g%
ECHO %subkey1:7=h%
ECHO %subkey1:8=i%
ECHO %subkey1:9=j%
ECHO %subkey2:0=k%
ECHO %subkey2:1=l%
ECHO %subkey2:2=m%
ECHO %subkey2:3=n%
ECHO %subkey2:4=o%
ECHO %subkey2:5=p%
ECHO %subkey2:6=q%
ECHO %subkey2:7=r%
ECHO %subkey2:8=s%
ECHO %subkey2:9=t%
ECHO %subkey3:0=u%
ECHO %subkey3:1=v%
ECHO %subkey3:2=w%
ECHO %subkey3:3=x%
ECHO %subkey3:4=y%
ECHO %subkey3:5=z%
REM after going through all the letters, we just pick random ones
ECHO %subkey3:6=a%
ECHO %subkey3:7=g%
ECHO %subkey3:8=k%
ECHO %subkey3:9=o%
set key=%subkey1%%subkey2%%subkey3%
echo String: %key%
pause
So as you can see, I am able to ECHO different keys, but I want to save the new one to the variable (EX in subkey 1, I wanna replace all the 0s by a's, then SAVE that so if I look at my variable afterwards, I can see the new one, and be able to call that in different functions.
same effect with much less code:
#echo off
setlocal enabledelayedexpansion
set "string=abcdefghijklmnopqrstuvwxyz"
set "result="
for /L %%i in (1,1,30) do call :add
echo %result%
goto :eof
:add
set /a x=%random% %% 26
set result=%result%!string:~%x%,1!
goto :eof
To assign a variable a value, use the SET command:
SET subkey1=%random%%random%%random%%random%%random%%random%
SET subkey1=%subkey1:0=a%
SET subkey1=%subkey1:1=b%
SET subkey1=%subkey1:2=c%
.
.
.
ECHO %subkey1%
And, of course, beware that %random% should never, ever be used as a source for cryptographic purposes.
#echo off
setlocal EnableDelayedExpansion
set charSets=abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!##$%^&*
set count=0
set /a countRaw=4+(%random%)%%20
for /L %%c in (1,1,%countRaw%) do (call :MAKERANDOMSTRING)
goto ENDRANDOMSTRING;
:MAKERANDOMSTRING
set buffer=% %
set count=0
set /a lowValue=30+(%random%)%%40
set /a length=10+!lowValue!
:Loop
set /a count+=1
set /a rand=%Random%%%69
set buffer=!buffer!!charSets:~%rand%,1!
if !count! leq !length! goto Loop
echo "%buffer%"
:ENDRANDOMSTRING

Batch File path vairiable %PATH%

I have been creating a chat room for my school, but I have to bring home the file to make changes as they become needed but my problem is the file path has to be changed each time I move the files from one system to another. so I would like to know how to create a %PATH% that will work for me
This is my full code for the main file:
As you may notice I'm new to this
NOTE: Everything here works fine with a set file path but I want it to work easier for when I change computers.
Anything could probably help
#echo off
:Tittle
cls
color 74
title Terms of Service
echo _________________________-Terms-______________________________
echo If you are using this ChatRoom then you agree to the following.
echo *you will not use Horrid Language
echo *you will make your account with either your real name or
echo Student id
echo.
echo This is monitored everyday so if anything is out of line it will be removed.
echo.
echo If you agree to follow these terms then type "yes" otherwise exit.
set /p c=Do you Agree to follow the terms?:
if %c% EQU yes goto Menu
if %c% EQU ADMIN1423 goto Admin
if %c% EQU dad goto Menu
if %c% EQU carrie goto Menu
if %c% EQU dad SET PATH=%PATH%;c:\Users\Dan W Frye\Desktop\(-_-)
:Admin
color 02
cls
Echo
Echo.
Echo 1.) Check User list
Echo.
Echo 2.) Create File Path
Echo.
Echo 3.) Admin Help
Echo.
echo 4.) N/A
echo.
Echo
set /p c=Selection Number:
if %c% EQU 1 goto UserList
if %c% EQU 2 goto CreatePath
if %c% EQU 3 goto AdminHelp
if %c% EQU 4 goto Admin
if %c% EQU back goto Tittle
:UserList
color 0b
cls
title User Listing
cls
start cmd
CALL "%PATH%\Data\Chat Settings\Users\BuAsTeCrHs.bat"
pause
goto Admin
:CreatePath
cls
color 01
echo
echo.
echo The File Path Must look like this(No " "): "Driver (C:)"\Containing folder" thats it the echo rest is automatically "\Data\Chat Settings\Users\....."
echo.
echo
echo.
echo The File Path you want to create.
set /p PATH=File Path:
echo.
echo The Location/Device you are using.
set /p LOC=Location:
echo.
echo %PATH% >>"%PATH%\Data\Chat Settings\File Paths\%LOC%.txt"
echo The file path has been created!
pause
goto Admin
:AdminHelp
pause
goto Tittle
:Menu
color 0b
cls
Echo -[ChatBox]-
Echo
Echo.
Echo 1.) Login
Echo.
Echo 2.) Register
Echo.
Echo 3.) Exit
Echo.
echo 4.) Help
echo.
Echo
Echo.
set /p c=Selection Number:
if %c% EQU 1 goto Login
if %c% EQU 2 goto Register
if %c% EQU 3 exit
if %c% EQU 4 goto help
if %c% EQU 5 goto Terms
:help
cls
echo if you are not able to see the chat log then you must not have
echo the file "Chatroom_reader.bat" open without this you cannot see
echo messages sent by other users.
echo.
echo if you have suggestions or comments then please type "comment"
echo.
echo if you need assistance with any other problem you may have
echo encountered then please type "other" to let the developer know
echo what the problem is. otherwise type "back" to go back to the menu.
set /p c=Option:
if %c% EQU comment goto comments
if %c% EQU other goto other
if %c% EQU back goto menu
:comments
cls
title Comments
echo Enter your Username, and Password to Place a Comment
echo.
set /p UN=Username:
echo.
set /p PW=Password:
echo.
if NOT Exist "%PATH%\Data\Chat Settings\Users\%UN%.txt" Goto Failed
echo %PW% >"%tmp%\chat.tmp"
fc "%tmp%\chat.tmp" "%PATH%\Data\Chat Settings\Users\%UN%.txt" >nul
if errorlevel==1 goto Failed
if errorlevel==0 goto Comment
:Comment
cls
echo.
set /p SUBJECT=Subject:
echo.
set /p COMMENT=Comment:
echo.
echo %SUBJECT% : %COMMENT% >"%PATH%\Data\Chat Settings\Comments\%UN%.txt"
goto User
:other
cls
title Other
echo Enter your Username, and Password to Place a Comment
echo.
set /p UN=Username:
echo.
set /p PW=Password:
echo.
if NOT Exist "C:\Users\Dan W Frye\Desktop\(-_-)\Data\Chat Settings\Users\%UN%.txt" Goto Failed
echo %PW% >"%tmp%\chat.tmp"
fc "%tmp%\chat.tmp" "C:\Users\Dan W Frye\Desktop\(-_-)\Data\Chat Settings\Users\%UN%.txt" >nul
if errorlevel==1 goto Failed
if errorlevel==0 goto OtherA
:OtherA
cls
set /p OTHERC=Other Concern:
echo.
echo %OTHERC% >"%PATH%\Data\Chat Settings\Other\%UN%.txt"
goto User
:Login
cls
echo Enter your Username, and Password to login to the Chat Server
echo.
set /p UN=Username:
echo.
set /p PW=Password:
echo.
if NOT Exist "%PATH%\Data\Chat Settings\Users\%UN%.txt" Goto Failed
echo %PW% >"%tmp%\chat.tmp"
fc "%tmp%\chat.tmp" "%PATH%\Data\Chat Settings\Users\%UN%.txt" >nul
if errorlevel==1 goto Failed
if errorlevel==0 goto User
:User
cls
Echo Welcome %UN% The Current date is %date%
echo
echo.
echo 1.) Chat
echo.
echo 2.) Logout
echo.
echo 3.) Change Password
echo.
echo 4.) Private Chat
echo.
echo 5.) Enter a Private Chat room
echo.
echo
set /p c=Selection Number:
if %c% EQU 1 goto chat
if %c% EQU 2 goto Menu
if %c% EQU 3 goto CHP
if %c% EQU 4 goto PRIVATE
if %c% EQU 5 goto PRIVATENTER
:PRIVATENTER
echo Please enter the name of the Private chat room if you do not know the name you may not enter.
set /p Chat=
if %Chat% EQU scooter goto scooter
if %Chat% EQU Cre-Br goto Cre-Br
:Cre-Br
cls
set name=[%time%]%UN%
cls
color 02
echo Last Message sent by %UN% \/
echo [%time%]%UN%:%text%
set /p text=Say:
echo %name% : %text% >>"%PATH%\Data\Chat Settings\Program_Files\Cre-Br.txt"
goto Cre-Br
:scooter
cls
echo %Chat%
set name=[%time%]%UN%
color 02
echo Last Message sent by %UN% \/
echo [%time%]%UN%:%text%
set /p text=Say:
echo %name% : %text% >>"%PATH%\Data\Chat Settings\Program_Files\scooter.txt"
goto scooter
:PRIVATE
cls
set /p Chat=Chat Name:
echo this is %UN%s Private chat room >>"%PATH%\Data\Chat Settings\Program_Files\%Chat%.txt"
echo.
echo #echo off >>"%PATH%\Data\Chat Settings\Private Chats\%Chat%.bat"
echo color 0b >>"%PATH%H:\(-_-)\Data\Chat Settings\Private Chats\%Chat%.bat"
echo cls >>"%PATH%\Data\Chat Settings\Private Chats\%Chat%.bat"
echo title Message Box >>"%PATH%\Data\Chat Settings\Private Chats\%Chat%.bat"
echo :home >>"%PATH%\Data\Chat Settings\Private Chats\%Chat%.bat"
echo cls >>"%PATH%\Data\Chat Settings\Private Chats\%Chat%.bat"
echo findstr /v "g91dhjt637hsuexv27niw9" "%PATH%\Data\Chat Settings\Program_Files\%Chat%.txt" >>"C:\Users\Dan W Frye\Desktop\Batch\Chat Settings\Private Chats\%Chat%.bat"
echo goto home >>"%PATH%\Data\Chat Settings\Private Chats\%Chat%.bat"
goto User
:CHP
cls
set /p PW=Old Password:
echo.
set /p NP=New Password:
echo %NP% >"%PATH%\Data\Chat Settings\Users\%UN%.txt
goto User
:Register
cls
color 07
echo Register (Note the username is your screen name Please use your real name or School ID EX, CaBu56789)
echo.
set /p NU=Username:
echo.
set /p NP=Password:
echo.
echo %NP% >"%PATH%\Data\Chat Settings\Users\%NU%.txt"
echo.
cls
goto login
:Failed
color 0c
cls
echo You have entered am invalid Username and or Password
echo Please try again or Register for free
pause
goto menu
:chat
set name=[%time%]%UN%
cls
color 02
echo Everything said here is on recored please mind your
echo language!
echo Last Message sent by %UN% \/
echo [%time%]%UN%:%text%
set /p text=Say:
echo %name% : %text% >>"%PATH%\Data\Chat Settings\Program_Files\ChatRoom.txt"
goto chat
%PATH% is reserved environment variable, documented here: http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/path.mspx?mfr=true
For diagnostics purposes you could echo that variable to analyze if that has such value you intended to have:
echo %path%
As mentioned by #Jermu Virtanen the %PATH% variable is a windows system set path...you should not use/change it unless you really know what you are doing.
So instead of using %PATH% use something like %PROGPATH%.
Also as mentioned by #foxdrive, if the user enters "dad" it will go to "menu" before setting the path variable. And again if you instead chose 'Admin' then to 'List users' it will again be calling the path without first setting it.
You could automate some of the login or path settings by grabbing the computer's domain name %userdomain% and current user %username%.
Ie;
SET "progpath=c:\Users\%username%\Desktop\(-_-)"
In your code the path was being set after it had already branched to the menu. This may work for you:
if %c% EQU carrie goto Menu
if %c% EQU dad SET PATH=%PATH%;c:\Users\Dan W Frye\Desktop\(-_-)& goto menu
To put the batch file on the path for every user, use this:
set path=%path%;%PUBLIC%
and copy the batch file to C:\Users\Public and run it from there.
Unless it needs extra permissions it should run from there for every user.

Resources