ok, I have 2 .txt files. "server.txt" and "local.txt", each have a number,
e.g
2 = server.txt
and
1 = local.txt.
I want so bat script reads both files and compares the numbers, another e.g
set local=1
set server=2
if %local% EQU %server% (
ECHO VERSION UP TO DATE
) else (
ECHO OUT OF DATE
)
pause
how would i set local to the number in the local.txt same for server.txt?
I assume the contents of the file local.txt as follows and that server.txt is similar.
1 = local.txt
Then, you can use something like the following to set local and server.
FOR /F "tokens=1 delims==" %%n IN (server.txt) DO SET server=%%n
FOR /F "tokens=1 delims==" %%n IN (local.txt) DO SET local=%%n
Related
How to remove the character from the variable in a batch script.
I have to create a CSV file for which i am using a batch script. I have a last field where the value comes as "0," or "1,".
I have to remove the , and just take the value 0 or 1.
I have written a code like :
<nul set /p test=%%S >> %fileDate%
how do i remove the , from the variable
for %%f in (*.sent) do (
for /f "tokens=3" %%S IN ('FIND "Status" "%%f"') DO (
<nul set /p test=%%S >> %fileDate%
set test=%%S
set test=%test:,= %
echo %test% >>%fileDate%
goto tests2
)
:tests2
)
this is my code , i have also tried to use the sub string and then used the echo to send the output to the file . But as i have used #ECHO OFF it is sending echo is off in the CSV file
I need some help to adjust a simple text.
I have generated a file from the command getmac.
The file looks like this:
1
2 F0-79-59-62-20-EB \Device\Tcpip_{F4744941-2A87-4DFA-A61B-393B8830B0C3}
3
My goal is to get to this point:
1 F0-79-59-62-20-EB
2
3
I.e. delete every string after the MAC and move it to the first line
or just read the mac and insert it to a variable without editing it at all
Thanks
Best regards,
Mike
C:\Users\Test\Desktop>SETLOCAL EnableDelayedExpansion
C:\Users\Test\Desktop>FOR /F "tokens=*" %a IN (c:\mymac.txt) DO (
SET line=%a
IF "!line:~0,2!" == "2 " SET macline=%a
)
C:\Users\Test\Desktop>(
SET line=F0-79-59-62-20-EB \Device\Tcpip_{F4744941-2A87-4DFA-A61B-
}
IF "!line:~0,2!" == "2 " SET macline=F0-79-59-62-20-EB \Device\Tcpip_{F474494
1-2A87-4DFA-A61B-393B8830B0C3}
)
C:\Users\Test\Desktop>FOR /F "tokens=2 delims= " %m IN ("") DO (
SET mac=%m
ECHO
)
C:\Users\Test\Desktop>pause
Press any key to continue . . .
The format of your generated file seems to be totally different from the output of the getmac command - at least, it is on my machine.
You should be able to use (from the prompt - but most responses on SO are of a batch file)
FOR /F %a IN ('getmac') DO SET mac=%a
echo %mac%
If I've understood the question right, this should work:
#ECHO OFF
SETLOCAL EnableDelayedExpansion
FOR /F "tokens=*" %%a IN (input.txt) DO (
SET line=%%a
IF "!line:~0,2!"=="2 " SET macline=%%a
)
FOR /F "tokens=2 delims= " %%m IN ("!macline!") DO (
SET mac=%%m
)
At this point the mac ist stored in the %mac% variable. If you want to output it into your file, append this to the code:
ECHO 1 %mac% > output.txt
ECHO. >> output.txt
ECHO 2 >> output.txt
ECHO 3 >> output.txt
If you want to reuse the mac, it's stored in %mac%.
I'm trying to write a script that will ping 10.x.x.185 for over 600 locations, and then export to a text file. I know >> can be used to append to a text file.
The IP ranges are as follows: 10.0.1.185, all the way up to 10.50.2.185, incrementing by 1 each time. I see an issue being at 10.0.255.185...
Bonus if I can have a pass and fail in two different files.
Any ideas?
The loop can be accomplished with two for /L loops, and piping output to two separate files is a little trickier, but still possible.
#echo off
:: Get the vast majority of the IP addresses
for /L %%A in (0,1,49) do (
for /L %%B in (0,1,255) do (
(ping 10.%%A.%%B.185>nul&&echo 10.%%A.%%B.185>>success.txt)||echo 10.%%A.%%B.185>>failure.txt
)
)
:: Get the 10.50.x.185 range separately since it only goes from 0 to 2
for /L %%A in (0,1,2) do (
(ping 10.50.%%A.185>nul&&echo 10.50.%%A.185>>success.txt)||echo 10.50.%%A.185>>failure.txt
)
The && means "only do this part if the prior command was successful," and the || means "only do this if the prior command failed.
The usual way in batch files to iterate over a list of consecutive numeric values is to use a for /l command. In your case, as you need two different series, you will need two nested for /l loops, one for each octet.
But as there are differences in the inner loop depending on the value of the outer loop, i will use environment variables to define how the inner loop should behave for each of the outer values.
#echo off
setlocal enableextensions enabledelayedexpansion
rem Get a carriage return into a variable to later show progress in console
for /f %%a in ('copy "%~f0" nul /z') do set "CR=%%a"
rem Define the ranges that will be used for each of the network octets
rem Values are those in for /l command: start step end
set "octetA=0 1 50"
for /l %%a in (%octetA%) do set "octetB%%a=1 1 255"
set "octetB50=1 1 2"
rem Iterate over the addresses.
rem Two streams are used to send the sucess/failure addresses to
rem the correct log file. That way we avoid having to open/write/close
rem each file for each write operation.
7>"sucess.txt" 8>"failure.txt" (
for /l %%a in (%octetA%) do for /l %%b in (!octetB%%a!) do (
rem Show current ip being tested to console
<nul set /p"=Testing 10.%%a.%%b.185!CR!"
rem Execute ping command and send the address to the
rem adecuate output stream depending on sucess/failure
( ping -n 1 10.%%a.%%b.185 2>nul | find "TTL=" >nul
) && (>&7 echo 10.%%a.%%b.185)||(>&8 echo 10.%%a.%%b.185)
)
)
echo IP address testing finished
I need to identify all files that begin with US_ within a folder, and then copy those to multiple numbered folders.
I'm using the following code for the copy, but cannot figure out how to identify the files to be copied. %ROOTPATH% has been define previously:
ECHO Which workstation are you copying user settings FROM?
SET /p WSFROM="Workstation Number?: "
ECHO Enter a range (eg. 5, eg. 10-15)
SET /p WSTO="Workstation Numbers?: "
for /f "usebackq delims=- tokens=1,2" %%a in ('%WSTO%') do (
set "LOWERWS=%%a"
set "UPPERWS=%%b"
)
if not "%UPPERWS%"=="" goto :copy_range
:copy_range
FOR /L %%i IN (%LOWERWS%, 1, %UPPERWS%) DO (
xcopy %ROOTPATH%\WORKSTATIONS\%WSFROM%\US_*.DAT %ROOTPATH%\WORKSTATIONS\%%i\US_*.DAT %%G)
This line should work with your code:
xcopy "%ROOTPATH%\WORKSTATIONS\%WSFROM%\US_*.DAT" "%ROOTPATH%\WORKSTATIONS\%%i\"
I have made a character file in which my game pulls data and variables from. Looks like so:
playerName= Marche
playerRace= Elf
playerHP= 100
playerSPD= 200
playerATK= 120
playerDEF= 70
Final Fantasy reference anyone...? Anyway, when the character levels up, I need a batch script to find the string "playerHP= 100". playerHP is set as a variable within the script. Basically, it takes the current health, and multiplies it by 120%, increasing the number. How do I echo the result of that math to replace the current number?
For example if that didn't make any sense, I have 100 health. I level up, thus increasing my health stat by 120%, so now I have 120 health. I would want to find the string "playerHP= 100" and replace it with "playerHP= 120".
If it can be avoided I don't want to download any other commands (I've seen sed a few times). Thanks much
EDIT: Instead of searching for the string and replacing I took jeb's advice and just deleted the file and re-echoed all of the data. It ended up looking like this:
set /a LeveledUpPlayerHP=(%ppHP%* 12) / (10)
set /a LeveledUpPlayerSPD=(%ppSPD%* 12) / (10)
set /a LeveledUpPlayerATK=(%ppATK%* 12) / (10)
set /a LeveledUpPlayerDEF=(%ppDEF%* 12) / (10)
echo Updating stats...
del "C:\Users\%USERNAME%\Desktop\CMDRPG\player\playerData.dll
ping 1.1.1.1 -n 1 -w 500 > nul
echo playerName= %playerName%>playerData.dll
echo playerRace= %playerRace%>>playerData.dll
echo playerHP= %LeveledUpPlayerHP%>>playerData.dll
echo playerSPD= %LeveledUpPlayerSPD%>>playerData.dll
echo playerATK= %LeveledUpPlayerATK%>>playerData.dll
echo playerDEF= %LeveledUpPlayerDEF%>>playerData.dll
The playerName and playerRace are all loaded in prior to this section of the code. Ping is used just as a wait function to let the file delete before echoing new data. Seems to work okay. Thanks all
try this (output is in %inifile%.new):
#ECHO OFF &SETLOCAL ENABLEDELAYEDEXPANSION
SET "inifile=file"
FOR /f %%a IN ('^<"%inifile%" find /c /v ""') DO SET /a lines=%%a
< "%inifile%" (
FOR /l %%a IN (1,1,%lines%) DO (
SET "line="
SET /p "line="
IF NOT "!line:playerHP=!"=="!line!" (
FOR /f "tokens=2delims= " %%b IN ("!line!") DO SET /a HP=%%b*12/10
SET "line=playerHP= !HP!"
)
ECHO(!line!
))>"%inifile%.new"
Input/output:
>type file
playerName= Marche
playerRace= Elf
playerHP= 100
playerSPD= 200
playerATK= 120
playerDEF= 70
>test.bat
>type file.new
playerName= Marche
playerRace= Elf
playerHP= 120
playerSPD= 200
playerATK= 120
playerDEF= 70
Presumeably it does not matter what order the values appear in you file. If that is so, then the following will effectively "edit" your file.
It first uses FINDSTR to isolate the current playerHP value, and FOR /F to parse out the value from the string. SET /A increments the playerHP. Then a new file is created using FINDSTR to write all the current values except for playerHP, and then the new playerHP info is appended to the end.
#echo off
set "file=gameData.txt"
for /f "tokens=2 delims==" %%N in ('findstr /bl "playerHP=" "%file%"') do set /a "newHP=%%N*120/100"
>"%file%.mod" (
findstr /blv "playerHP=" "%file%"
echo playerHP=%newHP%
)
move /y "%file%.mod" "%file%" >nul
But why go to all that trouble. I should think you already have all your values in memory as environment variables. Simply make sure that all variables that are to be stored in a file start with a common unique prefix. In your example, they all start with "player", but you probably want something a little more generic - perhaps a symbol like #. So you could have #playerName, #playerRace, etc.
Simply update the values in memory as needed. When it comes time to save the current game state to a file, then all you need to do is
>"gameData.txt" set #
A brand new file will be created each time containing all the current values.
To load the values, you do exactly what jeb suggested:
for /f "usebackq delims=" %%A in ("gameData.txt") do set %%A
To save your data in a file, you could use a block like
(
echo playerName=%playerName%
echo playerRace=%playerRace%
echo playerHP=%playerHP%
) > player.ini
And to load it you could use
for /F "delims=" %%L in (player.ini) do set "%%L"