I made a names.bat file
that will make a folder called profiles then add a batch in the profiles folder to view the profiles and I'm having a problem with the creation of program veiwer.bat
the names.bat contains this code:
#echo off
TITLE Profiles
SET /a YEAR=%DATE:~6,4%
if not exist Profiles mkdir Profiles
CD Profiles
ECHO #echo off>"Profile Viewer.bat"
ECHO :profile>>"Profile Viewer.bat"
ECHO title Profile Viewer>>"Profile Viewer.bat"
ECHO SET /P NAME=Search names:>>"Profile Viewer.bat"
ECHO call %NAME%.bat>>"Profile Viewer.bat"
ECHO goto profile>>"Profile Viewer.bat"
GOTO RES
:RES
SET /P NAME=State your name:
ECHO #echo off>%NAME%.txt
ECHO title %NAME%>>%NAME%.txt
ECHO echo Name:%NAME%>>%NAME%.txt
SET /P AGE=State your Age:
ECHO echo Age:%AGE%>>%NAME%.txt
SET /a YOB=%YEAR%-%AGE%
SET /P A=Where you born in %YOB%(Y/N)
IF %A%==Y goto YES
IF %A%==y goto YES
IF %A%==N goto NO
IF %A%==n goto NO
:NO
SET /P YOB=State your Year of birth:
ECHO echo Birth Date:%YOB%>>%NAME%.txt
goto CONTINUE
:YES
ECHO echo Birth Date:%YOB%>>%NAME%.txt
goto CONTINUE
:CONTINUE
ECHO pause>>%NAME%.txt
rename *.txt *.bat
start %NAME%.bat
%SystemRoot%\explorer.exe "C:\Users\%username%\Desktop\Profiles"
echo Here is your profile
pause
so I edit "Profile Viewer.bat" to see if it works it has this
#echo off
:profile
title Profile Viewer
SET /P NAME=Search names:
call .bat
goto profile
Where did the %NAME% variable go?
I want it to be in the profile viewer.bat
i know its an empty variable i want it to literally say in the code this;
#echo off
:profile
title Profile Viewer
SET /P NAME=Search names:
call %NAME%.bat
goto profile
I know I could make it myself but I want name.bat to do it for me
ECHO call %NAME%.bat>>"Profile Viewer.bat"
When this line is executed, the %NAME% variable is expanded. Since it hasn't been defined yet, it expands to nothing. Thus, we get the erroneous line in Profile Viewer.bat.
To prevent %NAME% from being expanded, we need to tell the command interpreter that we want an actual % instead of starting a variable. So, we need to escape the % like so:
ECHO call %%NAME%%.bat>>"Profile Viewer.bat"
Related
Trying to obtain an element in a list by its index, using batch script. Here is the code:
#Echo off
setlocal EnableDelayedExpansion
set acc[0]=default
set acc[1]=Account_2
set acc[2]=Account_3
set acc[3]=Account_4
set acc[4]=Account_5
if exist interator.txt (
set /p i=<interator.txt
echo "read: !i!"
echo "!acc[%i%]!"
REM start cmd /c setx AWS_PROFILE !acc[%i%]!
REM start cmd /k python script.py
set /A i=i+1
(echo !i!)>interator.txt
echo "write: !i!"
) else (
(echo 0)>interator.txt
)
Output Received:
"read: 0"
""
"write: 1"
As setx requires the CMD session to be closed, for affect to take place. I am trying a different approach to automate some regular stuff.
Expected Output:
"read: 0"
"default"
"write: 1"
#Echo off
setlocal EnableDelayedExpansion
set "acc[0]=default
set "acc[1]=Account_2"
set "acc[2]=Account_3"
set "acc[3]=Account_4"
set "acc[4]=Account_5"
if exist q65771965.txt (
set /p i=<q65771965.txt
echo "read: !i!"
FOR %%a IN (acc[!i!]) DO (
ECHO "!%%a!"
echo start cmd /c setx AWS_PROFILE "!%%a!"
echo start cmd /k python script.py
)
set /A i=i+1
(echo !i!)
echo "write: !i!"
) else (
(echo 0)
)
GOTO :EOF
OK - small changes to allow this to work on my test environment:
Changed name of file from interator.txt to q65771965.txt (suits my environment)
Removed updating of data file so the modifications are shown on-screen.
Replaced REM start with ECHO start to show the start commands on-screen.
Subtle syntax-oriented change : Use set "var1=data" for setting values - this avoids problems caused by trailing spaces.
Significant change : insert a for loop to transfer indirect values to a metavariable (%%a) and use these.
Possibly-required : I don't use setx much, but I've some memory of the argument's needing to be "quoted"
The problem is, you used echo "%acc[!i!]%" within a codeblock. You need another layer of parsing, like call echo "%%acc[!i!]%%"
As an alternative, restructure your code, so the critical part isn't in a code block:
#Echo off
setlocal EnableDelayedExpansion
set acc[0]=default
set acc[1]=Account_2
set acc[2]=Account_3
set acc[3]=Account_4
set acc[4]=Account_5
if not exist interator.txt (
(echo 0)>interator.txt
goto :eof
)
set /p i=<interator.txt
echo "read: !i!"
echo "%acc[!i!]%"
set /A i=i+1
(echo !i!)>interator.txt
echo "write: !i!"
(this code is functionally identically to yours, just structured in another way)
(btw: it should probably iterator, not interator - but that's only spelling)
I'm trying to set multiple variables in a loop so the amount of variables can be infinite. For example, it will ask how many variables it will set and ask for each one. then it will display them. All it says is "echo is OFF" for each variable. The variables are numbers. when I do this with CMD, it works.
I am using batch on Windows 8.
Here is my code:
#echo off
title HANGMAN
color f0
:start1
cls
echo HANGMAN
echo -------
pause
cls
set /p LETTERS=Amount of letters:
set num=0
:a
cls
set /a num=%num%+1
set /p INSERT=%num% letter:
set %num%=%INSERT%
if %LETTERS%==%NUM% (
goto finish
) else (
goto a
)
:finish
cls
echo %INSERT%
echo %num%
echo %1%
echo %2%
echo %3%
echo %4%
echo %%num%%
pause
This part seems to not work:
set %num%=%INSERT%
I recently played the game "Dictator" and wanted to recreate it in a batch script. I just began creating it when I ran into an issue. I have made programs which allow the user to save their progress and load it. I copied this code from a different program and put it in mine. Saving does seem to work but when i edit the save file (.cmd) and edit one of the saved variables, once I load that file nothing seems to happen. For example I saved one file called key.cmd and edited it to make the "pol" variable equivalent to 100. Once I loaded that file it did say loaded but the pol variable was still set to the default (10). I dont understand as I have made a folder called save and key.cmd file is inside it. I have used my save and load code multiple times in the past and have never had any issues. Please help! Here is my code:
:Setup
#echo off
title Dictator
color 0a
:Save Variables
set pol=10
set bui=10
set low=10
set cor=10
set peo=10
set cri=10
:Main
cls
echo 1 - Save
echo 2 - Load
echo 3 - Police - %pol%
echo 4 - Buissnes Men - %bui%
echo 5 - Lower Government - %low%
echo 6 - Corruption - %cor%
echo 7 - People - %peo%
echo 8 - Criminals - %cri%
choice /c 12345678 /n /m ">>> "
if %errorlevel% equ 1 goto Save
if %errorlevel% equ 2 goto load
:Save
cls
set /p pin="PIN: "
(
echo set pol=%pol%
echo set bui=%bui%
echo set low=%low%
echo set cor=%cor%
echo set peo=%peo%
echo set cri=%cri%
) >> saves\%pin%.cmd
echo SAVED
pause >nul
goto main
:Load
cls
set /p pin="PIN: "
if exist saves\%pin%.cmd (
call saves\%pin%.cmd
echo %pol%
echo LOADED
pause >nul
) else (
echo INCORRECT PIN
pause >nul
)
goto main
At :Load you need to allow spaces in the file name. Most users won't know of this weakness, but in order to allow spaces (and a few other 'special' characters) to be read in the user input without breaking it, you need to add setlocal enableextensions enabledelayedexpansion to the beginning of your batch file and make the set /p pin="PIN: " statement into set /p "pin=PIN: " and replace the %'s with !'s where it calls for %pin%.
In the end, your code should look like:
:Setup
setlocal enableextensions enabledelayedexpansion
#echo off
title Dictator
color 0a
:Save Variables
set pol=10
set bui=10
set low=10
set cor=10
set peo=10
set cri=10
:Main
cls
echo 1 - Save
echo 2 - Load
echo 3 - Police - %pol%
echo 4 - Buissnes Men - %bui%
echo 5 - Lower Government - %low%
echo 6 - Corruption - %cor%
echo 7 - People - %peo%
echo 8 - Criminals - %cri%
choice /c 12345678 /n /m ">>> "
if %errorlevel% equ 1 goto Save
if %errorlevel% equ 2 goto load
:Save
cls
set /p "pin=PIN: "
(
echo set pol=%pol%
echo set bui=%bui%
echo set low=%low%
echo set cor=%cor%
echo set peo=%peo%
echo set cri=%cri%
) >> saves\!pin!.cmd
echo SAVED
pause >nul
goto main
:Load
cls
set /p "pin=PIN: "
if exist saves\!pin!.cmd (
call saves\!pin!.cmd
echo %pol%
echo LOADED
pause >nul
) else (
echo INCORRECT PIN
pause >nul
)
goto main
I am trying to make a batch script that makes a config file for each file i a specified folder.
After it has created the config files then it executes a program that uses a selected config file then converts the specified file in the config.
here is my current code:
:start
#echo off
cls
cd /d %~dp0
goto source
rem This sets up the working folder. I usually do this because I need to but I am
rem not sure about it this time...
:source
echo Please select the folder with the ARF files in it.
set /p source=E.G. C:\Users\Elliot\Desktop\toconvert:
goto dest
rem This sets the folder location for the ARF files.
rem I am going to add a menu for simplicity sake later.
:dest
echo Please select where the mp4 files should appear.
set /p dest=E.G. C:\Users\Elliot\Desktop\Converted:
goto cfgname
rem This sets the output folder for the converted files.
rem Again I will make a menu in the future.
:cfgname
goto createcfg
rem this sets the name of the CFG that will be created for the ARF file
:createcfg
echo [Console] >> %MP4%.cfg
echo inputfile=%source%\1.arf >> MP4.cfg
echo media=MP4 >> MP4.cfg
echo showui=1 >> MP4.cfg
echo [UI] >> MP4.cfg
echo chat=1 >> MP4.cfg
echo qa=0 >> MP4.cfg
echo largeroutline=1 >> MP4.cfg
echo [MP4] >> MP4.cfg
echo outputfile=%dest%\1.mp4 >> MP4.cfg
echo width=1024 >> MP4.cfg
echo height=768 >> MP4.cfg
echo framerate=10 >> MP4.cfg
rem This makes the config file that is required by the player to convert
:convert
cd C:\programdata\webex\webex\500
nbrplay.exe -Convert "%cfg%"
:end
echo Thanks for using Elliot Labs Auto Converter.
echo for feature requests please email: elliot-labs#live.com
pause | echo Press any key to exit...
exit
:preloop
set num=0
goto loop
:loop
cls
echo %num%
pause
set /a num+=1
if %num%==6 (goto end) ELSE goto loop
This only works for one file. How do I make it apply to a whole folder.
#echo off &setlocal
for %%a in ("%cd%\*.arf") do call:MakeCFG "%%~a"
goto:eof
:MakeCFG
setlocal
set "MP4=%~n1"
set "source=%~dp1"
set "filename=%~n1"
set "dest=X:\destination"
(
ECHO([Console]
ECHO(inputfile=%source%%filename%
ECHO(media=MP4
ECHO(showui=1
ECHO([UI]
ECHO(chat=1
ECHO(qa=0
ECHO(largeroutline=1
ECHO([MP4]
ECHO(outputfile=%dest%\%filename%.mp4
ECHO(width=1024
ECHO(height=768
ECHO(framerate=10
)>"%MP4%.cfg"
exit /b 0
What is wrong with this code? It says ECHO is off.
#ECHO off
set /p pattern=Enter id:
findstr %pattern% .\a.txt > result
if %errorlevel%==0 (
set var2= <result
echo %var2%
set var1=%var2:~5,3%
echo %var1% > test.txt
echo %var1%
) else (
echo error
)
del result
pause
Any help is appreciated.
If your variable is empty somewhere, it will be the same as having the command "echo" on its own, which will just print the status of echo.
To avoid this, you should replace all your echo commands with something like this:
echo var2: %var2%
That way, if %var2% is empty it will just print "echo var2:" instead of "echo off".
As Laurent stated, it's not a problem of the ECHO, it's a problem of your code.
In batch files, blocks are completely parsed before they are executed.
While parsing, all percent expansion will be done, so it seems that your variables can't be changed inside a block.
But for this exists the delayed expansion, the delayed expansion will be evaluated in the moment of execution not while parsing the block.
It must be enabled, as per default the delayed expansion is disabled.
#ECHO off
setlocal EnableDelayedExpansion
set /p pattern=Enter id:
findstr %pattern% .\a.txt > result
if %errorlevel%==0 (
set var2= <result
echo(!var2!
set var1=!var2:~5,3!
echo(!var1! > test.txt
echo(!var1!
) else (
echo error
)
del result
I used here the construct echo( instead of echo as this will ensure echoing an empty line even if the variable is empty.
Not sure, if this post is still read, but nevertheless.
You should try the following:
On top of the code right after #echo off you have to put in
setlocal enabledelayedexpansion
Additionally anywhere you want to use variables changed in a block of brackets (like For-Loops or If's) you have to change the %into ! to get
!varname!
This should be helping...
Greetings
geisterfurz007
First create a file a.txt in the same directory u have this batch file ... write some text in that...Note: only Windows 2000
Windows ME
Windows XP
Windows Vista
Windows 7 supports FINDSTR
set /p pattern=Enter id:
findstr %pattern% a.txt > __query.tmp
set /p result=<__query.tmp
if %errorlevel%==0 (
set var2= %result%
echo %var2%
set var1= %var2:~5,3%
echo %var1% > test.txt
echo %var1%
) else (
echo error
)
del __query.tmp
pause
run this bath file .. you will find a substring(start=5,length=3) of the first line of string you have in a.txt in a newly created file test.txt. Finally got it working !
The solution for your problem is to put the "echo"s after the if block is completed.
Try this:
#ECHO off
set /p pattern=Enter id:
findstr %pattern% .\a.txt > result
if %errorlevel%==0 (
set var2= <result
set var1=%var2:~5,3%
goto print
) else (
echo error
goto result
)
:print
echo %var2%
echo %var1% > test.txt
echo %var1%
:result
del result
pause
This way you can see the solution as you wanted.
Cheers! ;]