Search code examples
pathwhitespacersyncspaces

Using white spaces in an rsync file path excecuted in a batch file


So I'm using rsync to back up some commonly used directories, using a batch script and windows scheduler as seen here:

rsync.bat

@echo off

C:
chdir C:\Cygwin\bin

bash --login -i -c %1

I'm using Windows Scheduler to run this batch script on regular intervals, which works perfectly - until I add spaces into the file paths. Here's an example of the target paths I use on the batch file shortcuts:

Target: C:\Cygwin\scripts\rsync.bat "rsync -vruh /cygdrive/t/uploads /cygdrive/d/Backups"

That works perfectly. Here's the one with whitespaces:

Target: C:\Cygwin\scripts\rsync.bat "rsync -vruh '/cygdrive/c/Users/username/My Documents' /cygdrive/d/Backups"

Note the single quotes on the first path with whitespaces. I can't for the life of me figure out why this won't work - I've tried backslash escaping the whitespaces, using the -s switch, which some research solved this problem in the past, but alas, that didn't work either. Anyone know how to get this to work?


Solution

  • The problem of spaces is in the argv processing done to interpret the command line. As with any other unix application you have to escape spaces in some way on the command line or they will be used to separate arguments.

    For example:

    rsync -av host:'"a long filename"' /tmp/
    rsync -av host:'a\ long\ filename' /tmp/
    rsync -av host:a\\\ long\\\ filename /tmp/
    

    Or use a '?' in place of a space as long as there are no other matching filenames than the one with spaces (since '?' matches any character):

    rsync -av host:a?long?filename /tmp/
    

    As long as you know that the remote filenames on the command line are interpreted by the remote shell then it all works fine.

    http://rsync.samba.org/FAQ.html#9