简体   繁体   English

在文本文件中查找字符串,然后重命名我的日志文件

[英]find string in text file and then renaming my log file

im working on a basic .bat file.我正在处理一个基本的 .bat 文件。 It checks if various files exists and if they dont, it will write "ERROR" in the log file.它检查各种文件是否存在,如果不存在,它将在日志文件中写入“错误”。 I then test this log file for the string "ERROR" and if it does I want to rename my log file, but i seem to be getting an error on my if statement.然后我测试这个日志文件中的字符串“ERROR”,如果确实如此,我想重命名我的日志文件,但我的 if 语句似乎出现错误。 Heres my code..这是我的代码..

set "filename=C:\Temp\%COMPUTERNAME%.txt"
echo Creating .txt file...

echo Getting the Computer name...

echo %COMPUTERNAME% >> filename
echo ArcGIS Desktop 10 File checker
pause
echo Looking for files.....
call:checkFileExists C:\support\msi_local\Oracle10g\marker.txt
pause

FIND "ERROR" filename
echo error level is %ERRORLEVEL%
pause

if %ERRORLEVEL% 1 (
    set "newfileName=C:\Temp\%COMPUTERNAME%%_ERROR.txt"
    rename fileName newfileName
)
pause

:checkFileExists
if exist %~1 (
    echo Success %~1 does exist >> C:\Temp\%COMPUTERNAME%.txt
) else (
    echo ERROR "C:\support\msi_local\Oracle10g\marker.txt"%~1 does not exist >> C:\Temp\%COMPUTERNAME%.txt
)

I get a error -我收到一个错误 -

The syntax of teh command is incorrect.
C:\Windows>if ERRORLEVEL 1(

Where am i going wrong?我哪里出错了? Thanks谢谢

try this:试试这个:

@echo off &SETLOCAL 
set "filename=C:\Temp\%COMPUTERNAME%.txt"
echo Creating .txt file...

echo Getting the Computer name...

>>"%filename%" echo %COMPUTERNAME%
echo ArcGIS Desktop 10 File checker
pause
echo Looking for files.....
call:checkFileExists "C:\support\msi_local\Oracle10g\marker.txt"
pause

FIND "ERROR" "%filename%"
echo error level is %ERRORLEVEL%
pause

if %ERRORLEVEL% equ 1 (
     set "newfileName=C:\Temp\%COMPUTERNAME%_ERROR.txt"
     move "%fileName%" "%newfileName%"
)
pause

:checkFileExists
if exist "%~1" (
     echo Success %~1 does exist >> C:\Temp\%COMPUTERNAME%.txt
) else (
     echo ERROR "C:\support\msi_local\Oracle10g\marker.txt"%~1 does not exist >> C:\Temp\%COMPUTERNAME%.txt
)
if %ERRORLEVEL% 1 (

should be应该

if ERRORLEVEL 1 (

And, since you're retyping the error rather than copy-pasting, note that there must be a space between the 1 and the (而且,由于您是重新输入错误而不是复制粘贴,请注意1(

The error appears that way because batch replaces any %var% with its value at that time (when it is 'parsed') and then executes the line, so batch substitutes whatever has been reported as %errorlevel% from your debug statement :) (eg 1 ) and then tries valiantly to work out what if 1 1 ( means.错误是这样出现的,因为批处理在当时(当它被“解析”时)用它的值替换了任何 %var% 然后执行该行,所以批处理替换了调试语句中报告为%errorlevel%任何内容:)(例如1 ) 然后勇敢地尝试计算出if 1 1 (意味着。

(btw, it would be a good idea to replace if exist %~1 ( with if exist "%~1" ( (顺便说一句, if exist %~1 (替换为if exist "%~1" (

This may seem redundant, removing and then replacing the quotes, BUT if you later decide to change the statement to if exist %file% ( it's only later you'll find out you'll get a crash when %file% contains a space. Best to be ever-mindful of the spaces-in-filenames problem; if you make quoting a habit, you'll be caught out less often. )这似乎是多余的,删除然后替换引号,但是如果您以后决定将语句更改为if exist %file% (只有稍后您会发现当 %file% 包含空格时您会崩溃。最好时刻注意文件名中的空格问题;如果你养成引用的习惯,你就不会经常被发现。)

The two syntaxes are subtly different:这两种语法略有不同:

Old style syntax (MS-DOS era) - check if the error level was 1 or more:旧式语法(MS-DOS 时代) - 检查错误级别是否为 1 或更高:

IF ERRORLEVEL 1

New style syntax - check if the %errorlevel% variable was not equal to 0:新样式语法 - 检查%errorlevel%变量是否不等于 0:

IF %errorlevel% NEQ 0

Note that the new style syntax uses % to indicate a variable, while with the old style syntax, ERRORLEVEL is a special keyword.请注意,新样式语法使用%表示变量,而在旧样式语法中, ERRORLEVEL是一个特殊关键字。

The new style syntax should be preferred, because it will handle programs that return -1 on error.应该首选新样式语法,因为它将处理错误返回-1程序。 If the program can return negative error codes on success, you could handle errors with IF %errorlevel GEQ 1 .如果程序在成功时可以返回负错误代码,您可以使用IF %errorlevel GEQ 1处理错误。 Either way, using the %errorlevel% variable allows a lot more flexibility.无论哪种方式,使用%errorlevel%变量都可以提供更大的灵活性。

For clarity I've used upper case for keywords and lower case for variables, but it should be case sensitive either way.为清楚起见,我对关键字使用大写,对变量使用小写,但无论哪种方式都应该区分大小写。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM