Im using Hudson for our HTML build main reason for using CI is to validate the html files during each files with cse validator. For validating the HTML I have used the following code
#echo off
PUSHD "F:\Solutions\Documents\Design\html\ValTest"
For %%X in (*.html) do (
"C:\Program Files\HTMLValidator100\cmdlineprocessor" -outputfile output.txt -r1 %%X
set HTMLVAL_ERROR=%ERRORLEVEL%
type output.txt >> result.txt
)
set ERRORLEVEL=%HTMLVAL_ERROR%
POPD
Validation process is working fine but even there is an error in the HTML file hudson is not triggering the build as failed, its always Success.
Please let me know how can I trigger the build failure from batch command.
You should use exit command :
#echo off
PUSHD "F:\Solutions\Documents\Design\html\ValTest" For %%X in (*.html) do ( "C:\Program Files\HTMLValidator100\cmdlineprocessor" -outputfile output.txt -r1 %%X set HTMLVAL_ERROR=%ERRORLEVEL% type output.txt >> result.txt )
POPD
exit %HTMLVAL_ERROR%
which sets the error level of the whole batch.
Related
I wrote a batch script to search a string "Error" in multiple XML files in a folder.
I'm using a for loop to iterate through all the files and using a find command to search the specific string in those files.
But, I see that the searching of string in a particular file happens only after one complete iteration instead of at the line where the find command is written. It doesn't give correct output.
I wrote the following script (Windows):
cd C:\Logs\
setlocal EnableDelayedExpansion
for /r . %%g in (*.xml) do ( set xml_file_name=%%~nxg
find /I "Error" "C:\Logs\!xml_file_name!"
if %errorlevel%==0 (
echo Error found at C:\Logs\!xml_file_name!
) else (
echo No Error found.
exit )
)
There are about 10-15 xml files and if there is a string "Error" in a xml file it should output "error found" else "no error found" for each file.
Please help me with the correct code.
You have an exit in „else“ case? So it quits after first „no error found“
EDIT: try this:
echo off
cd C:\Logs\
setlocal EnableDelayedExpansion
setlocal EnableDelayedExpansion
for /r . %%g in (*.xml) do ( set xml_file_name=%%~nxg
find /I "Error" "C:\Logs\!xml_file_name!" > nul & if errorlevel 1 (
echo No Error found.
) else (
echo Error found at C:\Logs\!xml_file_name!
)
)
I have this website with files on it ending with the same word, such as ".csv". I am making a script to automate the download of the files ending with the same word and save it in a folder.
The instructions was to download the files using curl consecutively, and I know this would be possible by looping through the files and executing curl on each. Then, save the output on a folder.
I know the basics of batch script but I can't find clear reference or benchmark on how could this be done. I am new to curl, just an ethusiast and not really an IT person.
Can anyone give me ideas/instructions on how to do it or, if possible, show sample script for me to follow? Please leave comments on the script for me to better understand also. Thanks, much appreciated!
This is what Ive done so far:
- > #echo off setlocal enabledelayedexpansion
>
> set "MainURL=https://*****/****/***/"
>
> for %%a in (%MainURL/*.properties%) do ( for /f "usebackq tokens=*
> delims==" %%a in ("%%~Fx") do call :curldownload %%a
>
> ) pause goto:eof
>
>
> :curldownload
> set /a count=count+1
set STRING=%* curl -k -O -u username !STRING!| findstr /r [a-z]*.properties ? ? ?
>
> :eof
This is what I came up but not working properly..
My problem is that two FOR loops are working separately, but don't want to work one after another.
The goal is:
The first loop creates XML files and only when the creation has already been done the second loop starts and counts the size of created XML files and writes it into .txt file.
#echo off
Setlocal EnableDelayedExpansion
for /f %%a in ('dir /b /s C:\Users\NekhayenkoO\test\') do (
echo Verarbeite %%~na
jhove -m PDF-hul -h xml -o C:\Users\NekhayenkoO\outputxml\%%~na.xml %%a
)
for /f %%i in ('dir /b /s C:\Users\NekhayenkoO\outputxml\') do (
echo %%~ni %%~zi >> C:\Users\NekhayenkoO\outputxml\size.txt
)
pause
This question can be answered easily when knowing what jhove is.
So I searched in world wide web for jhove, found very quickly the homepage JHOVE | JSTOR/Harvard Object Validation Environment and downloaded also jhove-1_11.zip from SourceForge project page of JHOVE.
All this was done by me to find out that jhove is a Java application which is executed on Linux and perhaps also on Mac using the shell script jhove and on Windows the batch file jhove.bat for making it easier to use by users.
So Windows command interpreter searches in current directory and next in all directories specified in environment variable PATH for a file matching the file name pattern jhove.* having a file extension listed in environment variable PATHEXT because jhove.bat is specified without file extension and without path in the batch file.
But the execution of a batch file from within a batch file without usage of command CALL results in script execution of current batch file being continued in the other executed batch file without ever returning back to the current batch file.
For that reason Windows command interpreter runs into jhove.bat on first file found in directory C:\Users\NekhayenkoO\test and never comes back.
This behavior can be easily watched by using two simple batch files stored for example in C:\Temp.
Test1.bat:
#echo off
cd /D "%~dp0"
for %%I in (*.bat) do Test2.bat "%%I"
echo %~n0: Leaving %~f0
Test2.bat:
#echo %~n0: Arguments are: %*
#echo %~n0: Leaving %~f0
On running from within a command prompt window C:\Temp\Test1.bat the output is:
Test2: Arguments are: "Test1.bat"
Test2: Leaving C:\Temp\Test2.bat
The processing of Test1.bat was continued on Test2.bat without coming back to Test1.bat.
Now Test1.bat is modified to by inserting command CALL after do.
Test1.bat:
#echo off
cd /D "%~dp0"
for %%I in (*.bat) do call Test2.bat "%%I"
echo Leaving %~f0
The output on running Test1.bat from within command prompt window is now:
Test2: Arguments are: "Test1.bat"
Test2: Leaving C:\Temp\Test2.bat
Test2: Arguments are: "Test2.bat"
Test2: Leaving C:\Temp\Test2.bat
Test1: Leaving C:\Temp\Test1.bat
Batch file Test1.bat calls now batch file Test2.bat and therefore the FOR loop is really executed on all *.bat files found in directory of the two batch files.
Therefore the solution is using command CALL as suggested already by Squashman:
#echo off
setlocal EnableDelayedExpansion
for /f %%a in ('dir /b /s "%USERPROFILE%\test\" 2^>nul') do (
echo Verarbeite %%~na
call jhove.bat -m PDF-hul -h xml -o "%USERPROFILE%\outputxml\%%~na.xml" "%%a"
)
for /f %%i in ('dir /b /s "%USERPROFILE%\outputxml\" 2^>nul') do (
echo %%~ni %%~zi>>"%USERPROFILE%\outputxml\size.txt"
)
pause
endlocal
A reference to environment variable USERPROFILE is used instead of C:\Users\NekhayenkoO.
All file names are enclosed in double quotes in case of any file found in the directory contains a space character or any other special character which requires enclosing in double quotes.
And last 2>nul is added which redirects the error message output to handle STDERR by command DIR on not finding any file to device NUL to suppress it. The redirection operator > must be escaped here with ^ to be interpreted on execution of command DIR and not as wrong placed redirection operator on parsing already the command FOR.
For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.
call /?
cd /?
dir /?
echo /?
for /?
And read also the Microsoft article Using command redirection operators.
You need to use the START command with the /WAIT flag when you launch an external application.
I believe it would look something like this:
START /WAIT jhove -m PDF-hul -h xml -o C:\Users\NekhayenkoO\outputxml\%%~na.xml %%a
That should cause the batch file to pause and wait for the external application to finish before proceeding.
I made a piece of batch-code, and I thought this will work. What I'm thinking that this code is doing? I have some plugins and I want to test if the deploy correct. So I get the pluginlink from the plugins.txt. Then I get the plugin from SVN with the java sentence. I deploy the plugin and get the feedback in test1.txt. Then I do a findStr in that file and searchs for "BUILD SUCCESSFUL" if it is there I want to add the sentence Build Gelukt and if it fails I want to add Build Fout. But I get always the answer Build Gelukt, while as you can see in the image he sends back that the build is Failed.
Whats wrong with this piece of code?
for /f "tokens=* delims= " %%a in (plugins.txt) do (
echo %%a
cd "C:\dotCMS Automatic Install"
java -cp .;"C:\dotCMS Automatic Install\svnkit.jar" Test %%a
cd %dotcms_home%
call ant deploy-plugins > test1.txt
FindStr "SUCCESSFUL" test1.txt
if %ERRORLEVEL% ==1 (echo ^<tr BGCOLOR=\"#FFFFFF\"^>^<td^>%%a^</td^>^<td^>Build Fout^</td^>^</tr^> >> C:\dotCMSResults\goedje.html ) else (echo ^<tr BGCOLOR=\"#00FF00\"^>^<td^>%%a^</td^>^<td^>Build Gelukt^</td^>^</tr^> >> C:\dotCMSResults\goedje.html)
del test1.txt
rem call ant undeploy-plugins >> test.txt
)
Classic batch problem - you are setting your ERRORLEVEL and attempting to access it using %ERRORLEVEL% within the same DO() clause. %VAR% expansion happens at parse time, and the entire FOR ... DO() statement is parsed once, so you are seeing the value of ERRORLEVEL before the statement was executed. Obviously that won't work.
jeb alluded to the answer in his comment regarding disappearing quotes. Your problem will be fixed if you setlocal enableDelayedExpansion at the top, and then use !ERRORLEVEL! instead of %ERRORLEVEL%. Also, GregHNZ is correct in that the ERRORLEVEL test should occur immediately after your FINDSTR statement.
There are other ways to handle ERRORLEVEL within parentheses that don't require delayed expansion:
The following tests if ERRORLEVEL is greater than or equal 1
IF ERRORLEVEL 1 (...) ELSE (...)
And below conditionally executes commands based on the outcome of the prior command
FindStr "SUCCESSFUL" test1.txt && (
commands to execute if FindStr succeeded
) || (
commands to execute if prior command failed.
)
The %ErrorLevel% variable applies to the immediately previous command only.
So when you do this:
echo Errorlevel: %ERRORLEVEL%
With your current code, you are getting the error level of the CD command above
Try putting your if %ERRORLEVEL% ==1 line immediately after the FindStr command, and then do the del and the cd afterward. Obviously you'll need to put the full path to the html file in your echo statement.
How would one go best about checking for existence of all files before building?
Let me explain; I mostly build stuff from the command prompt. No problems there, just put the build command and all in one .bat /.cmd file, and run it. It works fine.
But, for the normal running of my program, for example, I need several source files for the build, and then an additional few data files, measured data and such.
Is there a way to test via a batch file whether a file exists, and if it exists just write OK?
file1.for OK
file2.for OK
datafile.txt OK
data.dat MISSING FROM DIRECTORY
How could this be accomplished?
As a slightly more advanced approach:
#Echo Off
SetLocal EnableExtensions EnableDelayedExpansion
Set FileList=file1.for file2.for "File with spaces" ...
Set Build=1
For %%f In (%FileList%) Do Call :FileExists %%f
If Not Defined Build (
Echo.
Echo Build aborted. Files were missing.
GoTo :EOF
)
...
GoTo :EOF
:FileExists
Set FileName=%~1
If Exist "!FileName!" (
Echo !FileName! OK
) Else (
Echo !FileName! MISSING FROM DIRECTORY
Set Build=
)
GoTo :EOF
You can put all files into the FileList variable. The Build variable controls whether to continue with the build. A single missing file causes it to cancel.
Something like this?
#ECHO OFF
IF EXIST "c:\myfile1.txt" (ECHO myfile1.txt OK) ELSE (ECHO myfile1.txt FILE MISSING FROM DIRECTORY)
IF EXIST "c:\myfile2.txt" (ECHO myfile2.txt OK) ELSE (ECHO myfile2.txt FILE MISSING FROM DIRECTORY)
For a list of available commands, see http://ss64.com/nt/