What's goot wrong in this script?
Moderator: DosItHelp
What's goot wrong in this script?
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%
)
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%
)
Re: What's goot wrong in this script?
Looks like unbalanced parenthesis.
Re: What's goot wrong in this script?
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.
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.

Re: What's goot wrong in this script?
I add the ) in the end and it always print "not need to add"
What I need to change?
What I need to change?
Re: What's goot wrong in this script?
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
Re: What's goot wrong in this script?
Any explanation what's wrong in my script?
Re: What's goot wrong in this script?
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
I really don't know
Re: What's goot wrong in this script?
Replace the above withmor.bas wrote:Code: Select all
if %errorlevel%==0 (
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
Re: What's goot wrong in this script?
Why it's working like that,it's not going line by line?
Re: What's goot wrong in this script?
Why check errorLevel and why the redirection to FILE 'null' ?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%
)
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
Re: What's goot wrong in this script?
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%
Code: Select all
before parentheses: 0
inside parentheses: 0
after parentheses: 1
Liviu
Re: What's goot wrong in this script?
Warningmor.bas wrote:Why it's working like that,it's not going line by line?
- 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

ED