Desired number of spaces - optimizing the code?

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Message
Author
Aacini
Expert
Posts: 1914
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: Desired number of spaces - optimizing the code?

#16 Post by Aacini » 23 Dec 2014 11:43

@miskox,

You must realize that you don't need to store the offset in each record, because if you add /O switch to the first FINDSTR the result will include it...

Antonio

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: Desired number of spaces - optimizing the code?

#17 Post by Squashman » 23 Dec 2014 11:50

Aacini wrote:@miskox,

You must realize that you don't need to store the offset in each record, because if you add /O switch to the first FINDSTR the result will include it...

Antonio

+1

penpen
Expert
Posts: 2009
Joined: 23 Jun 2013 06:15
Location: Germany

Re: Desired number of spaces - optimizing the code?

#18 Post by penpen » 23 Dec 2014 12:25

If you still want to optimize speed, then you could use one of the following examples.

This creates 150 spaces:

Code: Select all

@echo off
setlocal enableDelayedExpansion enableExtensions
set "space=     "
set "space=!space: =%space: =      %!"
endlocal & set "space=%space%"
echo("%space%"
goto :eof

That creates 1024 spaces:

Code: Select all

@echo off
setlocal enableDelayedExpansion enableExtensions
set "space=  "
set "space=!space: =%space: =    %!"
set "space=!space: =%space: =    %!"
endlocal & set "space=%space%"
echo("%space%"
goto :eof


Gimmick: And this code aborts the actual cmd instance and returns -1 under my windows xp home:

Code: Select all

@echo off
setlocal enableDelayedExpansion enableExtensions
set "space=    "
set "space=!space: =%space: =    %!"
set "space=!space: =%space: =    %!"
endlocal & set "space=%space%"
echo("%space%"
goto :eof

penpen

miskox
Posts: 630
Joined: 28 Jun 2010 03:46

Re: Desired number of spaces - optimizing the code?

#19 Post by miskox » 23 Dec 2014 12:37

Aacini wrote:@miskox,

You must realize that you don't need to store the offset in each record, because if you add /O switch to the first FINDSTR the result will include it...

Antonio


Very good idea, thanks. I have everything done already so I will think about it - I might use it.

The only problem I see is this: if I manually edit the file then record lengths are not correct so I must reindex the file. Also when I do the reinder I skip 'empty' records.

Saso

Aacini
Expert
Posts: 1914
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: Desired number of spaces - optimizing the code?

#20 Post by Aacini » 23 Dec 2014 17:51

If you use the :StrLen subroutine to get the length of the edited line (in order to check if the new contents fits in the original line), then you may also use it to get the length of the original line. This way, it is not necessary to store the record length in the file neither...

Antonio

PS - You may generate a variable with 256 spaces without delayed expansion this way:

Code: Select all

set "space= "
for /L %%i in (1,1,8) do call set "space=%%space%%%%space%%"

... and you may write a "recLen" number of spaces without delayed expansion this way:

Code: Select all

call echo %%space:~0,%recLen%%%

miskox
Posts: 630
Joined: 28 Jun 2010 03:46

Re: Desired number of spaces - optimizing the code?

#21 Post by miskox » 24 Dec 2014 03:03

Aacini wrote:If you use the :StrLen subroutine to get the length of the edited line (in order to check if the new contents fits in the original line), then you may also use it to get the length of the original line. This way, it is not necessary to store the record length in the file neither...

Antonio


Yes! I was thinking about this and came to the same conclusion: FINDSTR /O is very good:

Pros:
- no need to store record offset and record length information at all (record offset gets display with /O, I will get record length with a temporary file)
- if I change/update/modify the file with NOTEPAD I don't need to do anything - everything stays the same - very good. No fear of having bad indexes (record offset and record length sizes).

Cons:
- I have to reprogram everything. But I will do this. This is much better solution. Thank Aacini!

Aacini wrote:
PS - You may generate a variable with 256 spaces without delayed expansion this way:

Code: Select all

set "space= "
for /L %%i in (1,1,8) do call set "space=%%space%%%%space%%"




This works, too. Great. Thanks again!

Saso

Post Reply