1

I need some help with windows batch script

What am i trying to achieve:

  • retrieve the youngest (last) revision from repository
  • search for a particular string in that file
  • if string exists, load a web page

What i have tried:

set REVNO=$(svnlook youngest D:\Subversion\...)
set REVURL=D:\Subversion\...\db\revprops\0\
findstr /m "string_I_am_searching_for" %REVURL%%REVNO%
if %errorlevel%==0 (
  start http://webpage
)

Why am i trying to do this:

I want to start Hudson CI automatic test after a commit that has a particular string in message (ex. Autobuild: true)

I am not able to debug my code, since i am unfamiliar with Windows batch scripting

In my script I used:

  • Concatenation of variables (which might be wrong) because i was not sure if i can append variable to the path
  • findstr to find if a file contains string (i know that findstr can be used on variables too)

I would not be surprised if my small script has semantic problems as well as syntactic problems, because, as I have already mentioned, I haven't done that much of batch scripting before

I have no requirements, no specifications, so the script may be implemented in any way possible. So i am open for suggestions

P.S. I have read also that using overweight procedures on post-commit is not a good idea, because svn does not return controls back to the user until it is done with everything -> This is not a big problem

EDIT: I found by side testing that i didn't really aquire the youngest revision number, but rather used the command string. SO after googling a little more i found that it is not that easy in windows to call some command and store the result in variable. So i have decided to use file to store intermediate results.

This is what i have tried:

cd D:\Subversion\...\hooks
SET OUTPUT=testing
FOR /F "tokens=*" %a in ('revision.bat') do ( SET OUTPUT=%a )
findstr /m "o" D:\Subversion\...\db\revprops\0\%a
if %errorlevel%==0 (
  start http://webpage
)

in revision.bat:

svnlook youngest D:\Subversion\...

Now i get this error in SVN:

post-commit hook failed (exit code 255) with output:
a" ) was unexpected at this time.

I believe it is because i create variable a in a wrong place at wrong time. But I do not get this error if i run bat file manually

Boris Mocialov
  • 3,439
  • 2
  • 28
  • 55
  • You told us, what you have tried, but nothing about the results, error messages and so on. – Endoro Jul 09 '13 at 12:05
  • @Endoro well, as i have said, i do not know how to debug batch scripts. And since this is executed on the background, i do not see command prompt comming up. When i try to write to file using `echo %REVURL%%REVNO% >> file.txt` the file is not created – Boris Mocialov Jul 09 '13 at 12:07
  • The file may be created, but because you're not specifying the full path to `file.txt' you don't know where it's going. Specify a **full path** to `file.txt`, and make sure that whatever account your server process runs under has permission write to that path. – alroc Jul 09 '13 at 12:39
  • @alroc now i am trying to save intermediate results in files, but then i get another error. Updated my question – Boris Mocialov Jul 09 '13 at 12:58

1 Answers1

0

Found one way to do it - to store intermediate function in .bat file:

post-commit.bat

@ECHO OFF
cd D:\Subversion\...\hooks
SET OUTPUT
FOR /F "tokens=*" %%a in ('revision.bat') do ( SET OUTPUT=%%a )
findstr /m "o" D:\Subversion\...\db\revprops\0\%OUTPUT%
if %errorlevel%==0 (
  start http://website
)

revision.bat

svnlook youngest D:\Subversion\...

This is rather a workaround than a solution in my opinion

Thanks to: https://serverfault.com/questions/245679/store-output-of-windows-command-in-batch-file

Community
  • 1
  • 1
Boris Mocialov
  • 3,439
  • 2
  • 28
  • 55
  • `start` will invoke Internet Explorer (or whatever your default web browser is), which will be expecting an interactive user at some point. What are you doing about all of the iexplore.exe processes that will be spawned but not terminated? – alroc Jul 09 '13 at 13:29
  • @alroc there is no interaction needed, website launches service which does all the rest work. Haven't thought much about processes yet. I am open for all advices and proposals – Boris Mocialov Jul 09 '13 at 13:33
  • After a few weeks of this, you're going to have a bunch of abandoned iexplore.exe processes on your server. You *have* to think about this before implementing such a script If accessing a URL once is all that's required to trigger a process, use something like `wget` or a PowerShell `Invoke-WebRequest` which won't leave orphaned processes strewn about. Or find a way to configure Hudson to monitor for this change on its own, without requiring the hook script at all. – alroc Jul 09 '13 at 13:46
  • @alroc this is a good point. Thank you. The first priority is to make it work properly and then i will think about client like cURL or wget – Boris Mocialov Jul 09 '13 at 13:52
  • @alroc bitsadmin seems to be still available for Windows – Boris Mocialov Jul 09 '13 at 13:54
  • BITSAdmin seems like the wrong tool for the job. Just because something *can* use HTTP doesn't mean that you *should* use it every time you need an HTTP client. – alroc Jul 09 '13 at 13:57
  • @alroc When you expressed your concern regarding processes i decided to check and if internet explorer is the default browser, then processes are created but not terminated, but if i have chrome as the default browser, processes are terminated. This is what i can say after some experiments - i have some `iexplorer` processes, but none `chrome` – Boris Mocialov Jul 09 '13 at 14:16
  • I would not install Chrome as the default browser on a server just to take advantage of what might be a bug. Do it the right way to start with. – alroc Jul 09 '13 at 14:21