well I'm trying as title say> to make a score system. ive been googling. and tried different things but I simply cant get it to work
i want it to work like this: you type in your name. then it checks if the dll file exist in the players folder. if it doesn't, it will make it. and if it does. it will just overwrite.
if it does exist however. it will first retrieve the existing score value from the dll file.
then I want it to just add in 1 point by every time
you come to the :addscore section.
and each time it passes :addscore. this new value will then be saved to the file (replaced) and then this continues :P
heres the code I got so far: and as u can see. it does not retrieve the score from the file. and neither it will set the score value any higher than 1... any tips?
#echo off
:: User check
:usercheck
set /p usrn=Username:
set score=0
if exist "D:\General_menus\users\%usrn%\playerdb.dll" (
for /f %%A in ("D:\General_menus\users\%usrn%\playerdb.dll") do set score=%%A
echo Welcome back %usrn%
goto addscore
) else (
echo Hello new player %usrn%
set score=0
)
pause>nul
:: user check end
::====================================================
::add score
:addscore
set /a score+=1
echo your score is %score%
echo saving data
goto scorecheck
::add score end
::===================================================
:scorecheck
echo %score%
echo %score% > "\General_menus\users\%usrn%\playerdb.dll"
pause>nul
goto usercheck
Problem solved by: -#Thilo
fix: remove the bunny ears from line:" for /f %%A in ("D:\General_menus\users\%usrn%\playerdb.dll") do set score=%%A"
Related
I wanna make an if/else statement where if my variabel is empty it has to echo an text but it is never empty because it keeps giving me the previous value I wrote in cmd. How can I possibly solve this, I tried using startlocal and endlocal but I couldn't get it to work this is my code:
#echo off
set /p "Input= text:"
FOR %%a IN (%Input%) DO (
IF "%%a"=="" (echo Write atleast 1 word) else (
ECHO %%a
)
)
In this picture u can see that if I write something first and click enter it puts everything underneath each other but the second time I wrote nothing and got the same values back
Cmd picture
Remove whatever is in the input variable at the beginning.
#echo off
SET "Input="
set /p "Input= text:"
this is my very first post on this site but as I already found much help on stackoverflow, I now ask this question myself. I did not find an answer to this issue. My English isn't that good and as my Problem is quite complex, I hope you guys are all able to understand me. If not or if there are any questions that are needed to be able to help me, feel free to ask.
So I want to make a Batch file that moves files by the Extension (in my case, it's .pdf) but not all of them. I know there is this command:
move "Y:\Scan\*.pdf" "Y:\Scan\Archive\"
But this will move all pdf files to the Archive Folder and I don't want that.
To get a better overview what my plan is, a small description is needed I guess.
Imagine you have a home Server and your Computer is connected to it, a Printer and a scanner aswell. I have many documents in Folders in my home Office and I want to have them all on my Server. This is done trough scanning all Sheets of paper in These Folders. Everytime I scan something, this is saved as .pdf in a Folder on my local Computer, sorted by time and Looks like this: 2017-06-15-10-30.pdf
My current script is asking 3 times for userinput. One time for a village Name, one time for a street's Name and one time for the number of the house because the Folders for the Archive should be named like the corresponding house they are referring to. So I have These 3 Inputs in my Batch already and also the whole Folder Name saved in a new variable.
My plan is to now ask the user how many pdf's are corresponding to the last created Folder and whatever the user is typing, this amount of files will be stored in that Folder. To understand this a Little better, here is what i've done so far (my Batch code).
#echo off
cls
title Archive-Script (by Relentless)
color 0b
:reset
cls
REM This is the village's name
SET ON=""
REM This is the street's name
SET SN=""
REM This is the number of the house
SET HN=""
REM This is a variable to store whether the user wants to reset the variables or not (used later)
SET RESET=""
:start
cls
IF %RESET% == N echo To overwrite the village's name, please enter the name again and hit enter. If you don't want to overwrite the name, just hit enter!
IF NOT %RESET% == N echo Please enter the village's name (confirm by pressing enter):
IF %RESET% == N echo Current name: %ON%
set /p "ON="
echo The village's name was set to %ON%!
echo.
IF %RESET% == N echo To overwrite the street's name, please enter the name again and hit enter. If you don't want to overwrite the name, just hit enter!
IF NOT %RESET% == N echo Please enter the street's name (confirm by pressing enter):
IF %RESET% == N echo Current name: %SN%
set /p "SN="
echo The street's name was set to %SN%!
echo.
IF %RESET% == N echo To overwrite the house's number, please enter the number again and hit enter. If you don't want to overwrite the number, just hit enter!
IF NOT %RESET% == N echo Please enter the house's number (confirm by pressing enter):
IF %RESET% == N echo Current number: %HN%
set /p "HN="
echo The house's number was set to %HN%!
echo.
echo.
echo.
echo.
echo.
SET FOLDERNAME="%ON%, %SN% %HN%"
echo The folder %FOLDERNAME% will now be created!
mkdir ""%FOLDERNAME%""
timeout /t 2 /nobreak
:pdfmove
cls
echo How many pdf-files are corresponding to the latest created folder?
set /p "PDF="
for /l %%x in (1,1,%PDF%) do (
echo %%x
move Y:\Scan\*.pdf Y:\Scan\%FOLDERNAME%\
)
:resetoption
cls
echo %ON%
echo %SN%
echo %HN%
echo.
echo Should the variables be resetted again? (J/N)
set /p "RESET="
IF %RESET% == J goto reset
IF %RESET% == N goto start
As you can see, I already tried to implement the move Option, but this will move ALL pdf files instead of one on each for Loop. Can anyone help me with that? I guess the code is pretty simple to understand.
Greetz Relentless
for /l %%x in (1,1,%PDF%) do (
set "moved="
for /f "delims=" %%a in ('dir /b /a-d "y:\scan\*.pdf" '
) do if not defined moved (
echo %%x : %%a
set "moved=Y"
move "Y:\Scan\%%a" Y:\Scan\%FOLDERNAME%\
)
)
I believe what you want to do is to move a user-input number of pdfs.
The above change will, for each %%x, first set the flag moved to nothing, then perform a directory-scan, in /b basic form (filename only) /a-d with no directorynames. Each of the filenames present in the directory will be assigned to %%a in turn. The delims= option allows filenames that contain spaces to be processed.
When the first file is found, its name is echoed with the sequence number and the flag is set to Y (any non-empty string will do). The file is then moved.
When the for...%%a... processes the next filename, moved is now set to a value, so no more file-moves are performed.
The entire process is then repeated for each value of %%x, so %PDF% moves of the first .pdf filename found in the source directory will be moved because one is moved each time and the dir command will not find the file that was last moved because well - it's been moved and hence is no longer in the source directory.
OK, here's a fix to produce a variable number of spaces
SET "tspaces=T"
:addspace
SET "tspaces=%tspaces% "
CALL SET "testspace=%%tspaces:~%pdf%%%"
IF "%testspace%" neq " " GOTO addspace
FOR /L %%x IN (1,1,%pdf%) DO (
set "moved="
for /f "delims=" %%a in ('dir /b /a-d "y:\scan\*.pdf" '
) do if not defined moved (
echo %%x : %%a
set "moved=Y"
call move "Y:\Scan\%%a" "Y:\Scan\%FOLDERNAME%\olddocumen%%tspaces:~0,%%x%%.pdf"
)
)
The first part sets up tspaces to t+a sufficient number of spaces.
Note that in the second part, the move is now called which resolves the variable-substring. Since %%x varies from 1 to %pdf% then the selection from tspaces is %%x characters - it has a minimum of 1, when the final character of olddocument is required; thereafter a number of spaces follow, so the t is the initial value of tspaces.
So basically I have a batch file that requires alot of user input. I was wondering if it was possible to have any filler data already present when the question is asked, and if the user needs to change something, they can go edit that data. For example
And then the user enter their first and last name.
But is it possible to start with a default name that the user can go back and edit if they need?
This probably isn't necessary, But this is the code I use for the user input.
Set /p "Author=Please enter your name: "
And I understand for the Author it wouldn't make much sense to have preset data, but there are other instances where it would be useful for me to do this. Is it possible?
nearly impossible to edit a preset value with pure batch, but you can easily give a default value (works, because set /p is not touching the variable, if input is empty)
set "author=First Last"
set /p "author=Enter name or press [ENTER] for default [%author%]: "
echo %author%
The method below have the inconvenience that the screen must be cleared before the user enter the data, but I am working trying to solve this point:
EDIT: I fixed the detail of the first version
#if (#CodeSection == #Batch) #then
#echo off
rem Enter the prefill value
CScript //nologo //E:JScript "%~F0" "First Last"
rem Read the variable
echo -----------------------------------------------------------
set /P "Author=Please enter your name: "
echo Author=%Author%
goto :EOF
#end
WScript.CreateObject("WScript.Shell").SendKeys(WScript.Arguments(0));
For further details, see this post.
You can set the var first and then prompt the user only if it's not defined like so:
set Author=First Last
if not defined Author set /p "Author=Please enter your name: "
You can also do this backwards where you can define a value if the user didn't define it, like so:
set /p "Author=Please enter your name: "
if not defined Author set Author=First Last
There Is another way yet to achieve this. It uses vbs script to get input and assign it to a variable, -The script can be created within and called from .bat files.
I developed this method as an alternative to accepting user input from set /p in order to validate input and prevent setting of variables with spaces or special characters.
*Validation methods omitted as does not relate to the question
Best method is to have the script generator as a secondary .bat file that you can call, as it allows for a lot of versatility regarding the information the vbs input box conveys, as well as being able to be used in any circumstance within one or more .bat files you want to be able to control input defaults with.
In your main program, when Preparing to get input-
REM Define title:
Set inputtitle=The title you want the input box to have
REM Declare variable to be assigned:
Set variableforinput=VariableName
REM Define the default Variable Value:
Set defaultinput=VariableName's Desired Default Value
REM getting the Input:
CALL "inputscript.bat" %inputtitle% %variableforinput% %defaultinput%
inputscript.bat:
#echo off
SET inputtitle=%~1
SET variableforinput=%~2
SET defaultinput=%~3
SET %variableforinput%=
SET input=
:main
REM exit and cleanup once variable is successfully defined
IF DEFINED input GOTO return
REM this creates then starts the VBS input box, storing input to a text file.
(
ECHO Dim objFSO 'File System Object
ECHO Set objFSO = CreateObject("Scripting.FileSystemObject"^)
ECHO Dim objTS 'Text Stream Object
ECHO Const ForWriting = 2
ECHO Set objTS = objFSO.OpenTextFile("%userprofile%\getinput.txt", ForWriting,
True^)
ECHO objTS.Write(InputBox("Please enter %variableforinput% to continue.","%inputtitle%","%defaultinput%",0,0^)^)
ECHO objTS.Close(^)
ECHO Set bjFSO = Nothing 'Destroy the object.
ECHO Set objTS = Nothing 'Destroy the object.
) >GetInput.vbs
START GetInput.vbs
REM a pause that allows time for the input to be entered and stored is required.
cls
ECHO (N)ext
CHOICE /T 20 /C nz /N /D n >nul
IF ERRORLEVEL ==2 GOTO main
IF ERRORLEVEL ==1 GOTO loadinput
:loadinput
IF NOT EXIST "%userprofile%\getinput.txt" GOTO invInput
<%userprofile%\getinput.txt (
SET /P input=
)
IF NOT DEFINED input GOTO invInput
REM opportunity for other validation of input before returning to main.
GOTO main
:invInput
SET input=
IF EXIST "GetInput.vbs" (
DEL /Q "GetInput.vbs"
)
REM ends the vbs script ready for the next attempt to provide input
taskkill /pid WScript.exe /T >nul
Timeout 1 >nul
GOTO main
REM assigns the input value to the variable name being set in Your Main program.
:return
SET %variableforinput%=%input%
SET input=
IF EXIST "%userprofile%\getinput.txt" (
DEL /Q "%userprofile%\getinput.txt"
)
IF EXIST "GetInput.vbs" (
DEL /Q "GetInput.vbs"
)
GOTO :EOF
I wrote an open-source Windows console program called editenv that replaces my older editv32/editv64/readline.exe utilities:
https://github.com/Bill-Stewart/editenv
Basically, editenv lets you interactively edit the value of an environment variable. One of my common use cases is to edit the Path environment variable for the current process:
editenv Path
editenv also provides the following convenience features:
--maskinput allows you to hide the typed input (note that this feature does not provide any encryption or security)
--allowedchars and --disallowchars allow you to specify which characters are allowed for input
--minlength and --maxlength let you choose a minimum and maximum length of the input string
--timeout lets you specify a timeout after which input is entered automatically
The most recent binaries are available here:
https://github.com/Bill-Stewart/editenv/releases
askingFile.cmd < response.txt
Take the input to the batch from the indicated file, one line per answer
This is my script. Whenever the player goes to this part of the script it says missing operator. But it only says it the first time. Help.
:M221
(
set /p Gold=<"%CD%\Data Files\gold.dat"
)
set /a gold=%gold%+60
echo You have earned 60 gold today. Time to go home and go to bed.
pause
cls
goto home3
:M321
(
set /p Gold=<"%CD%\Data Files\gold.dat"
)
set /a gold=%gold%+80
echo You have earned 80 gold today. Time to go home and go to bed.
pause
cls
goto home3
:M421
(
set /p Gold=<"%CD%\Data Files\gold.dat"
)
set /a gold=%gold%+150
echo You have made a spectacular sword and earned 150 gold today. Time to go home and go to bed.
pause
cls
goto home3
Not really enough information.
I'd check the contents of gold.dat.
If the file does not exist, it will give you a different response.
If it is empty ( a zero-byte file) or contains simply a space newl-line or a new-line alone, the code will work happily.
If the file contains , and a newline, you'll get the error you report.
you should post the content of "%CD%\Data Files\gold.dat", and, btw. you should remove the part %CD%\ from the path, it is not neccesarry.
That's because gold may not be a number on your first pass!
I have a batch file and I want to include an external file containing some variables (say configuration variables). Is it possible?
Note: I'm assuming Windows batch files as most people seem to be unaware that there are significant differences and just blindly call everything with grey text on black background DOS. Nevertheless, the first variant should work in DOS as well.
Executable configuration
The easiest way to do this is to just put the variables in a batch file themselves, each with its own set statement:
set var1=value1
set var2=value2
...
and in your main batch:
call config.cmd
Of course, that also enables variables to be created conditionally or depending on aspects of the system, so it's pretty versatile. However, arbitrary code can run there and if there is a syntax error, then your main batch will exit too. In the UNIX world this seems to be fairly common, especially for shells. And if you think about it, autoexec.bat is nothing else.
Key/value pairs
Another way would be some kind of var=value pairs in the configuration file:
var1=value1
var2=value2
...
You can then use the following snippet to load them:
for /f "delims=" %%x in (config.txt) do (set "%%x")
This utilizes a similar trick as before, namely just using set on each line. The quotes are there to escape things like <, >, &, |. However, they will themselves break when quotes are used in the input. Also you always need to be careful when further processing data in variables stored with such characters.
Generally, automatically escaping arbitrary input to cause no headaches or problems in batch files seems pretty impossible to me. At least I didn't find a way to do so yet. Of course, with the first solution you're pushing that responsibility to the one writing the config file.
If the external configuration file is also valid batch file, you can just use:
call externalconfig.bat
inside your script. Try creating following a.bat:
#echo off
call b.bat
echo %MYVAR%
and b.bat:
set MYVAR=test
Running a.bat should generate output:
test
Batch uses the less than and greater than brackets as input and output pipes.
>file.ext
Using only one output bracket like above will overwrite all the information in that file.
>>file.ext
Using the double right bracket will add the next line to the file.
(
echo
echo
)<file.ext
This will execute the parameters based on the lines of the file. In this case, we are using two lines that will be typed using "echo". The left bracket touching the right parenthesis bracket means that the information from that file will be piped into those lines.
I have compiled an example-only read/write file. Below is the file broken down into sections to explain what each part does.
#echo off
echo TEST R/W
set SRU=0
SRU can be anything in this example. We're actually setting it to prevent a crash if you press Enter too fast.
set /p SRU=Skip Save? (y):
if %SRU%==y goto read
set input=1
set input2=2
set /p input=INPUT:
set /p input2=INPUT2:
Now, we need to write the variables to a file.
(echo %input%)> settings.cdb
(echo %input2%)>> settings.cdb
pause
I use .cdb as a short form for "Command Database". You can use any extension.
The next section is to test the code from scratch. We don't want to use the set variables that were run at the beginning of the file, we actually want them to load FROM the settings.cdb we just wrote.
:read
(
set /p input=
set /p input2=
)<settings.cdb
So, we just piped the first two lines of information that you wrote at the beginning of the file (which you have the option to skip setting the lines to check to make sure it's working) to set the variables of input and input2.
echo %input%
echo %input2%
pause
if %input%==1 goto newecho
pause
exit
:newecho
echo If you can see this, good job!
pause
exit
This displays the information that was set while settings.cdb was piped into the parenthesis. As an extra good-job motivator, pressing enter and setting the default values which we set earlier as "1" will return a good job message.
Using the bracket pipes goes both ways, and is much easier than setting the "FOR" stuff. :)
So you just have to do this right?:
#echo off
echo text shizzle
echo.
echo pause^>nul (press enter)
pause>nul
REM writing to file
(
echo XD
echo LOL
)>settings.cdb
cls
REM setting the variables out of the file
(
set /p input=
set /p input2=
)<settings.cdb
cls
REM echo'ing the variables
echo variables:
echo %input%
echo %input2%
pause>nul
if %input%==XD goto newecho
DEL settings.cdb
exit
:newecho
cls
echo If you can see this, good job!
DEL settings.cdb
pause>nul
exit
:: savevars.bat
:: Use $ to prefix any important variable to save it for future runs.
#ECHO OFF
SETLOCAL
REM Load variables
IF EXIST config.txt FOR /F "delims=" %%A IN (config.txt) DO SET "%%A"
REM Change variables
IF NOT DEFINED $RunCount (
SET $RunCount=1
) ELSE SET /A $RunCount+=1
REM Display variables
SET $
REM Save variables
SET $>config.txt
ENDLOCAL
PAUSE
EXIT /B
Output:
$RunCount=1
$RunCount=2
$RunCount=3
The technique outlined above can also be used to share variables among multiple batch files.
Source: http://www.incodesystems.com/products/batchfi1.htm
Kinda old subject but I had same question a few days ago and I came up with another idea (maybe someone will still find it usefull)
For example you can make a config.bat with different subjects (family, size, color, animals) and apply them individually in any order anywhere you want in your batch scripts:
#echo off
rem Empty the variable to be ready for label config_all
set config_all_selected=
rem Go to the label with the parameter you selected
goto :config_%1
REM This next line is just to go to end of file
REM in case that the parameter %1 is not set
goto :end
REM next label is to jump here and get all variables to be set
:config_all
set config_all_selected=1
:config_family
set mother=Mary
set father=John
set sister=Anna
rem This next line is to skip going to end if config_all label was selected as parameter
if not "%config_all_selected%"=="1" goto :end
:config_test
set "test_parameter_all=2nd set: The 'all' parameter WAS used before this echo"
if not "%config_all_selected%"=="1" goto :end
:config_size
set width=20
set height=40
if not "%config_all_selected%"=="1" goto :end
:config_color
set first_color=blue
set second_color=green
if not "%config_all_selected%"=="1" goto :end
:config_animals
set dog=Max
set cat=Miau
if not "%config_all_selected%"=="1" goto :end
:end
After that, you can use it anywhere by calling fully with 'call config.bat all' or calling only parts of it (see example bellow)
The idea in here is that sometimes is more handy when you have the option not to call everything at once. Some variables maybe you don't want to be called yet so you can call them later.
Example test.bat
#echo off
rem This is added just to test the all parameter
set "test_parameter_all=1st set: The 'all' parameter was NOT used before this echo"
call config.bat size
echo My birthday present had a width of %width% and a height of %height%
call config.bat family
call config.bat animals
echo Yesterday %father% and %mother% surprised %sister% with a cat named %cat%
echo Her brother wanted the dog %dog%
rem This shows you if the 'all' parameter was or not used (just for testing)
echo %test_parameter_all%
call config.bat color
echo His lucky color is %first_color% even if %second_color% is also nice.
echo.
pause
Hope it helps the way others help me in here with their answers.
A short version of the above:
config.bat
#echo off
set config_all_selected=
goto :config_%1
goto :end
:config_all
set config_all_selected=1
:config_family
set mother=Mary
set father=John
set daughter=Anna
if not "%config_all_selected%"=="1" goto :end
:config_size
set width=20
set height=40
if not "%config_all_selected%"=="1" goto :end
:end
test.bat
#echo off
call config.bat size
echo My birthday present had a width of %width% and a height of %height%
call config.bat family
echo %father% and %mother% have a daughter named %daughter%
echo.
pause
Good day.
The best option according to me is to have key/value pairs file as it could be read from other scripting languages.
Other thing is I would prefer to have an option for comments in the values file - which can be easy achieved with eol option in for /f command.
Here's the example
values file:
;;;;;; file with example values ;;;;;;;;
;; Will be processed by a .bat file
;; ';' can be used for commenting a line
First_Value=value001
;;Do not let spaces arround the equal sign
;; As this makes the processing much easier
;; and reliable
Second_Value=%First_Value%_test
;;as call set will be used in reading script
;; refering another variables will be possible.
Third_Value=Something
;;; end
Reading script:
#echo off
:::::::::::::::::::::::::::::
set "VALUES_FILE=E:\scripts\example.values"
:::::::::::::::::::::::::::::
FOR /F "usebackq eol=; tokens=* delims=" %%# in (
"%VALUES_FILE%"
) do (
call set "%%#"
)
echo %First_Value% -- %Second_Value% -- %Third_Value%
While trying to use the method with excutable configuration
I noticed that it may work or may NOT work
depending on where in the script is located the call:
call config.cmd
I know it doesn't make any sens, but for me it's a fact.
When "call config.cmd" is located at the top of the
script, it works, but if further in the script it doesn't.
By doesn't work, I mean the variable are not set un the calling script.
Very very strange !!!!