0

I'm fairly certain this has already been asked here, but I have not been able to find a thread that can actually help me.

What I want to do is have a batch file that will look for a text file, and if it is found, verify that the contents are correct. My current code is as follows:

:SEARCHFILE
if exist "C:\FOLDER\TEXT.TXT" (
    goto :SEARCHCONTENTS
) else (
    goto :SEARCHFAIL1
)

:SEARCHCONTENTS
COLOR A0
ECHO FILE FOUND. VERIFYING FILE.
set /p INPUT=< "C:\FOLDER\TEXT.TXT"
IF %INPUT%==LONGSTRINGOFTEXT (
    GOTO :SEARCHSUCCESS
) ELSE (
    GOTO :SEARCHFAIL2
)

:SEARCHFAIL1
COLOR 40
ECHO FILE NOT FOUND.
ECHO FILE WILL NOW BE CREATED.
GOTO :CREATEFILE


:SEARCHFAIL2
COLOR 40
ECHO FILE NOT VALID.
ECHO FILE WILL NOW BE REPAIRED.
GOTO :CREATEFILE

:CREATEFILE
ECHO LONGSTRINGOFTEXT > "C:\FOLDER\TEXT.TXT"

:SEARCHSUCCESS
COLOR A0
ECHO FILE SUCCESSFULLY VALIDATED.
PAUSE

Originally, the text file was going to have 25 lines, and the batch would verify each one like this:

set /p INPUT1=< "C:\FOLDER\TEXT.TXT"
set /p INPUT2=<< "C:\FOLDER\TEXT.TXT"
set /p INPUT3=<< "C:\FOLDER\TEXT.TXT"
..
set /p INPUT25=< "C:\FOLDER\TEXT.TXT"
    IF NOT %INPUT1%==LONGSTRINGOFTEXT1 GOTO :SEARCHFAIL2
    IF NOT %INPUT2%==LONGSTRINGOFTEXT2 GOTO :SEARCHFAIL2
    IF NOT %INPUT3%==LONGSTRINGOFTEXT3 GOTO :SEARCHFAIL2
    ..
    IF NOT %INPUT25%==LONGSTRINGOFTEXT25 GOTO :SEARCHFAIL2

I would still like to do it this way if possible, but I still have no clue what the syntax error is in the line that inputs the "LONGSTRINGOFTEXT" into the batch.

Any advice?

  • I don't get it. Why go to all this trouble to avoid overwriting the file if it's already correct? Why not just assume it's invalid and recreate it regardless? The end result is the same. Before script execution, file is in an unknown state. After script execution, file is known good. – rojo Mar 08 '15 at 14:43
  • Well, I did adjust the code a bit to post it here. The real script doesn't actually repair the file, and this is by design. If the file is correct, you get result A, if not, you get result B. – Fragdog Mar 08 '15 at 14:48

2 Answers2

0

That seems like an odd thing to do, but you don't have to change much to get your code to work.

There is no << redirection to read the next line. You need to redirect once for many inputs. Also, you should probably quote your text to get your IF statements to work properly if there are spaces or poison characters.

< "C:\FOLDER\TEXT.TXT" (
  set /p INPUT1=
  set /p INPUT2=
  set /p INPUT3=
  ...
  set /p INPUT25=
)
IF NOT "%INPUT1%"=="LONGSTRINGOFTEXT1" GOTO :SEARCHFAIL2
IF NOT "%INPUT2%"=="LONGSTRINGOFTEXT2" GOTO :SEARCHFAIL2
IF NOT "%INPUT3%"=="LONGSTRINGOFTEXT3" GOTO :SEARCHFAIL2
...
IF NOT "%INPUT25%"=="LONGSTRINGOFTEXT25" GOTO :SEARCHFAIL2

You really should explicitly undefine the variable before you read the line, since SET /P will not clear any existing value if the input is empty. And it is safer to use delayed expansion. You can do all the inputs with a simple loop. And you can embed your target file within your script and read the desired values from there. You can put the test logic in a subroutine and return an error if the test failed. The following works as long as none of your target lines begin with :.

@echo off
:: Most of your code goes here

call :testFile || goto :SEARCHFAIL2


:: Make sure there is an EXIT /B at the end to prevent the code
:: from falling into the subroutine
exit /b

:testFile
setlocal disableDelayedEpansion
:::LONG STRING OF TEXT 1
:::LONG STRING OF TEXT 1
:::LONG STRING OF TEXT 1
:::etc. ...
:::LONG STRING OF TEXT 25
< "C:\FOLDER\TEXT.TXT" (
  for /f "tokens=* delims=:" %%A in ('findstr "^:::" "%~f0"') do (
    set "test=%%A"
    set "input="
    set /p "input="
    setlocal enableDelayedExpansion
    if "!input!" neq "!test!" exit /b 1
    endlocal
  )
)
exit /b 0

The advantage of the above is you can easily adapt it for any size file by simply adding more lines to test.

dbenham
  • 127,446
  • 28
  • 251
  • 390
  • Okay, I've tried that out, but this is what I'm getting in the CMD window: File found. Verifying file. FINDSTR: Cannot open f0 File not found. – Fragdog Mar 08 '15 at 15:40
  • @Fragdog - Oops, I forgot the `~` in `%~f0`. All fixed. – dbenham Mar 08 '15 at 16:13
  • Ah, cheers. I'll have to test it in the morning, gotta get some sleep. I'll let you know how it goes. – Fragdog Mar 08 '15 at 16:18
0

You may define the strings of text in an array that is very easy to manage. The verification of the file may be done reading it with FOR /F command instead of SET /P, and the creation of the file is as simple as your direct ECHO command. I also slightly modified your code in order to made it simpler.

@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION

REM DEFINE THE ARRAY OF LINES
SET "LINE[1]=LONGSTRINGOFTEXT1"
SET "LINE[2]=LONGSTRINGOFTEXT2"
SET "LINE[3]=LONGSTRINGOFTEXT3"
...
SET "LINE[25]=LONGSTRINGOFTEXT25"
SET NUMLINES=25


:SEARCHFILE
if not exist "C:\FOLDER\TEXT.TXT" goto :SEARCHFAIL1

:SEARCHCONTENTS
COLOR A0
ECHO FILE FOUND. VERIFYING FILE.
SET I=0
FOR /F "USEBACKQ DELIMS=" %%A IN ("C:\FOLDER\TEXT.TXT") DO (
   SET /A I=I+1
   FOR %%I IN (!I!) DO IF "%%A" NEQ "!LINE[%%I]!" GOTO :SEARCHFAIL2
)
GOTO :SEARCHSUCCESS


:SEARCHFAIL1
COLOR 40
ECHO FILE NOT FOUND.
ECHO FILE WILL NOW BE CREATED.
GOTO :CREATEFILE


:SEARCHFAIL2
COLOR 40
ECHO FILE NOT VALID AT LINE %I%.
ECHO FILE WILL NOW BE REPAIRED.
GOTO :CREATEFILE

:CREATEFILE
(FOR /L %%I IN (1,1,%NUMLINES%) DO ECHO !LINE[%%I]!) > "C:\FOLDER\TEXT.TXT"

:SEARCHSUCCESS
COLOR A0
ECHO FILE SUCCESSFULLY VALIDATED.
PAUSE
Community
  • 1
  • 1
Aacini
  • 65,180
  • 12
  • 72
  • 108