Variable problem

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
darioit
Posts: 230
Joined: 02 Aug 2010 05:25

Variable problem

#1 Post by darioit » 19 Aug 2010 06:47

Hello everybody,
I have this problem, I want to load a file in array and after display the resul of both variable, this is a code

Code: Select all

@echo off&setlocal

:: (list.txt = 1234;abcd)
for /f "usebackq tokens=1,2 delims=;" %%a in ("%~dp0list.txt") do set cod%%a=%%b

echo %cod1234% (result correct "abcd")

set a=1234
echo %cod%%a% (result wrong)
echo cod%a% (result wrong)
echo %cod%a%% (result wrong)
echo %cod%%a%% (result wrong)


But something is wrong, do you have any suggestion?

Thanks in advance
Dario

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Variable problem

#2 Post by aGerman » 19 Aug 2010 06:55

Try
call echo %%cod%a%%%
[EDIT: cod was part of the name, not a variable /]

Regards
aGerman

orange_batch
Expert
Posts: 442
Joined: 01 Aug 2010 17:13
Location: Canadian Pacific
Contact:

Re: Variable problem

#3 Post by orange_batch » 19 Aug 2010 17:15

Hi, not sure why you're doing it this way, but here's some information and examples.

The code you wrote:

Code: Select all

@echo off&setlocal

:: (list.txt = 1234;abcd)
for /f "usebackq tokens=1,2 delims=;" %%a in ("%~dp0list.txt") do set cod%%a=%%b

echo %cod1234% (result correct "abcd")

set a=1234
DOS reads from % to %, as highlighted in the following.
Black=text. Green=escaped text. Blue=first expansion. Red=second expansion.
echo:%cod%%a% (result wrong)
echo:cod%a% (result wrong)
echo:%cod%a%% (result wrong)
echo:%cod%%a%% (result wrong)


If you didn't know, enabledelayedexpansion allows you to use !var! to expand variables at the time of execution, unlike %var% which expands as soon as the interpreter sees it. %var% is expanded when a FOR loop initializes, before it processes, which is why it becomes static and doesn't change. So, aside from not being static, !var! also allows us to combine to process dynamic variables, !var%dynamic%!. However to process a changing dynamic variable in a FOR loop, use CALL %%var!dynamic!%% (%% escapes to text %). This will expand !dynamic! to it's "value", so the interpreter sees %varvalue% at execution. This bypasses %var%'s early expansion problem, which makes it effectively the same as !varvalue! would be. Anyhow, with organized syntax, we'll do a basic !var%dynamic%!.

Code: Select all

@echo off&setlocal enabledelayedexpansion

:: (list.txt = 1234;abcd)
for /f "usebackq delims=; tokens=1,2" %%a in ("%~dp0list.txt") do set "cod%%a=%%b"

echo:%cod1234% (result correct "abcd")

set "a=1234"

echo:!cod%a%! (result correct "abcd")
pause


You said you wanted to load your file into an array? A DOS array is accomplished with the following.
This will catalog all of each line, including blank lines. Following this, I'll adapt it to your code.

Code: Select all

@echo off&setlocal enabledelayedexpansion

for /f "delims=] tokens=1*" %%x in ('type mytext.txt ^| find /v /n ""') do (
set /a linecount+=1
set "line!linecount!=%%y"
)

:: Display array.
set line

:: To process array...
for /l %%x in (1,1,%linecount%) do (
:: Your code here. The following outputs the array to newtext.txt:
echo:!line%%x!>>newtext.txt
)


Catalog your list.txt into a DOS array. Does not include blank lines, though it could.

Code: Select all

@echo off&setlocal enabledelayedexpansion

for /f "usebackq delims=; tokens=1,2" %%x in ("%~dp0list.txt") do (
set "line%%x=%%y"
)

:: Display array.
set line

:: To process array... use 'set line'. Variables are organized numerically then alphabetically.
for /f "delims== tokens=1*" %%x in ('set line') do (
echo:Variable name: %%x
echo:Has value of: %%y
)

darioit
Posts: 230
Joined: 02 Aug 2010 05:25

Re: Variable problem

#4 Post by darioit » 20 Aug 2010 08:13

Thanks for your response but this not solve my problem

I have resolved the problem in this way, please take a look ad let me know if I wrote a good code, regards, Dario

Code: Select all

@ECHO On
setlocal enabledelayedexpansion

:: setta variabili data e ora
SET G=%DATE:~0,2%
SET M=%DATE:~3,2%
SET Y=%DATE:~6,4%
FOR /F "tokens=1 delims=."  %%G IN ('TIME /T') DO SET HH=%%G
:: SET HH=%TIME:~0,2%
SET MM=%TIME:~3,2%
SET SS=%TIME:~6,2%
SET DD=%Y%%M%%G%
SET DD0=%Y%-%M%-%G%
SET DD1=%G%/%M%/%Y%
SET ORA=%HH%%MM%

SET ORIGINE=D:\ROOT
SET LOGS=%ORIGINE%\LOGS
SET SCRIPTS=%ORIGINE%\scripts
SET XP=%ORIGINE%\XP

FOR /F "usebackq tokens=1,2 delims=;" %%a in ("%~dp0lista1.txt") do set codice%%a=%%b
FOR /F "usebackq tokens=1,2 delims=;" %%a in ("%~dp0lista2.txt") do set classe%%a=%%b

IF EXIST %XP%\RCV\YI.* FOR %%i in (%XP%\RCV\YI.*) DO CALL :ELABORA %%i
GOTO :EOF

:ELABORA
SET FLAG=N
CALL :verifica_lastrec %1
IF "%FLAG%" EQU "Y" GOTO:EOF
MD %1_WORK
SET "DIR_WORK=%1_WORK"
FOR /F "delims=" %%a in (%1) do set "name=%%a" &call :procName

wzzip %XP%\RCV\Backup\BACKUP_%newCode2%_%DD%_%ORA%.ZIP %DIR_WORK%\IT* > nul
wzzip %XP%\RCV\Backup\BACKUP_%newCode2%_%DD%_%ORA%.ZIP %1 > nul

FOR /F "delims=" %%i in ('DIR /a-d /b %DIR_WORK%\IT*') DO IF "%%~xi" NEQ ".NOT_FOUND" MOVE /Y %DIR_WORK%\%%i D:\ROOT\XP\Project\ > nul
IF EXIST %DIR_WORK%\*.NOT_FOUND DEL /Q %DIR_WORK%\*.NOT_FOUND > nul
RD /Q %DIR_WORK%
DEL /Q %1
GOTO:EOF

:procName
IF "%name:~2,1%"=="0" (
    SET newCode1=!codice%name:~4,4%!
   SET newCode2=!classe%name:~0,2%!
    GOTO fine_ricerca
)

:fine_ricerca
SET "name=%name:&=^&%"
>> %DIR_WORK%\"filename_fix.%newCode1%.%newCode2%" ECHO %name%
GOTO:EOF

:verifica_lastrec
FOR /F %%d IN ('type "%1"^|find "" /v /c') DO SET /a cnt_rec=%%d
SET /A lastrec=%cnt_rec%-1
FOR /F "tokens=* skip=%lastrec%" %%i IN (%1) DO SET linea1=%%i
CALL SET lastrec=%%linea1:~2,1%%%
IF "%lastrec%" NEQ "9" MOVE /Y %1 D:\ROOT\XP\RCV\Errati &SET FLAG=Y
GOTO:EOF

Post Reply