How would I capture the time down the second into a variable using a batch file. My script right now looks like this.
CLS
#ECHO OFF
set yy=%date:~-4%
set mm=%date:~-7,2%
set dd=%date:~-10,2%
set newdate=%dd%%mm%%yy%
echo %newdate%
I've captured the date, and it prints exactly what I need, but now I need to append the time to the variable. How would I do it?
#ECHO OFF
CLS
set yy=%date:~-4%
set dd=%date:~-7,2%
set mm=%date:~-10,2%
set newdate=%dd%%mm%%yy%_%Time:~0,8%
set newdate=%newdate::=%
echo %newdate%
pause
Read the time into a variable first to get a snapshot so that it doesn't keep ticking while you're extracting the fields. Then use the set substring operators to extract what you want.
The time has the format HH:MM:SS.MS (at least in my en-us locale). One gotcha is that the hour field might start with a leading space, so you need an if condition to change it to a leading zero or to remove the space.
set "current_time=%time%"
set "hour=%current_time:~0,2%"
if "%current_time:~0,1%"==" " set "hour=0%current_time:~1,1%"
set "min=%current_time:~3,2%"
set "sec=%current_time:~6,2%"
set "ms=%current_time:~-2%"
set "newtime=%hour% %min% %sec% %ms%"
echo %newtime%
If you want to remove the front space in the hour instead of changing it to a leading zero, then you'd do this instead:
set "hour=%current_time:~0,2%"
if "%current_time:~0,1%"==" " set "hour=%current_time:~1,1%"
This worked for me:
CLS
#ECHO OFF
set yy=%date:~-4%
set mm=%date:~-7,2%
set dd=%date:~-10,2%
set newdate=%dd%%mm%%yy% & time /T
echo %newdate%
pause
You can run multiple commands by adding the & symbol. If you wish to only append time, if the first command suceed, use && instead.
Related
Each time I open the batch file, I would like it to read the information currently stored in the text file, and then apply that stored information it pulled to calculating a new integer.
I'm trying to figure out how to get a number copied from a text file, stored as a variable, and then updated to a new integer in that text file, say adding 1 to the value.
I've been sifting through information online, and everything seems to point in a different direction.
Here is a test code I've gotten from digging thus-far:
set file="Test.txt"
set /a _Counter =< %file%
echo:%_Counter%
set /a "_Update=%_Counter%+1"
echo:%_Update% >%file%
timeout /t 10
For some reason when I try to get the information for the counter, it doesn't pull any data from the text file, and I'm left with this line output by the batch file.
F:\Users\Test\Documents\JumbledDirectory> set /a _Counter = Directory\Test.txt 0<F:\Users\Test\Documents\Jumbled
The most common answer I've seen is to use something along the lines of
set /p _Counter=< Test.txt
echo %_Counter%
As seen here: Windows batch command(s) to read first line from text file
But upon doing this I've either ended up with
echo:%_Counter%
being completely blank, or it defaults to 0 each time.
Any help would be appreciated as I've sadly been trying to find how to get this simple function for around 6 hours now.
#ECHO Off
SETLOCAL
set "file=q72474185.txt"
set /p _Counter=< "%file%"
echo:%_Counter%
set /a _Update=_Counter+1
echo:%_Update% >"%file%"
TYPE "%file%"
GOTO :EOF
When you use the point-click-and-giggle method of executing a batch, the batch window will close if a syntax-error is found or the script runs to completion. You can put a pause after statements and home in on the error, but better to open a 'command prompt' and run your batch from there so that the window remains open and any (error) messages will be displayed.
The error message would be about missing operand.
I've changed the filename as I track each question I respond to with its own set of data.
Use set "var=value" for setting string values - this avoids problems caused by trailing spaces. Don't assign a terminal \, Space or " - build pathnames from the elements - counterintuitively, it is likely to make the process easier.
set /a does not expect input from anywhere, it simply performs the calculation and assigns the result. Quotes are not necessary so I've removed them. % are also not required in a set /a but can be required if you are using delayedexpansion.
set /p expects input, so I've used that to read the file. Note that set /a disregards spaces, but set and set /p will include the space before the = in the variablename assigned, and _Counter & _Counter are different variables.
So having the batch in the same directory as the textfile this will work:
REM get input of file as Counter
set /p Counter=<number.txt
REM add a number to Counter and assign it as Counter
set /a "Counter=%Counter%+3
REM empty the file
break>number.txt
REM write Counter in the file
echo %Counter% >> number.txt
i need to get the last 2 tokens from a variable in Batch
the variable is
Rastreando a rota para user722-PC [192.168.1.106]
the output i need is a variable containing
user722-PC
and another containing
[192.168.1.106]
and no, i cant use
for /f "tokens=5,6 delims= " %%a in ("%variable%") do set host=%%a & set ip=%%b
echo %ip% %host%
because in this case i specified tokens 5,6 and what i want to to is to get the last 2 tokens dynamicaly, so i must NOT specify any token numbers mannualy
// this way i can get only the last token, considering that the delimiter is a space, and the delimiter cant be changed
FOR %%a in (%variable%) do set lastPart=%%a
ECHO %lastPart%
#echo off
setlocal EnableDelayedExpansion
set "var=Rastreando a rota para user722-PC [192.168.1.106]"
set "last=%var: =" & set "lastBut1=!last!" & set "last=%"
SET last
Hint: run this program with #echo on
#ECHO OFF
SETLOCAL
SET "variable=Rastreando a rota para user722-PC [192.168.1.106]"
FOR %%a IN (%variable%) DO CALL SET "lastbut1=%%lastpart%%"&SET "lastpart=%%a"
SET last
GOTO :EOF
calling the set re-parses it, so set "lastbut1=%lastpart%" is executed in a subshell.
This could also be done using delayedexpansion about which there are many articles on SO.
The short explanation for how it works is *** MAGIC ***
A longer explanation is this:
the commands CALL SET "lastbut1=%%lastpart%%" and SET "lastpart=%%a" are executed in that sequence for each element in %variable% which is a simple string with elements separated by spaces, tabs, commas or semicolons.
The & is used to separate the commands and serves merely to allow many commands to be specified on he same physical line. The code could also be written
(
CALL SET "lastbut1=%%lastpart%%"
SET "lastpart=%%a"
)
which is exactly the same; the parentheses are required and enclose the individual commands. There an additional syntax-requirement here. The opening ( must be on the same physical line as the do.
The CALL SET "lastbut1=%%lastpart%%" executes SET "lastbut1=%lastpart%" in a sub-shell, setting lastbut1 to the current value of lastpart. Then the SET "lastpart=%%a" sets lastpart to the value in %%a.
So, on the next iteration, lastbut1 gets the value that was applied to lastpart in the prior iteration, and so on until the very last time, when lastpat acquires the very last item on the list and lastbut1 has just been assigned the value just before that.
Hence, to get lastbut2, all you'd need would be to use
FOR %%a IN (%variable%) DO call set "lastbut2=%%lastbut1%%&CALL SET "lastbut1=%%lastpart%%"&SET "lastpart=%%a"
which sets lastbut2 from lastbut1, lastbut1 from lastpart and lastpart from %%a in that sequence.
Okay, so I want to be able to set my variable "EQP1" to whatever "chareqp1.txt" has printed in it. I have this, but it doesn't work, when I echo the variable it is saved as the text "C:\Users\Slots\Slot1\chareqp1.txt".
set /a "EQP1=C:\Users\Slots\Slot1\chareqp1.txt"
What's wrong with it?
Use set /p instead of set /a
Set /P "EQP1="<"C:\Users\Slots\Slot1\chareqp1.txt"
You are using the wrong parameter, it's set /p and you need redirection to read the first line from a file.
set /p "EQP1=" <"C:\Users\Slots\Slot1\chareqp1.txt"
If it's not the first line you'll have to parse the file with for /f and maybe findstr
I am trying to print the value in BUILD_VER using below code,for some reason instead of the value in BUILD_VER ,"BUILD_VER" gets printed,
can anyone suggest why is it so?
#echo off
REM set $NetPath="Z:\Build_ver\build_ver.txt"
set $NetPath="\\Network\Build_ver\build_ver.txt"
set /p version=<\\Network\Build_ver\build_ver.txt
set $BUILD_VER= %version%
echo BUILD_VER
#echo off
REM set $NetPath="Z:\Build_ver\build_ver.txt"
set $NetPath="\\Network\Build_ver\build_ver.txt"
set /p version=<\\Network\Build_ver\build_ver.txt
set BUILD_VER=%version%
echo %BUILD_VER%
You need to encase variables in % (percent) symbols, to output their values.
I also took out the dollar symbol. If you included it the last two lines would be -
set $BUILD_VER=%version%
echo %$BUILD_VER%
It's just a bit redundant.
In this qustion, Save and Load .bat game I used Mat's answer.
But now I have a problem with "saving" numbers using said answer.
If the variable is not double digits (for example 1 or 0) it will "save" the variable as " " and thus will crash the game whenever you do anything that needs that variable. The game sets the variable fine before that.
For example if I pick up the rag, then type Inv, it will say I'm holding the rag. If I then save and load again, then type Inv, it wont say I'm holding anything!
It also won't echo "Nothing" which it should do if %raghave% = 00
(I also have the save file open in Notepad++ and so can see that set RagHave=)
(Also if I use Mat's code with the spaces, then the variable is set as "set RagHave=1" and so adds a space at the end)
The problem is Mat's solution!
For better understanding I repeat his solution
#echo #ECHO OFF > savegame.cmd
#echo SET ITEMS=%ITEMS% >> savegame.cmd
#echo SET HEALTH=%HEALTH% >> savegame.cmd
#echo SET MONEY=%MONEY% >> savegame.cmd
In my opinion, it has multiple disadvantages.
The # prefix isn't necessaray.
The redirection is repeated for each line (I don't like redundancy).
It needs the spaces, as without spaces you got problems with numbers.
Sample with items=1
#echo set ITEMS=1>>savegame.cmd
This results not in writeing set items=1 it writes set items= to 1>>savegame.cmd 1>> is the standard stream.
You can solve all problems with
(
echo #ECHO OFF
echo SET "ITEMS=%ITEMS%"
echo SET "HEALTH=%HEALTH%"
echo SET "MONEY=%MONEY%"
) > savegame.cmd
The quotes are used to ensure that "hidden" spaces after the set are ignored.
Btw. It's a bad idea to use a construct like if %raghave% = 00, (you need two equal signs), as 00 isn't a normal number you can't count or calculate with it, it's better to use 0 instead.
Then also this should work
set /a items=0
set /a items=items+1
set /a items=items-1
if %items%==0 echo There are no items