Computer Hope
Microsoft => Microsoft DOS => Topic started by: nothlit on April 30, 2008, 04:12:27 PM
-
*NAME RESET WARN MAX INIT EXPRESSION INCREMENT
PRIMESEQ 00###00000 00###30000 00###90000 0 SEQUENCE+INCREMENT 5
What I am after here, above I have a *cfg file that is stored locally on many client machines, I am wanting to write a bat file that will edit this file and figured out what the reset value is or more importantly the 3 digits that are marked as ### in other words I want to create a Variable that will set to %VAR:~94,3% based on the text in the file, but I have no clue how to pull out the needed information, I could try a find command, but am still learning as I go and any help would be greatly appreciated.
The reason for this rewrite, many of the customers have an old configuration where they run out of numbers and don't reset on time due to a small number between the warn and max, when I rewrite the *cfg file for example say the reset is already 0011100000 the warm and max would be 112 and would fix some client issues. Thanks for the help.
-
Anyone have an idea that would point me in the right direction? :)
-
the 3 digits that are marked as ### in other words I want to create a Variable that will set to %VAR:~94,3% based on the text in the file
I see 9 digits marked as ###.
-
More details. :)
In this file, I am trying to pull the reset value, for example lets say the reset value is 0012300000, I would want to pull out '123' and have that be my variable, then for the warn I would want to add +1 so the new value for the variable would be 124 and the max would also be 124, sorry for the confusion with the extra ### symbols.
-
Are the 3 digits always the ones specified by %VAR:~94,3%, that is, are they the 95rd, 96th and 97th characters in the string (in which the first chaarcter is character 0)?
In that case you can use set /a (a is for arithmetic) to extract them as a numerical variable and increment by 1 and test if the limit has been reached.
-
The first char should always be '00' then the number I need to pull, but the spacing could be off in the file so the ~94 could change.
-
Are they always like this...
One line with 14 "words" separated by spaces?
*NAME RESET WARN MAX INIT EXPRESSION INCREMENT PRIMESEQ 00###00000 00###30000 00###90000 0 SEQUENCE+INCREMENT 5
-
Correct
-
So, it's all on one line & you want the 3 red digits in the blue section?
And the numbers might change but the structure does not?
*NAME RESET WARN MAX INIT EXPRESSION INCREMENT PRIMESEQ 00###00000 00###30000 00###90000 0 SEQUENCE+INCREMENT 5
-
I might have misunderstood. It is probably two lines, meaning that the 'Enter' Key was probably pushed, but that would be a correct statement in saying the structure doesn't change, but the numbers do.
-
I might have misunderstood. It is probably two lines, meaning that the 'Enter' Key was probably pushed, but that would be a correct statement in saying the structure doesn't change, but the numbers do.
"probably" won't hack it
-
Ok I double checked it is two lines
-
Ok I double checked it is two lines
How do they split?
-
*NAME RESET WARN MAX INIT EXPRESSION INCREMENT
PRIMESEQ 00###00000 00###30000 00###90000 0 SEQUENCE+INCREMENT 5
They split after increment, the new lines starts with PRIMESEQ
-
This is test.cfg
*NAME RESET WARN MAX INIT EXPRESSION INCREMENT
PRIMESEQ 0012300000 00###30000 00###90000 0 SEQUENCE+INCREMENT 5
This is test.bat
@echo off
set filename=test.cfg
echo demo: show file
type %filename%
echo.
echo demo: show line containing PRIMESEQ
type %filename% | find "PRIMESEQ"
echo.
REM This does the work
REM get 2nd space-delimited token from line
for /f "tokens=1,2 delims= " %%T in ('type %filename% ^| find "PRIMESEQ"') do set string=%%U
echo demo: show 2nd token
echo %string%
REM get the 3 chars starting at offset 2 from start of string (i.e. chars 3, 4 & 5)
REM put it into an arithmetic variable
set /a number=%string:~2,3%
echo.
echo demo: show reset number
echo %number%
This is the output
C:\>test.bat
demo: show file
*NAME RESET WARN MAX INIT EXPRESSION INCREMENT
PRIMESEQ 0012300000 00###30000 00###90000 0 SEQUENCE+INCREMENT 5
demo: show line containing PRIMESEQ
PRIMESEQ 0012300000 00###30000 00###90000 0 SEQUENCE+INCREMENT 5
demo: show 2nd token
0012300000
demo: show reset number
123
-
Thank you thank you thank you.
Yes that will work perfectly. :) Thank you for all your help.
-
Thought of something. After running the BAT file I want to rename or delete the bat file so it can't be run again. I know I could set a variable using the %CD%, but I noticed that is the bat file is run from the command as follows ...
c:
cd program files\bat
"folder\edit.bat"
If I set local=%CD% in the bat file that is run, I will get 'C:\Program Files\bat' as my local variable. Is there a way around this to force it to take the actual location of the bat file so I can remove it?
-
Is there a way around this to force it to take the actual location of the bat file so I can remove it?
the filename + extension of a batch file is contained in the variable %0 (percent zero). The full name would be
%~dpnx0
so, adding quotes in case the path and/or name has spaces,
del "%~dpnx0"
would make a batch delete itself, but you would get an error message because you have deleted the running batch from within itself.
-
Ok, so %0 gives me the whole path, but what is the ~dpnx refer to? or is that like sending commands to the delete command?
-
Ok, I think I found something. I did Call /? in a command prompt and find %~dp1 - expands %1 to a drive letter and path only, but still confused on the nx
-
They are standard variable modifiers.
If you typed for /? at the command prompt, didn't you see this?
%~I - expands %I removing any surrounding quotes (")
%~fI - expands %I to a fully qualified path name
%~dI - expands %I to a drive letter only
%~pI - expands %I to a path only
%~nI - expands %I to a file name only
%~xI - expands %I to a file extension only
%~sI - expanded path contains short names only
%~aI - expands %I to file attributes of file
%~tI - expands %I to date/time of file
%~zI - expands %I to size of file
they can be combined to get compound results:
%~dpI - expands %I to a drive letter and path only
%~nxI - expands %I to a file name and extension only
%~fsI - expands %I to a full path name with short names only
so if %0 is a batch file's own name and extension, then
%~d0 is its drive letter with a colon
%~p0 is the full path to its folder
%~n0 is its bare name without extension
%~x0 is its extension with a dot before in front of it (usually .bat or .cmd)
Put them together adding quotes in case the path has spaces and you get:
"%~dpnx0"
-
Thank you for the explanation. :)