Skip to content

Instantly share code, notes, and snippets.

@MaxXSoft
Created April 2, 2022 07:31
Show Gist options
  • Save MaxXSoft/fd4ef76784f94e60c5ba612ebdf27358 to your computer and use it in GitHub Desktop.
Save MaxXSoft/fd4ef76784f94e60c5ba612ebdf27358 to your computer and use it in GitHub Desktop.
Script for running `rsync` on Windows, based on the cwRsync.
' Helper script for running `rsync` from the command line.
' Author: MaxXing, 2022.
'
' You can get `rsync` for Windows on https://www.itefix.net/cwrsync.
' Then you should place this script in the same folder as the `rsync.exe` file.
'
' Usage:
' rsync.vbs [options] [source] [destination]
' returns true if the given path is valid
Function IsPathValid(path)
Set re = New RegExp
re.Pattern = "^[^\\][\x20-\x7E]+$"
IsPathValid = re.Test(path)
End Function
' converts a path to a Cygwin path
Function ConvertPathToCygwin(path)
If Not IsPathValid(path) Then
WScript.Echo "Invalid path: " & path
WScript.Quit 1
End If
path = Replace(Replace(path, ":\", "/"), "\", "/")
ConvertPathToCygwin = "/cygdrive/" & path
End Function
' get & check the script directory
Set fso = CreateObject("Scripting.FileSystemObject")
scriptDir = fso.GetParentFolderName(WScript.ScriptFullName)
If Not IsPathValid(scriptDir) Then
WScript.Echo "Invalid script directory."
WScript.Quit 1
End If
' get the path to the rsync executable and ssh executable
rsyncPath = fso.BuildPath(scriptDir, "rsync.exe")
sshPath = fso.BuildPath(scriptDir, "ssh.exe")
If Not fso.FileExists(rsyncPath) Or Not fso.FileExists(sshPath) Then
WScript.Echo "The rsync/ssh executable does not exist."
WScript.Quit 1
End If
' generate command line
command = rsyncPath & " -e '" & sshPath & "'"
handledSrc = False
For Each arg In WScript.Arguments
If Left(arg, 1) <> "-" And Not handledSrc Then
handledSrc = True
path = fso.GetAbsolutePathName(arg)
command = command & " '" & ConvertPathToCygwin(path) & "'"
Else
command = command & " " & arg
End If
Next
' run the command line
Set ws = CreateObject("WScript.Shell")
ws.Run "powershell.exe -command """ & command & "; pause"""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment