Computer Hope

Microsoft => Microsoft DOS => Topic started by: nothlit on April 30, 2008, 04:12:27 PM

Title: Set Var=?
Post by: nothlit on April 30, 2008, 04:12:27 PM
Code: [Select]
*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.
Title: Re: Set Var=?
Post by: nothlit on May 07, 2008, 12:38:15 PM
Anyone have an idea that would point me in the right direction? :)
Title: Re: Set Var=?
Post by: Dias de verano on May 07, 2008, 02:01:15 PM
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 ###.
Title: Re: Set Var=?
Post by: nothlit on May 08, 2008, 01:31:45 PM
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.
Title: Re: Set Var=?
Post by: Dias de verano on May 08, 2008, 01:42:31 PM
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.


Title: Re: Set Var=?
Post by: nothlit on May 12, 2008, 07:25:24 AM
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.
Title: Re: Set Var=?
Post by: Dias de verano on May 12, 2008, 10:39:43 AM
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
Title: Re: Set Var=?
Post by: nothlit on May 12, 2008, 01:06:24 PM
Correct
Title: Re: Set Var=?
Post by: Dias de verano on May 12, 2008, 01:44:19 PM
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

Title: Re: Set Var=?
Post by: nothlit on May 12, 2008, 02:18:39 PM
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.
Title: Re: Set Var=?
Post by: Dias de verano on May 12, 2008, 03:02:59 PM
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
Title: Re: Set Var=?
Post by: nothlit on May 12, 2008, 06:41:02 PM
Ok I double checked it is two lines
Title: Re: Set Var=?
Post by: Dias de verano on May 13, 2008, 12:07:23 AM
Ok I double checked it is two lines


How do they split?
 
Title: Re: Set Var=?
Post by: nothlit on May 13, 2008, 07:35:48 AM
Code: [Select]
*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
Title: Re: Set Var=?
Post by: Dias de verano on May 13, 2008, 12:04:54 PM
This is test.cfg

Quote
*NAME         RESET         WARN             MAX      INIT       EXPRESSION             INCREMENT
PRIMESEQ 0012300000 00###30000 00###90000   0  SEQUENCE+INCREMENT            5

This is test.bat

Quote
@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

Quote
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


Title: Re: Set Var=?
Post by: nothlit on May 14, 2008, 07:40:39 AM
Thank you thank you thank you.

Yes that will work perfectly. :) Thank you for all your help.
Title: Re: Set Var=?
Post by: nothlit on May 15, 2008, 09:55:11 AM
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 ...

Code: [Select]
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?
Title: Re: Set Var=?
Post by: Dias de verano on May 15, 2008, 10:03:01 AM
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.


Title: Re: Set Var=?
Post by: nothlit on May 15, 2008, 11:53:14 AM
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?
Title: Re: Set Var=?
Post by: nothlit on May 15, 2008, 12:09:37 PM
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
Title: Re: Set Var=?
Post by: Dias de verano on May 15, 2008, 12:35:32 PM
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"



Title: Re: Set Var=?
Post by: nothlit on May 15, 2008, 12:58:09 PM
Thank you for the explanation. :)