开发者

batch scripting

开发者 https://www.devze.com 2023-03-24 18:36 出处:网络
Intro : I am new to batch scripting. Will like to have a leads on the following scripts. Objective : Have to write a batch command to update an application . And then read the log that is generated

Intro : I am new to batch scripting. Will like to have a leads on the following scripts.

Objective : Have to write a batch command to update an application . And then read the log that is generated after the application has been updated , to check if it has been successfull.

I run the application using a simple batch file , which has the command

"C:\Program Files\Application\Test.exe" /app开发者_如何学Pythonlication paramater /q /from "\\share\file"

The logfile (xyz.log) for the result in generated in

C:\Documents and Settings\<user>\locals~1\temp\Application

I want my batch file to run the first command and read the results generated in the log file (serach for success string).

CanI get any inputs on this please.


This is NOT tested, but it's good starting point, and you can use it to learn some MS-DOS/batch commands. If you need specific syntax help, type in the command prompt [some-command] /?

@echo off
:: variables --------------------
set appexe=C:\Program Files\Application\Test.exe
set share=\\share\file
set logfile=%TEMP%\Application\xyz.log
set appcmd="%appexe%" /param /q /from "%share%"

if not exist "%appexe%" echo Executable not found& goto END
if not exist "%share%" echo Shared folder not found& goto END

:: run executable ---------------
%appcmd%

if not exist "%logfile%" echo Logfile not found& goto END

:: iterate through logfile ------
find "update success text" "%logfile%" >NUL || (echo Update unsuccessful& goto END)
echo Update successful!

:END
pause
exit

Edit: Used find rather than a for loop. +1 and thanks to Andriy M


You can use the FIND command to search a file for a string:

FIND "success" "%TEMP%\Application\xyz.log" >NUL && (ECHO Update successful) || (ECHO Update failed)
  • %TEMP% evaluates to the system temporary folder for the current user.

  • >NUL suppresses the output of the FIND command.

  • && passes control to the subsequent command (ECHO) if the string was found.

  • || passes control to the command after it (the other ECHO) if the search failed.

If you want to perform more than one command as a result of FIND, you can use something like this:

FIND "string" file_name && (command1 & command2 & …) || (command3 & command4 & …)

Alternatively you can use the GOTO command. For example, like this:

FIND "success" "%TEMP%\Application\xyz.log" >NUL || (GOTO searchfailed)
series of commands as a result of successful search
GOTO :EOF

:searchfailed
series of commands as a result of unsuccessful search

:label_name is a label declaration. GOTO searchfailed means jump to the line where the searchfailed label is declared.

GOTO :EOF essentilly exits the batch script. (:EOF is a special pre-defined label that points at the End Of File.)

0

精彩评论

暂无评论...
验证码 换一张
取 消