Problem with EnableDelayedExpansion

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Ranguna173
Posts: 104
Joined: 28 Jul 2011 17:32

Problem with EnableDelayedExpansion

#1 Post by Ranguna173 » 20 Apr 2013 05:40

Hello everyone :D

I've been coding this script but I can't seem to get it to work.

What the code does:
    It counts the number of lines (%num%) of dfg.txt.
    Saves each line to a variable (%line(n)%(n being the number of the line))
    It finds the lines in database.enc and if they don't exist the script adds them like %line(n)%=%ran1%%ran2% to database.enc(%ran1% and %ran2% are generated by %random%, they are always unique because the batch also
searches the database.enc for %ran1% and %ran2% and if they are found it generates new ones until they are unique).
    After all that it writes a new file (fileenc.txt) with the previously generated %ran1%%ran2% variables acording to each line from dfg.txt.

And as I said for some reason it isn't working.

Here's the code:

Code: Select all

@echo on &setlocal enableDelayedExpansion
set tempvar=temp

findstr /R /N "^" dfg.txt | find /C ":">totalyrandomwordthatnonewilleveruserandimeaneveryihopeyouhaventusedthis
< totalyrandomwordthatnonewilleveruserandimeaneveryihopeyouhaventusedthis set /p "num="
del totalyrandomwordthatnonewilleveruserandimeaneveryihopeyouhaventusedthis

for /f "tokens=2 delims=:" %%i in ('find /c /v "" dfg.txt') do (
  for /l %%j in (1 1 %%i) do (
    set /p "line%%j="
  )
)<dfg.txt

set lines=1
:bb
if %tempvar%==pmet (
    set /a lines=%lines%+1
)
set "tempvar=temp"
FINDSTR /C:"'!line%lines%!'" database.enc > nul
if errorlevel 1 (
    set ran=%random%
    set ran2=%random%
    FINDSTR /C:"%ran%%ran2%" database.enc > nul
    if errorlevel 1 (
       echo '%line!lines!%'=%ran%%ran2%>>database.enc
       echo %ran%%ran2%>>fileenc.txt
       if %lines% gtr %num% (
          set "tempvar=pmet"
          goto bb
         )
       set action3=%action2%
       set action2=%action1%
       set "action1=Saved as fileenc.txt"
       pause
      ) else (
       goto bb
      )
   ) else (
    for /f "tokens=2 delims==" %%a in ('FINDSTR /C:"'%line!lines!%'" database.enc') do (
       echo %%a>>fileenc.txt
       if %lines% gtr %num% (
          set "tempvar=pmet"
          goto bb
         )
       set action3=%action2%
       set action2=%action1%
       set "action1=Saved as fileenc.txt"
      )
    pause
   )
pause


Contents of dfg.txt:

Code: Select all

hello
test
test2

empty line4


The script isn't writing the apropriated lines, %line!lines!% should be %line(number)% which should be the content of a line but instead it's nothing, an empty variable.

Why is it an empty variable and what can I do to correct this ?

Thanks for reading and please help :)

abc0502
Posts: 1007
Joined: 26 Oct 2011 22:38
Location: Egypt

Re: Problem with EnableDelayedExpansions

#2 Post by abc0502 » 20 Apr 2013 06:05

you have a missing argument after /c in the first for command, it should be something like this /c:"word"

Ranguna173
Posts: 104
Joined: 28 Jul 2011 17:32

Re: Problem with EnableDelayedExpansions

#3 Post by Ranguna173 » 20 Apr 2013 06:11

Yeah I know, it's wrong because the "word" should be the contents of %line!lines!%, but for some reason it's empty.

abc0502
Posts: 1007
Joined: 26 Oct 2011 22:38
Location: Egypt

Re: Problem with EnableDelayedExpansions

#4 Post by abc0502 » 20 Apr 2013 06:13

you lost me ?
where exactly the output should be ? in the output on the cmd window or in the files ?

Ranguna173
Posts: 104
Joined: 28 Jul 2011 17:32

Re: Problem with EnableDelayedExpansions

#5 Post by Ranguna173 » 20 Apr 2013 06:26

The output would be inside fileenc.txt

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Problem with EnableDelayedExpansions

#6 Post by foxidrive » 20 Apr 2013 06:30

On a casual inspection is this %line!lines!% better replaced with this !line%lines%!

abc0502
Posts: 1007
Joined: 26 Oct 2011 22:38
Location: Egypt

Re: Problem with EnableDelayedExpansion

#7 Post by abc0502 » 20 Apr 2013 06:35

I agree with Foxidrive, but even then the variable will hold nothing? it's empty.
if it is supposed to hold the line content then all the code should be in the first for command, or in a function that is being called from inside that for command.

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

Re: Problem with EnableDelayedExpansion

#8 Post by Aacini » 20 Apr 2013 06:36

  • When FIND command process an immediate file this way: find /c /v "" newDocument.txt, the result include a series of dashes and the name of the file: "---------- NEWDOCUMENT.TXT: 7". In order to get just the number, you need to redirect the file instead: find /c /v "" < newDocument.txt
  • Your ran and ran2 variables have the same value. You must use Delayed Expansion in order for they to be different:

    Code: Select all

        set ran=!random!
        set ran2=!random!
  • You must use Delayed Expansion in all variables inside parentheses that changed its value inside parentheses; otherwise the expanded value is the same the variable had before enter the parentheses.
  • This line is wrong:

    Code: Select all

           echo '%line!lines!%'=%ran%%ran2%>>database.enc
    If your purpose is to show the value of variable line!lines!, then you must use this form instead:

    Code: Select all

           echo '!line%lines%!'=%ran%%ran2%>>database.enc
    but only if lines is NOT modified inside the parentheses! Otherwise, you must use a different form.

I suggest you to use the standard array notation on these variables: line[!lines!]; this form is clearer and may help you to avoid errors. For further details, see this post.

Antonio

Ranguna173
Posts: 104
Joined: 28 Jul 2011 17:32

Re: Problem with EnableDelayedExpansion

#9 Post by Ranguna173 » 20 Apr 2013 07:00

I dunno what's going on but I can't write a single code right now :shock:

I'll just do a complete re-write in another time.

Thanks for all the help :)

Post Reply