What's goot wrong in this script?

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
mor.bas
Posts: 66
Joined: 25 Apr 2012 04:28

What's goot wrong in this script?

#1 Post by mor.bas » 15 Aug 2012 09:31

I have a script and I'm not understanding why it's not working.\
I check if file exist
If yes I check for a string in this file if it not there i want to add some lines to this file.



set good_file=e:\FileName.txt
IF NOT EXIST "%good_file%" (
echo filename. missing.
) ELSE (
findstr "computerhope" %good_file% > null
if %errorlevel%==0 (
echo no need to add
) ELSE (
(
ECHO abcd
ECHO erty
ECHO Line 3
) >> %good_file%
)

Squashman
Expert
Posts: 4488
Joined: 23 Dec 2011 13:59

Re: What's goot wrong in this script?

#2 Post by Squashman » 15 Aug 2012 09:59

Looks like unbalanced parenthesis.

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

Re: What's goot wrong in this script?

#3 Post by abc0502 » 15 Aug 2012 10:02

Squashman right you are missing one last ")"
but even if you put it when the word not exist in the file it refuse to write the lines to the file :?:
I think it has something related to the errorlevel some how. :roll:

mor.bas
Posts: 66
Joined: 25 Apr 2012 04:28

Re: What's goot wrong in this script?

#4 Post by mor.bas » 15 Aug 2012 10:25

I add the ) in the end and it always print "not need to add"
What I need to change?

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

Re: What's goot wrong in this script?

#5 Post by abc0502 » 15 Aug 2012 10:26

Ok i made this one and it works, but i don't really understand what is wrong with your code except the missing ")" at the end

Code: Select all

@echo off
set "good_file=D:\1.txt"

IF not exist %good_file% ( Echo File Missing & Goto eof
) Else ( Goto ok )

:ok
Findstr "computerhope" "%good_file%" >nul
IF %Errorlevel% == 0 ( Echo No need to add & Goto eof
) Else ( Goto add )

:add
(
ECHO abcd
ECHO erty
ECHO Line 3
) >> %good_file%
pause
Exit /B

:eof
pause
Exit /B

mor.bas
Posts: 66
Joined: 25 Apr 2012 04:28

Re: What's goot wrong in this script?

#6 Post by mor.bas » 15 Aug 2012 11:32

Any explanation what's wrong in my script?

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

Re: What's goot wrong in this script?

#7 Post by abc0502 » 15 Aug 2012 11:48

I don't really understand what is wrong but i think it could be related to the nested if statment or the error level
I really don't know

Liviu
Expert
Posts: 470
Joined: 13 Jan 2012 21:24

Re: What's goot wrong in this script?

#8 Post by Liviu » 15 Aug 2012 11:55

mor.bas wrote:

Code: Select all

if %errorlevel%==0 (
Replace the above with

Code: Select all

if not errorlevel 1 (

The way you had it, %errorlevel% was evaluated at the time the top "else" block was parsed, before findstr executed, and it was always 0 at that point.

Liviu

mor.bas
Posts: 66
Joined: 25 Apr 2012 04:28

Re: What's goot wrong in this script?

#9 Post by mor.bas » 15 Aug 2012 12:10

Why it's working like that,it's not going line by line?

Ed Dyreen
Expert
Posts: 1569
Joined: 16 May 2011 08:21
Location: Flanders(Belgium)
Contact:

Re: What's goot wrong in this script?

#10 Post by Ed Dyreen » 15 Aug 2012 12:17

mor.bas wrote:

Code: Select all

set good_file=e:\FileName.txt
IF NOT EXIST  "%good_file%" (
    echo filename. missing.
    ) ELSE (
      findstr  "computerhope" %good_file% > null   
     if %errorlevel%==0 (
       echo no need to add
    ) ELSE (
    (
     ECHO abcd
      ECHO erty
      ECHO Line 3
      ) >> %good_file%
     )

Why check errorLevel and why the redirection to FILE 'null' ?

Code: Select all

set "good_file=e:\FileName.txt"

IF NOT EXIST "%good_file%" (

       echo filename. missing.

) ELSE findstr /c:"computerhope" "%good_file%" 2>&1 >nul &&(

       echo no need to add
) ||(
       ECHO abcd
       ECHO erty
       ECHO Line 3

)>>"%good_file%"


ED

Liviu
Expert
Posts: 470
Joined: 13 Jan 2012 21:24

Re: What's goot wrong in this script?

#11 Post by Liviu » 15 Aug 2012 16:31

mor.bas wrote:Why it's working like that,it's not going line by line?

In a way yes, but the "lines" in the text file are not the same as the (logical) "lines" the interpreter considers. In particular, code inside parentheses is parsed in one pass.

For example, the following

Code: Select all

set "x=0"
echo before parentheses: %x%
(
  set "x=1"
  echo inside parentheses: %x%
)
echo after parentheses:  %x%
outputs

Code: Select all

before parentheses: 0
inside parentheses: 0
after parentheses:  1

Liviu

Ed Dyreen
Expert
Posts: 1569
Joined: 16 May 2011 08:21
Location: Flanders(Belgium)
Contact:

Re: What's goot wrong in this script?

#12 Post by Ed Dyreen » 15 Aug 2012 19:01

mor.bas wrote:Why it's working like that,it's not going line by line?
Warning
  • converts LF ( Line Feed ) to CRLF ( Carriage Return + Line Feed ).
  • matches are faster but case insensitive.

Code: Select all

@echo off &setlocal disableDelayedExpansion

set "$iFile=e:\FileName.txt"
set "$oFile=e:\FileName.txt"

cls
echo.¯ '%$iFile%'
type "%$iFile%" &echo.
echo.® '%$iFile%'

more "%$iFile%" > "tmp.TXT"

for /f %%? in ( '2^>nul ^( ^< "%$iFile%" find.EXE /c /v "" ^)' ) do set "$end=%%?"

< "tmp.TXT" (

       for /l %%! in (

              1, 1, %$end%

       ) do   set "$=" &set /p "$=" &(

              setlocal enabledelayedexpansion
              rem (
                     (echo(!$!)
                     if "!$!" neq "!$:computerhope=!" (

                            (echo(abcd)
                            (echo(erty)
                            (echo(Line 3)
                     )
              rem )
              endlocal
       )

) > "%$oFile%"

echo.
echo.¯ '%$oFile%'
type "%$oFile%" &echo.
echo.® '%$oFile%'

pause
exit
ps: your thread title is very intuitive, did it take you long to come up with that ? :D


ED

Post Reply