user3541265
user3541265

Reputation: 1

I'm trying to extract a string of data from a text file that has spaces.

Here is my Code:

FOR /F "delims=" %%1 IN (COAFN1T_N1E827682.BST) DO (
SET TempRecord=%%1
echo %TempRecord%
SET Strat=%TempRecord:~254,8%
SET VCNum=%TempRecord:~263,4%
goto EndWHile
)

:EndWhile

This is what it looks like when i run it. You can see when i echo TempRecord it is blank.

Z:\CLIENTS\COAF\NewUsed\BIN\BAT\test>(
SET TempRecord=201404   xxxxxxxx               xxxxxx               xxxx xxxxxxx
Rd.                                              xxxxx                TX xxxxx  
 xxx_x_V7   xxx_V2     N1E_V1     NUB_C_V8              2.39
              05/24/2014 4102014  VC01 03/31/2014
 echo
 SET Strat=~254,8
 SET VCNum=~263,4
 goto EndWHile
)

ECHO is on.

Upvotes: 0

Views: 28

Answers (1)

Magoo
Magoo

Reputation: 80023

Within a block statement (a parenthesised series of statements), the entire block is parsed and then executed. Any %var% within the block will be replaced by that variable's value at the time the block is parsed - before the block is executed - the same thing applies to a FOR ... DO (block).

Hence, IF (something) else (somethingelse) will be executed using the values of %variables% at the time the IF is encountered.

Two common ways to overcome this are 1) to use setlocal enabledelayedexpansion and use !var! in place of %var% to access the changed value of var or 2) to call a subroutine to perform further processing using the changed values.

%n, where n is 0 to 9 refers to the argument number supplied to the procedure (eg. from the command-line) You must use a..z or A..Z for loop-control variables (there are a few other characters too, but only letters are documented) - and this is CaSe-SeNsItIvE,

so:

SETLOCAL ENABLEDELAYEDEXPANSION
FOR /F "delims=" %%a IN (COAFN1T_N1E827682.BST) DO (
SET TempRecord=%%a
echo !TempRecord!
SET Strat=!TempRecord:~254,8!
SET VCNum=!TempRecord:~263,4!
goto EndWHile
)

:EndWhile

should work for you - I haven't tested it.

Upvotes: 1

Related Questions