Can't explain the error!

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
tinfanide
Posts: 117
Joined: 05 Sep 2011 09:15

Can't explain the error!

#1 Post by tinfanide » 10 May 2012 08:41

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?

Fawers
Posts: 187
Joined: 08 Apr 2012 17:11
Contact:

Re: Can't explain the error!

#2 Post by Fawers » 10 May 2012 08:46

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

tinfanide
Posts: 117
Joined: 05 Sep 2011 09:15

Re: Can't explain the error!

#3 Post by tinfanide » 10 May 2012 09:18

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"

Fawers
Posts: 187
Joined: 08 Apr 2012 17:11
Contact:

Re: Can't explain the error!

#4 Post by Fawers » 10 May 2012 09:36

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

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

Re: Can't explain the error!

#5 Post by foxidrive » 10 May 2012 10:58

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

tinfanide
Posts: 117
Joined: 05 Sep 2011 09:15

Re: Can't explain the error!

#6 Post by tinfanide » 10 May 2012 23:51

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.

Fawers
Posts: 187
Joined: 08 Apr 2012 17:11
Contact:

Re: Can't explain the error!

#7 Post by Fawers » 11 May 2012 12:06

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.

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

Re: Can't explain the error!

#8 Post by Aacini » 11 May 2012 12:27

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
      )
   )
)

tinfanide
Posts: 117
Joined: 05 Sep 2011 09:15

Re: Can't explain the error!

#9 Post by tinfanide » 12 May 2012 08:26

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.

Post Reply