Showing posts with label example. Show all posts
Showing posts with label example. Show all posts

Saturday, September 29, 2012

My Excel macro selection

Some easy to use Excel macro’s. All explained below are available in one Excel Macro Example.

All macro’s are combined in one example Excel sheet template to test the usage of it.

Password Generation

Function RndPassword(vLength)
'This function will generate a random strong password of variable
'length.
'This script is provided under the Creative Commons license located
'at http://creativecommons.org/licenses/by-nc/2.5/ . It may not
'be used for commercial purposes with out the expressed written consent
'of NateRice.com

For x = 1 To vLength
Randomize
vChar = Int(89 * Rnd) + 33
If vChar = 34 Then 'this is quote character, replace it with a single quote
vChar = 39
End If
RndPassword = RndPassword & Chr(vChar)
Next
End Function








An example usage of this Password generation function: I have a list with on each row a button to generate a password in the cell next to the button. Each button is linked to the same macro (for easy copy paste of the row). The cell to fill with the new password is automatically detected based on the cell in which the button is drawn. Before setting the new generated password, a warning is shown in a message box.









ExcelMacroPasswordGeenration









Sub Process_GenNewPassword()
Dim LRange As String
Dim RowOffset As Integer
Dim ColumnOffset As Integer
Dim newGenPassword As String
'Find cell that button resides in
LName = Application.Caller
Set bBox = ActiveSheet.Buttons(LName)
LRange = bBox.TopLeftCell.Address
RowOffset = 0 ' relative location of the row in which to work, same row used
FirstNameColumnOffset = -5 ' relative location of firstname column in row to show in warning message box
LastNameColumnOffset = -4 ' relative location of lastname column in row to show in warning message box
PasswordColumnOffset = 2 ' relative location of column in row where the generated password should be set
sResult = MsgBox("The password for user '" & Range(LRange).Offset(RowOffset, FirstNameColumnOffset).Value & " " & Range(LRange).Offset(RowOffset, LastNameColumnOffset).Value & "' will be replaced with a new random password!", vbExclamation + vbOKCancel, "New password generation")
If (sResult <> 1) Then
Exit Sub
End If
newGenPassword = RndPassword(10)
Range(LRange).Offset(RowOffset, PasswordColumnOffset).Value = newGenPassword
End Sub








Checkboxes









Checkbox LinkedCell









Whenever you need many checkboxes in your sheet, you'll probably need each checkbox to be linked to the cell it is residing on.




When copy-pasting rows or columns, the checkboxes will be copied as well, but they will still be linked to the same cell as the checkbox you started to copy-paste. The little macro shown below, will update each Checkbox on the active sheet and set it's linked cell to the cell on which the Checkbox is drawn.









Sub setCheckBoxLinkedCell()
'Loop through all Checkboxes in the active sheet
'Set for each Checkbox it's "LinkedCell" value to the cell on which the Checkbox is drawn
Application.ScreenUpdating = False
' turns off screen updating
Application.DisplayStatusBar = True
' makes sure that the statusbar is visible
Application.StatusBar = "Updating linked cells of all Checkboxes in this sheet."
For Each chk In ActiveSheet.Checkboxes
LRow = ActiveSheet.Checkboxes(chk.Name).TopLeftCell.Row
LColumn = ActiveSheet.Checkboxes(chk.Name).TopLeftCell.Column
cA1 = Application.ConvertFormula("R" & LRow & "C" & LColumn, xlR1C1, xlA1)

ActiveSheet.Shapes.Range(Array(chk.Name)).Select
With Selection
.Value = xlOff
.LinkedCell = cA1
.Display3DShading = False
End With
Next chk
Application.StatusBar = "Linked cells of all Checkboxes in this sheet are updated."
Application.ScreenUpdating = True
End Sub








Save date on Checkbox check









The macro below will set the date in the cell next to the Checkbox whenever the checkbox is checked. All checkboxes are linked to the same macro, for easy copy-pasting. The cell in which to save the date will be detected based on the checkbox location.









ExcelMacroCheckboxDate









Sub Process_CheckBox()
Dim cBox As CheckBox
Dim LRow As Integer
Dim LRange As String

LName = Application.Caller
Set cBox = ActiveSheet.Checkboxes(LName)

'Find address that checkbox resides in
LRange = cBox.TopLeftCell.Address
DateRowOffset = 0 ' row offset (relative to the checkbox location) in which the date should be set
DateColumnOffset = 1 ' column offset (relative to the checkbox location) in which the date should be set

'Change date if checkbox is checked
If cBox.Value > 0 Then
Range(LRange).Offset(DateRowOffset, DateColumnOffset).Value = Date

'Clear date if checkbox is unchecked
Else
Range(LRange).Offset(DateRowOffset, DateColumnOffset).Value = Null
End If
End Sub








Create Folder Structure









I copied my previously explained 'Create Folder Structure' in this Excel Macros Example. For all details, see my previous blog post. An import of an existing system folder structure is now added in there as well.









ExcelMacroCreateFolderStructure









Run macro upon opening a workbook




To run a macro whenever the workbook Excel sheet is opened, the Sub Workbook_Open in 'ThisWorkbook' can be used. This is needed if you want to link some keyboard keys to specific macro's.







Link keys to macro




To link a keyboard key to a specific macro action, the Application.OnKey "..." can be used in the auto startup macro. The keys must be unlinked before closing the workbook. I used this functionality in my Exam Point Counter workbook, described in this previous blog post. In 'ThisWorkbook':







'startup macro
Private Sub Workbook_Open()
Application.OnKey "t", "tKeyPressed"
Application.OnKey "e", "eKeyPressed"
Application.OnKey "s", "sKeyPressed"
Application.OnKey "t", "tKeyPressed"
End Sub
Private Sub Workbook_BeforeClose()
Application.OnKey "t"
Application.OnKey "e"
Application.OnKey "s"
Application.OnKey "t"
End Sub



In any module, for example KeyboardAction:







Sub tKeyPressed()
Application.DisplayStatusBar = True
Application.StatusBar = "t key linked to special action in KeyboardAction module"
End Sub
Sub eKeyPressed()
Application.DisplayStatusBar = True
Application.StatusBar = "e key linked to special action in KeyboardAction module"
End Sub
Sub sKeyPressed()
Application.DisplayStatusBar = True
Application.StatusBar = "s key linked to special action in KeyboardAction module"
End Sub








Search Lookup









The search lookup functionality has also been described in details in this previous blog post. The macro is included in this Excel Macro Example as well.









The formula for E9 below looks like:









 ExcelMacroSearchLookupFomula









ExcelMacroSearchLookup









Open Save









A button with linked macro to navigate to a folder is shown in the module 'OpenSave'.








A basic example of the resource exporter I showed in a previous post, is included as well. It will export the example data set into a text file, and while doing so, all special characters will be converted (eg &#233). For rows in the list of AlternativeEncoding, an alternative encoding for special characters will be used (eg \u00E9). To make the sheet readable, the special characters can be converted back into readable special characters. The conversion is based on a hex2ascii converter, unicode encoding, html encoding and decoding.









When you would reuse this code, please note, in order to support FileSystemObject, you’ll need to add reference to Microsoft scripting runtime in your Excel workbook VBA. In order to do so, open the Visual Basic environment in Excel (ALT+F11) > Menu ‘Tools’ > ‘Reference’ > Enable ‘Microsoft Scripting Runtime’. If this isn’t done in the workbook, the error ‘User defined type not defined’ will appear when writing an export file on ‘Dim fso As New FileSystemObject’.









ExcelMacroOpenSave









When the special characters are encoded using the macro, they will look like this:









ExcelMacroOpenSaveEncoded









PhoneBook vCard export



A sheet in which all phonebook data can be added (can be exported from any other application) is now available. Once all contact information is set, it can be exported into a vCard format which is supported by many address and contact management applications. More details can be found in this specific blog post.



image



All code is available in the Excel Macro Example.

Sunday, April 26, 2009

Find-Replace text in files from command line

In Linux one can easily find and replace some text in files by using the sed application. In Windows, I couldn’t find a decent build in equivalent. I ended by using the ported version of sed for Windows. I works quite well using the same syntax and doesn’t require to be installed. Just make sure sed.exe, libiconv2.dll and libintl3.dll are existing in the same folder or on the classpath.

One should know any file altered by sed for Windows will result in this file being also converted from Windows line ending (using 2 characters: Carriage Return + Line Feed) to Linux line endings (only 1 character: Line Feed). This can sometime give some strange results. To overcome this, one should make sure the last replacement in the file is set to replace the Line Feeds back to Carriage Return + Line Feed:

sed -i "s/$/\r/" "%DIR%\file.txt"


Some other useful sed commands are available from sed sourcefourge:



FILE SPACING:
 # double space a file
 sed G
 # double space a file which already has blank lines in it. Output file
 # should contain no more than one blank line between lines of text.
 sed '/^$/d;G'
 # triple space a file
 sed 'G;G'
 # undo double-spacing (assumes even-numbered lines are always blank)
 sed 'n;d'
 # insert a blank line above every line which matches "regex"
 sed '/regex/{x;p;x;}'
 # insert a blank line below every line which matches "regex"
 sed '/regex/G'
 # insert a blank line above and below every line which matches "regex"
 sed '/regex/{x;p;x;G;}'
NUMBERING:
 # number each line of a file (simple left alignment). Using a tab (see
 # note on '\t' at end of file) instead of space will preserve margins.
 sed = filename | sed 'N;s/\n/\t/'
 # number each line of a file (number on left, right-aligned)
 sed = filename | sed 'N; s/^/     /; s/ *\(.\{6,\}\)\n/\1  /'
 # number each line of file, but only print numbers if line is not blank
 sed '/./=' filename | sed '/./N; s/\n/ /'
 # count lines (emulates "wc -l")
 sed -n '$='
TEXT CONVERSION AND SUBSTITUTION:
 # IN UNIX ENVIRONMENT: convert DOS newlines (CR/LF) to Unix format.
 sed 's/.$//'               # assumes that all lines end with CR/LF
 sed 's/^M$//'              # in bash/tcsh, press Ctrl-V then Ctrl-M
 sed 's/\x0D$//'            # works on ssed, gsed 3.02.80 or higher
 # IN UNIX ENVIRONMENT: convert Unix newlines (LF) to DOS format.
 sed "s/$/`echo -e \\\r`/"            # command line under ksh
 sed 's/$'"/`echo \\\r`/"             # command line under bash
 sed "s/$/`echo \\\r`/"               # command line under zsh
 sed 's/$/\r/'                        # gsed 3.02.80 or higher
 # IN DOS ENVIRONMENT: convert Unix newlines (LF) to DOS format.
 sed "s/$//"                          # method 1
 sed -n p                             # method 2
 # IN DOS ENVIRONMENT: convert DOS newlines (CR/LF) to Unix format.
 # Can only be done with UnxUtils sed, version 4.0.7 or higher. The
 # UnxUtils version can be identified by the custom "--text" switch
 # which appears when you use the "--help" switch. Otherwise, changing
 # DOS newlines to Unix newlines cannot be done with sed in a DOS
 # environment. Use "tr" instead.
 sed "s/\r//" infile >outfile         # UnxUtils sed v4.0.7 or higher
 tr -d \r <infile >outfile            # GNU tr version 1.22 or higher
 # delete leading whitespace (spaces, tabs) from front of each line
 # aligns all text flush left
 sed 's/^[ \t]*//'                    # see note on '\t' at end of file
 # delete trailing whitespace (spaces, tabs) from end of each line
 sed 's/[ \t]*$//'                    # see note on '\t' at end of file
 # delete BOTH leading and trailing whitespace from each line
 sed 's/^[ \t]*//;s/[ \t]*$//'
 # insert 5 blank spaces at beginning of each line (make page offset)
 sed 's/^/     /'
 # align all text flush right on a 79-column width
 sed -e :a -e 's/^.\{1,78\}$/ &/;ta'  # set at 78 plus 1 space
 # center all text in the middle of 79-column width. In method 1,
 # spaces at the beginning of the line are significant, and trailing
 # spaces are appended at the end of the line. In method 2, spaces at
 # the beginning of the line are discarded in centering the line, and
 # no trailing spaces appear at the end of lines.
 sed  -e :a -e 's/^.\{1,77\}$/ & /;ta'                     # method 1
 sed  -e :a -e 's/^.\{1,77\}$/ &/;ta' -e 's/\( *\)\1/\1/'  # method 2
 # substitute (find and replace) "foo" with "bar" on each line
 sed 's/foo/bar/'             # replaces only 1st instance in a line
 sed 's/foo/bar/4'            # replaces only 4th instance in a line
 sed 's/foo/bar/g'            # replaces ALL instances in a line
 sed 's/\(.*\)foo\(.*foo\)/\1bar\2/' # replace the next-to-last case
 sed 's/\(.*\)foo/\1bar/'            # replace only the last case
 # substitute "foo" with "bar" ONLY for lines which contain "baz"
 sed '/baz/s/foo/bar/g'
 # substitute "foo" with "bar" EXCEPT for lines which contain "baz"
 sed '/baz/!s/foo/bar/g'
 # change "scarlet" or "ruby" or "puce" to "red"
 sed 's/scarlet/red/g;s/ruby/red/g;s/puce/red/g'   # most seds
 gsed 's/scarlet\|ruby\|puce/red/g'                # GNU sed only
 # reverse order of lines (emulates "tac")
 # bug/feature in HHsed v1.5 causes blank lines to be deleted
 sed '1!G;h;$!d'               # method 1
 sed -n '1!G;h;$p'             # method 2
 # reverse each character on the line (emulates "rev")
 sed '/\n/!G;s/\(.\)\(.*\n\)/&\2\1/;//D;s/.//'
 # join pairs of lines side-by-side (like "paste")
 sed '$!N;s/\n/ /'
 # if a line ends with a backslash, append the next line to it
 sed -e :a -e '/\\$/N; s/\\\n//; ta'
 # if a line begins with an equal sign, append it to the previous line
 # and replace the "=" with a single space
 sed -e :a -e '$!N;s/\n=/ /;ta' -e 'P;D'
 # add commas to numeric strings, changing "1234567" to "1,234,567"
 gsed ':a;s/\B[0-9]\{3\}\>/,&/;ta'                     # GNU sed
 sed -e :a -e 's/\(.*[0-9]\)\([0-9]\{3\}\)/\1,\2/;ta'  # other seds
 # add commas to numbers with decimal points and minus signs (GNU sed)
 gsed -r ':a;s/(^|[^0-9.])([0-9]+)([0-9]{3})/\1\2,\3/g;ta'
 # add a blank line every 5 lines (after lines 5, 10, 15, 20, etc.)
 gsed '0~5G'                  # GNU sed only
 sed 'n;n;n;n;G;'             # other seds
SELECTIVE PRINTING OF CERTAIN LINES:
 # print first 10 lines of file (emulates behavior of "head")
 sed 10q
 # print first line of file (emulates "head -1")
 sed q
 # print the last 10 lines of a file (emulates "tail")
 sed -e :a -e '$q;N;11,$D;ba'
 # print the last 2 lines of a file (emulates "tail -2")
 sed '$!N;$!D'
 # print the last line of a file (emulates "tail -1")
 sed '$!d'                    # method 1
 sed -n '$p'                  # method 2
 # print the next-to-the-last line of a file
 sed -e '$!{h;d;}' -e x              # for 1-line files, print blank line
 sed -e '1{$q;}' -e '$!{h;d;}' -e x  # for 1-line files, print the line
 sed -e '1{$d;}' -e '$!{h;d;}' -e x  # for 1-line files, print nothing
 # print only lines which match regular expression (emulates "grep")
 sed -n '/regexp/p'           # method 1
 sed '/regexp/!d'             # method 2
 # print only lines which do NOT match regexp (emulates "grep -v")
 sed -n '/regexp/!p'          # method 1, corresponds to above
 sed '/regexp/d'              # method 2, simpler syntax
 # print the line immediately before a regexp, but not the line
 # containing the regexp
 sed -n '/regexp/{g;1!p;};h'
 # print the line immediately after a regexp, but not the line
 # containing the regexp
 sed -n '/regexp/{n;p;}'
 # print 1 line of context before and after regexp, with line number
 # indicating where the regexp occurred (similar to "grep -A1 -B1")
 sed -n -e '/regexp/{=;x;1!p;g;$!N;p;D;}' -e h
 # grep for AAA and BBB and CCC (in any order)
 sed '/AAA/!d; /BBB/!d; /CCC/!d'
 # grep for AAA and BBB and CCC (in that order)
 sed '/AAA.*BBB.*CCC/!d'
 # grep for AAA or BBB or CCC (emulates "egrep")
 sed -e '/AAA/b' -e '/BBB/b' -e '/CCC/b' -e d    # most seds
 gsed '/AAA\|BBB\|CCC/!d'                        # GNU sed only
 # print paragraph if it contains AAA (blank lines separate paragraphs)
 # HHsed v1.5 must insert a 'G;' after 'x;' in the next 3 scripts below
 sed -e '/./{H;$!d;}' -e 'x;/AAA/!d;'
 # print paragraph if it contains AAA and BBB and CCC (in any order)
 sed -e '/./{H;$!d;}' -e 'x;/AAA/!d;/BBB/!d;/CCC/!d'
 # print paragraph if it contains AAA or BBB or CCC
 sed -e '/./{H;$!d;}' -e 'x;/AAA/b' -e '/BBB/b' -e '/CCC/b' -e d
 gsed '/./{H;$!d;};x;/AAA\|BBB\|CCC/b;d'         # GNU sed only
 # print only lines of 65 characters or longer
 sed -n '/^.\{65\}/p'
 # print only lines of less than 65 characters
 sed -n '/^.\{65\}/!p'        # method 1, corresponds to above
 sed '/^.\{65\}/d'            # method 2, simpler syntax
 # print section of file from regular expression to end of file
 sed -n '/regexp/,$p'
 # print section of file based on line numbers (lines 8-12, inclusive)
 sed -n '8,12p'               # method 1
 sed '8,12!d'                 # method 2
 # print line number 52
 sed -n '52p'                 # method 1
 sed '52!d'                   # method 2
 sed '52q;d'                  # method 3, efficient on large files
 # beginning at line 3, print every 7th line
 gsed -n '3~7p'               # GNU sed only
 sed -n '3,${p;n;n;n;n;n;n;}' # other seds
 # print section of file between two regular expressions (inclusive)
 sed -n '/Iowa/,/Montana/p'             # case sensitive
SELECTIVE DELETION OF CERTAIN LINES:
 # print all of file EXCEPT section between 2 regular expressions
 sed '/Iowa/,/Montana/d'
 # delete duplicate, consecutive lines from a file (emulates "uniq").
 # First line in a set of duplicate lines is kept, rest are deleted.
 sed '$!N; /^\(.*\)\n\1$/!P; D'
 # delete duplicate, nonconsecutive lines from a file. Beware not to
 # overflow the buffer size of the hold space, or else use GNU sed.
 sed -n 'G; s/\n/&&/; /^\([ -~]*\n\).*\n\1/d; s/\n//; h; P'
 # delete all lines except duplicate lines (emulates "uniq -d").
 sed '$!N; s/^\(.*\)\n\1$/\1/; t; D'
 # delete the first 10 lines of a file
 sed '1,10d'
 # delete the last line of a file
 sed '$d'
 # delete the last 2 lines of a file
 sed 'N;$!P;$!D;$d'
 # delete the last 10 lines of a file
 sed -e :a -e '$d;N;2,10ba' -e 'P;D'   # method 1
 sed -n -e :a -e '1,10!{P;N;D;};N;ba'  # method 2
 # delete every 8th line
 gsed '0~8d'                           # GNU sed only
 sed 'n;n;n;n;n;n;n;d;'                # other seds
 # delete lines matching pattern
 sed '/pattern/d'
 # delete ALL blank lines from a file (same as "grep '.' ")
 sed '/^$/d'                           # method 1
 sed '/./!d'                           # method 2
 # delete all CONSECUTIVE blank lines from file except the first; also
 # deletes all blank lines from top and end of file (emulates "cat -s")
 sed '/./,/^$/!d'          # method 1, allows 0 blanks at top, 1 at EOF
 sed '/^$/N;/\n$/D'        # method 2, allows 1 blank at top, 0 at EOF
 # delete all CONSECUTIVE blank lines from file except the first 2:
 sed '/^$/N;/\n$/N;//D'
 # delete all leading blank lines at top of file
 sed '/./,$!d'
 # delete all trailing blank lines at end of file
 sed -e :a -e '/^\n*$/{$d;N;ba' -e '}'  # works on all seds
 sed -e :a -e '/^\n*$/N;/\n$/ba'        # ditto, except for gsed 3.02.*
 # delete the last line of each paragraph
 sed -n '/^$/{p;h;};/./{x;/./p;}'
SPECIAL APPLICATIONS:
 # remove nroff overstrikes (char, backspace) from man pages. The 'echo'
 # command may need an -e switch if you use Unix System V or bash shell.
 sed "s/.`echo \\\b`//g"    # double quotes required for Unix environment
 sed 's/.^H//g'             # in bash/tcsh, press Ctrl-V and then Ctrl-H
 sed 's/.\x08//g'           # hex expression for sed 1.5, GNU sed, ssed
 # get Usenet/e-mail message header
 sed '/^$/q'                # deletes everything after first blank line
 # get Usenet/e-mail message body
 sed '1,/^$/d'              # deletes everything up to first blank line
 # get Subject header, but remove initial "Subject: " portion
 sed '/^Subject: */!d; s///;q'
 # get return address header
 sed '/^Reply-To:/q; /^From:/h; /./d;g;q'
 # parse out the address proper. Pulls out the e-mail address by itself
 # from the 1-line return address header (see preceding script)
 sed 's/ *(.*)//; s/>.*//; s/.*[:<] *//'
 # add a leading angle bracket and space to each line (quote a message)
 sed 's/^/> /'
 # delete leading angle bracket & space from each line (unquote a message)
 sed 's/^> //'
 # remove most HTML tags (accommodates multiple-line tags)
 sed -e :a -e 's/<[^>]*>//g;/</N;//ba'
 # extract multi-part uuencoded binaries, removing extraneous header
 # info, so that only the uuencoded portion remains. Files passed to
 # sed must be passed in the proper order. Version 1 can be entered
 # from the command line; version 2 can be made into an executable
 # Unix shell script. (Modified from a script by Rahul Dhesi.)
 sed '/^end/,/^begin/d' file1 file2 ... fileX | uudecode   # vers. 1
 sed '/^end/,/^begin/d' "$@" | uudecode                    # vers. 2
 # sort paragraphs of file alphabetically. Paragraphs are separated by blank
 # lines. GNU sed uses \v for vertical tab, or any unique char will do.
 sed '/./{H;d;};x;s/\n/={NL}=/g' file | sort | sed '1s/={NL}=//;s/={NL}=/\n/g'
 gsed '/./{H;d};x;y/\n/\v/' file | sort | sed '1s/\v//;y/\v/\n/'
 # zip up each .TXT file individually, deleting the source file and
 # setting the name of each .ZIP file to the basename of the .TXT file
 # (under DOS: the "dir /b" switch returns bare filenames in all caps).
 echo @echo off >zipup.bat
 dir /b *.txt | sed "s/^\(.*\)\.TXT/pkzip -mo \1 \1.TXT/" >>zipup.bat

End (kill) some application from command line

Different methods exist in Windows to end or kill some application from command line.

The normal way to do this is by using the TASKKILL command, for example to end FireFox one can use:

TASKKILL /F /IM “firefox.exe”


When you need to stop some command window it can be dangerous to just kill any cmd.exe, so one could use the window title to kill a specific batch window, for example to kill any batch window with name starting with “SIM-” I used:



TASKKILL /FI "WINDOWTITLE eq SIM-*"


But sometimes you’ll need to be more specific, I wanted an automated way to kill some Java application but only when it was started with some specific parameter. The only way I found to do so on Windows was by using the WMIC commands. This example show how I killed our Java monitor application which started as follows:



java -Dapp.prop.file.monitor="monitor_system.properties" -classpath SOME_JARS Monitor


Kill this specific Java instance:



wmic process where "name like '%%java.exe%%' AND commandline like '%%monitor%%'" DELETE


Before killing your application, you want to make sure your query will result in the correct process, one can easily check the query by executing the similar wmic command without yet killing the process:



wmic process where "name like '%%java.exe%%' AND commandline like '%%monitor%%'" get processid, name, commandline /VALUE


More examples and combinations that can be used with wmic are available on this page.



The Linux equivalent we used was pkill, for example to kill our simulators we used:



pkill -f "simulator.jar"

Sunday, April 19, 2009

SVN Automated repeated backup / dump – Windows / Linux

Every project using SVN should have an automated backup system in place for their SVN repository. We decided we wanted a weekly incremental backup. But we also wanted to easily create a new initial backup. I scripted it on Windows and Linux with the Shell/Batch scripts shown below.

For both scripts, the same methods is used: we keep track of the last backed up revision in a file called svn_backup_delta.txt. Every time the script is started, an incremental dump is exported and archived, starting from the last dumped revision till the latest available revision. If the latest revision is the same as the last dumped revision, nothing will happen.

If the svn_backup_delta.txt doesn’t exist or the revision could not be read correctly, a complete initial dump from revision 0 till the latest revision will be exported and archived. The scripts are called daily or weekly with a standard OS scheduler. When we want to have a new initial complete dump, we simply remove the svn_backup_delta.txt file.

The scripts can be a good starting point to create your own automated SVN dumping mechanism.

Windows backup_svn.bat Batch script. This script now expects the svn.exe and svnrdump.exe to be in the same folder as the script. It also requires a repos.txt and backup.properties files in the same folder. The output of the script will be stored in the same folder within the file backup_svn.log.

@echo off 
  TITLE %~n0 - see log %~n0.log 
  echo All details will be logged into %~n0.log 
  CALL :STARTBATCH >> %~dp0%~n0.log 2>&1 
  GOTO :EOF

:STARTBATCH     REM All properties are set in backup.properties     REM Variables that change in a for loop, should be marked with !VARIABLE! instead of %VARIABLE%     REM Necessary to use this, otherwise SET !VARIABLE! in for loop fails     setlocal enabledelayedexpansion     ::CONFIGURATION     ECHO %Date:~-4%%Date:~-7,2%%Date:~-10,2%-%Time% CHECKING FOR CHANGES AT %DATE%: %TIME%     REM Get properties from backup.properties and set as variables     FOR /F "tokens=1,2 delims==" %%A IN (%~dp0backup.properties) DO (     echo %Date:~-4%%Date:~-7,2%%Date:~-10,2%-%Time% %%A: %%B     set %%A=%%B     ) 
  :END_CONFIGURATION

REM Loop through all repos 
  for /f "delims=," %%r in (%~dp0repos.txt) DO ( 
  echo. 
  ECHO %Date:~-4%%Date:~-7,2%%Date:~-10,2%-%Time% Starting SVN backup 
  ECHO %Date:~-4%%Date:~-7,2%%Date:~-10,2%-%Time% Using following configuration: 
  SET REPO_URL=%REPO_BASE%/%%r 
  ECHO %Date:~-4%%Date:~-7,2%%Date:~-10,2%-%Time% -Repository URL: "%REPO_BASE%/%%r" 
  SET LAST_DELTA_FILE=%DUMP_BASE%\%%r\svn_backup_delta_%%r.txt 
  ECHO %Date:~-4%%Date:~-7,2%%Date:~-10,2%-%Time% -File to keep track of last revision backed up: !LAST_DELTA_FILE! 
  SET DUMP_FILE=%DUMP_BASE%\%%r 
  SET Today=%Date: =0% 
  SET Today=!Today:~-4!%Date:~-7,2%%Date:~-10,2% 
  SET Now=%Time: =0% 
  FOR /F "tokens=1,2 delims=:.," %%A IN ("!Now!") DO SET Now=%%A%%B

IF NOT EXIST "!DUMP_FILE!" mkdir "!DUMP_FILE!" 
  %~dp0svn --username %SVN_USERNAME% --password %SVN_PASS% --non-interactive --trust-server-cert --no-auth-cache info !REPO_URL! > %TEMP%\repoinfo.tmp

SET LAST_REV= 
  for /f "usebackq tokens=1,2 delims=: " %%i in (`find "Revision: " "%TEMP%\repoinfo.tmp"`) do (     SET LAST_REV=%%j     )     REM Check if !LAST_REV! retrieved of SVN is a number. If not, exit script.     echo !LAST_REV!| findstr /r "^[0-9]*$">nul || ( ECHO %Date:~-4%%Date:~-7,2%%Date:~-10,2%-%Time% ERROR: Last revision number is !LAST_REV! of %%r doesn't seem to be valid, aborting.     GOTO :END )     ECHO %Date:~-4%%Date:~-7,2%%Date:~-10,2%-%Time% Last revision: !LAST_REV!     ECHO %Date:~-4%%Date:~-7,2%%Date:~-10,2%-%Time% Last delta file: !LAST_DELTA_FILE!     REM Check if full backup exists, if true, do an incremental backup     IF NOT EXIST !LAST_DELTA_FILE! (         ECHO %Date:~-4%%Date:~-7,2%%Date:~-10,2%-%Time% Not delta file found, processing full export         call :FULL     ) ELSE (         ECHO %Date:~-4%%Date:~-7,2%%Date:~-10,2%-%Time% Delta found, processing delta         call :DELTA     ) 
  ) 
  ECHO %Date:~-4%%Date:~-7,2%%Date:~-10,2%-%Time% All repo's done, backup finalized 
  GOTO :END

REM Do an incremental backup 
  :DELTA     ECHO %Date:~-4%%Date:~-7,2%%Date:~-10,2%-%Time% DELTA     SET LAST_DELTA=     for /f "usebackq tokens=1,2 delims=: " %%i in (`find "Revision: " "!LAST_DELTA_FILE!"`) do (         SET LAST_DELTA=%%j     )     ECHO %Date:~-4%%Date:~-7,2%%Date:~-10,2%-%Time% Last delta:!LAST_DELTA!     ECHO %Date:~-4%%Date:~-7,2%%Date:~-10,2%-%Time% Last revision:!LAST_REV!     IF "!LAST_DELTA!"=="" GOTO FULL     IF "!LAST_REV!"=="" GOTO :END     IF "!LAST_DELTA!"=="!LAST_REV!" GOTO :END     REM Check if !LAST_DELTA! is a number. If not, exit script.         echo !LAST_DELTA!| findstr /r "^[1-9][0-9]*$">nul || ( ECHO ERROR: DELTA REVISION NUMBER IN !LAST_DELTA_FILE! OF %REPO_URL% DOESN'T SEEM TO BE A NUMBER... ABORTING BACKUP...     GOTO :END )     SET /A INCRE_LAST_DELTA=LAST_DELTA+1     SET DUMP_FILE_NAME=!Today!-!Now!-INCREMENTAL-R!INCRE_LAST_DELTA!-TO-R!LAST_REV!.dump     SET DUMP_FILE=!DUMP_FILE!\!DUMP_FILE_NAME!     ECHO %Date:~-4%%Date:~-7,2%%Date:~-10,2%-%Time% Creating dump file name: !DUMP_FILE_NAME!, !DUMP_FILE!     %~dp0svnrdump --username %SVN_USERNAME% --password %SVN_PASS% --non-interactive --trust-server-cert --no-auth-cache dump !REPO_URL! -r !INCRE_LAST_DELTA!:!LAST_REV! --incremental > !DUMP_FILE!     echo %Date:~-4%%Date:~-7,2%%Date:~-10,2%-%Time% %~dp0%ZIP% a -tzip  !DUMP_FILE!.zip !DUMP_FILE! -v%MAX_SIZE%     call %~dp0%ZIP% a -tzip  !DUMP_FILE!.zip !DUMP_FILE! -v%MAX_SIZE%     IF EXIST !DUMP_FILE!.zip DEL /F !DUMP_FILE!     IF EXIST !DUMP_FILE!.zip.* DEL /F !DUMP_FILE!     ECHO Revision: !LAST_REV! > !LAST_DELTA_FILE!     IF EXIST "!LAST_DELTA_FILE!" GOTO DELTA     GOTO :END

REM Do a full backup 
  :FULL     ECHO %Date:~-4%%Date:~-7,2%%Date:~-10,2%-%Time% FULL     ECHO FULL > !LAST_DELTA_FILE!     SET DUMP_FILE_NAME=!Today!-!Now!-INITIAL-R!LAST_REV!.dump     SET DUMP_FILE=!DUMP_FILE!\!DUMP_FILE_NAME!     ECHO %Date:~-4%%Date:~-7,2%%Date:~-10,2%-%Time% Dump file name: !DUMP_FILE_NAME!     ECHO %Date:~-4%%Date:~-7,2%%Date:~-10,2%-%Time% Dump file: !DUMP_FILE!     %~dp0svnrdump --username %SVN_USERNAME% --password %SVN_PASS% --non-interactive --trust-server-cert --no-auth-cache dump !REPO_URL! -r 0:!LAST_REV! > !DUMP_FILE!     echo %Date:~-4%%Date:~-7,2%%Date:~-10,2%-%Time% %~dp0%ZIP% a -tzip !DUMP_FILE!.zip !DUMP_FILE! -v%MAX_SIZE%     call %~dp0%ZIP% a -tzip !DUMP_FILE!.zip !DUMP_FILE! -v%MAX_SIZE%     IF EXIST !DUMP_FILE!.zip DEL /F !DUMP_FILE!     IF EXIST !DUMP_FILE!.zip.* DEL /F !DUMP_FILE!     ECHO Revision: !LAST_REV! > !LAST_DELTA_FILE!     IF EXIST "!LAST_DELTA_FILE!" GOTO DELTA     GOTO :END

:END     ECHO %Date:~-4%%Date:~-7,2%%Date:~-10,2%-%Time% END     GOTO :EOF


Linux svnbackup.sh Shell script:



#!/bin/sh
#SETUP
REPO_URL=https://url:8443/base/project
REPO_PATH=c:/repository/project
DUMP_DIR=/home/user/dumps
LAST_DELTA_FILE=svn_backup_delta.txt
#BEGINING OF THE SCRIPT ITSELF
date=$(date +%Y%m%d)
LOG=dump-$date.log
LAST_REV=$(svn info $REPO_URL |grep Revision |gawk ' { print $2}')
#Both files MUST exist, otherwise, wer'e going to init new "delting"
if [ -e $LAST_DELTA_FILE ] ; then
LAST_DELTA=$(cat $LAST_DELTA_FILE)
if [ $LAST_DELTA -ne $LAST_REV ] ; then
DUMP_FILE_NAME=$date-INCREMENTAL-R$((LAST_DELTA+1))-TO-R$LAST_REV.dump.gz
DUMP_FILE=$DUMP_DIR/$DUMP_FILE_NAME
svnadmin dump $REPO_PATH -r $((LAST_DELTA +1)):$LAST_REV --incremental --deltas 2>>$LOG |gzip -c > $DUMP_FILE
else
echo nothing to do : last revision number is same as last delta number, run it again later !
exit 0
fi
else
DUMP_FILE_NAME=$date-INITIAL-R$LAST_REV.dump.gz
DUMP_FILE=$DUMP_DIR/$DUMP_FILE_NAME
svnadmin dump $REPO_PATH 2>>$LOG | gzip -c > $DUMP_FILE
fi
#Is all ok ?
if [ $? -eq 0 ] ; then
echo -n $LAST_REV > $LAST_DELTA_FILE
else
echo "ERROR :" $date "Problem occured while saving dump" >>$LOG
fi

Update 02/07/2010: Script tested and working in Windows 7 (in combination with Visual SVN server). Make sure to add the bin folder of Visual SVN server to your 'path' system variable to make the 'svn' command available system wide. Run the script with right-click 'Run as administrator'.

 

Update 27/06/2016 on Windows script to use a backup_properties.txt, repo.txt files and validate the retrieved revision and delta numbers before executing the windows backup