我很确定这已经在这里被问过,但是我找不到能够真正帮助我的线程。
我想要做的是有一个查找文本文件的批处理文件,如果找到它,请验证内容是否正确。 我目前的代码如下:
: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
最初,文本文件将有25行,批处理将验证每一行:
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
如果可能的话,我仍然希望这样做,但我仍然不知道在将“LONGSTRINGOFTEXT”输入批处理的行中语法错误是什么。
有什么建议吗?
答案 0 :(得分:0)
这似乎是一件奇怪的事情,但是你不必为改变你的代码而改变太多。
没有<<
重定向来读取下一行。您需要为许多输入重定向一次。此外,如果有空格或有毒字符,您应该引用您的文本以使您的IF语句正常工作。
< "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
在读取行之前,您确实应该明确地取消定义变量,因为如果输入为空,SET / P将不会清除任何现有值。使用延迟扩展更安全。您可以通过简单的循环完成所有输入。您可以将目标文件嵌入脚本中并从那里读取所需的值。您可以将测试逻辑放在子例程中,如果测试失败则返回错误。只要没有任何目标行以:
开头,以下内容就会起作用。
@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
上述优点是您只需添加更多行进行测试,即可轻松将其调整为任何大小的文件。
答案 1 :(得分:0)
您可以在array中定义非常容易管理的文字字符串。可以使用FOR / F命令而不是SET / P来读取文件,并且创建文件就像直接ECHO命令一样简单。我还稍微修改了你的代码,以使其更简单。
@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