Page 1 of 1

Can't explain the error!

Posted: 10 May 2012 08:41
by tinfanide

Code: Select all

@ECHO OFF


FOR /L %%N IN (0,1,2) DO (

   FOR /F "tokens=*" %%A IN ('MORE +%%N 1.txt') DO SET "word1=%%A"&GOTO :next
   :next
   FOR /F "tokens=*" %%B IN ('MORE +%%N 2.txt') DO SET "word2=%%B"&GOTO :end
   :end
   IF %word1%==%word2% (
      ECHO yes
   ) ELSE (
      ECHO no
   )
)

PAUSE


I want to compare each line of two text files.
But why is the error?

Re: Can't explain the error!

Posted: 10 May 2012 08:46
by Fawers
Try enabling delayed expansion and enclose the variables in quotes or brackets.

Code: Select all

@ECHO OFF
setlocal enableDelayedExpansion

FOR /L %%N IN (0,1,2) DO (

   FOR /F "tokens=*" %%A IN ('MORE +%%N 1.txt') DO SET "word1=%%A"&GOTO :next
   :next
   FOR /F "tokens=*" %%B IN ('MORE +%%N 2.txt') DO SET "word2=%%B"&GOTO :end
   :end
   IF "!word1!"=="!word2!" (
      ECHO yes
   ) ELSE (
      ECHO no
   )
)

PAUSE

Re: Can't explain the error!

Posted: 10 May 2012 09:18
by tinfanide
Fawers wrote:Try enabling delayed expansion and enclose the variables in quotes or brackets.

Code: Select all

@ECHO OFF
setlocal enableDelayedExpansion

FOR /L %%N IN (0,1,2) DO (

   FOR /F "tokens=*" %%A IN ('MORE +%%N 1.txt') DO SET "word1=%%A"&GOTO :next
   :next
   FOR /F "tokens=*" %%B IN ('MORE +%%N 2.txt') DO SET "word2=%%B"&GOTO :end
   :end
   IF "!word1!"=="!word2!" (
      ECHO yes
   ) ELSE (
      ECHO no
   )
)

PAUSE

Yes, but it returns an error
"Cannot access file C:\test\+%N"

Re: Can't explain the error!

Posted: 10 May 2012 09:36
by Fawers
In this case, try those:

Code: Select all

FOR /F "tokens=*" %%A IN ('type 1.txt^|MORE +%%N') DO SET "word1=%%A"&GOTO :next

Code: Select all

FOR /F "tokens=*" %%B IN ('type 2.txt^|MORE +%%N') DO SET "word2=%%B"&GOTO :end

Re: Can't explain the error!

Posted: 10 May 2012 10:58
by foxidrive
This does the job. I'm not sure if you have long text files...

Code: Select all

@ECHO OFF
setlocal enableDelayedExpansion

FOR /L %%N IN (0,1,2) DO (
set skip=%%N
set "word1="
set "word2="

   FOR /F "tokens=*" %%A IN ('MORE +!skip! 1.txt') DO if not defined word1 SET "word1=%%A"
   FOR /F "tokens=*" %%B IN ('MORE +!skip! 2.txt') DO if not defined word2 SET "word2=%%B"

   IF "!word1!"=="!word2!" (ECHO yes) ELSE (ECHO no)

)

PAUSE

Re: Can't explain the error!

Posted: 10 May 2012 23:51
by tinfanide

Code: Select all

@ECHO ON
setlocal enableDelayedExpansion

FOR /L %%N IN (0,1,2) DO (
set skip=%%N
set "word1="
REM set "word2="

   FOR /F "tokens=*" %%A IN ('MORE +!skip! 1.txt') DO IF NOT DEFINED word1 SET "word1=%%A"
ECHO !word1!
   FOR /F "tokens=*" %%B IN ('MORE +!skip! 2.txt') DO SET "word2=%%B"
ECHO !word2!
   REM IF "!word1!"=="!word2!" (ECHO yes) ELSE (ECHO no)

)

PAUSE


Comparing !word1! with !word2!,
I am wondering why

Code: Select all

SET "word1="
IF NOT DEFINED word1 SET "word1=%%A"

Only SET the variable = the first line (after skipping the precedent line)?
I omit

Code: Select all

SET "word2="
IF NOT DEFINED word 2 SET "word2=%%B"

and it takes the last line.

Re: Can't explain the error!

Posted: 11 May 2012 12:06
by Fawers
tinfanide wrote:Comparing !word1! with !word2!,
I am wondering why

Code: Select all

SET "word1="
IF NOT DEFINED word1 SET "word1=%%A"

Only SET the variable = the first line (after skipping the precedent line)?
I omit

Code: Select all

SET "word2="
IF NOT DEFINED word 2 SET "word2=%%B"

and it takes the last line.


set "word1=%%A" will only work if word1 is not defined. As soon as it gets defined, the set part of the command will be ignored in the following loops.
As you did in the "word2" part, if you omit IF NOT DEFINED word1, word1 will be set to the last word.

Re: Can't explain the error!

Posted: 11 May 2012 12:27
by Aacini
Although your method solve the problem, it is very inefficient because it execute MORE command with every line of each file! This method is faster:

Code: Select all

@echo on
setlocal EnableDelayedExpansion
< 1.txt (
   for /F "delims=" %%a in (2.txt) do (
      set word1=
      set /P word1=
      set "word2=%%a"
      if "!word1!" == "!word2!" (
         echo yes
      ) else (
         echo no
      )
   )
)

Re: Can't explain the error!

Posted: 12 May 2012 08:26
by tinfanide
Fawers wrote:
tinfanide wrote:Comparing !word1! with !word2!,
I am wondering why

Code: Select all

SET "word1="
IF NOT DEFINED word1 SET "word1=%%A"

Only SET the variable = the first line (after skipping the precedent line)?
I omit

Code: Select all

SET "word2="
IF NOT DEFINED word 2 SET "word2=%%B"

and it takes the last line.


set "word1=%%A" will only work if word1 is not defined. As soon as it gets defined, the set part of the command will be ignored in the following loops.
As you did in the "word2" part, if you omit IF NOT DEFINED word1, word1 will be set to the last word.


Wonderful. It was just far more than I could have understood.