reset Dropbox conflicted copies with Windows PowerShell

My Dropbox account was unattached from my Dropbox directory on my netbook for a while, but I updated many text files on my netbook, so that when I re-installed Dropbox, it renamed my changes like this:

  • android (Rahula's conflicted copy 2014-10-22).txt – my good updated local files, considered conflicted by Dropbox
  • android.txt – the online version, downloaded by Dropbox

I wanted a quick script to find these conflicts, remove the downloaded versions, and return the conflicted filenames to their original state.  Here’s what I built:

First, I listed all of the changes compactly, so that I could visually scan through them:

gci -r -i '* (Rahula''s conflicted copy *'

– good, but this produces a big listing of PowerShell Objects, so I compacted that list to just the file locations, which is easier to scan visually:

gci -r -i '* (Rahula''s conflicted copy *' |%{echo $_.fullname}

Next I detoured briefly to test a generalised regular expression on an example name:

'android (Rahula''s conflicted copy 2014-10-22).txt' -replace ' \(Rahula''s conflicted copy ....-..-..\)',''

– that works, but it’s more precise to focus in on a digital date:

'android (Rahula''s conflicted copy 2014-10-22).txt' -replace ' \(Rahula''s conflicted copy \d{4}-\d\d-\d\d\)',''

That regular expression works, so next I listed the conflicted files stripped back to their original locations:

gci -r -i '* (Rahula''s conflicted copy *' |%{echo ($_.fullname -replace ' \(Rahula''s conflicted copy \d{4}-\d\d-\d\d\)','')}

Finally, ready to go, I forced the conflicted files back to their original locations:

gci -r -i '* (Rahula''s conflicted copy *' |%{mi $_.fullname ($_.fullname -replace ' \(Rahula''s conflicted copy \d{4}-\d\d-\d\d\)','') -force}

– and that one-liner now contains the PowerShell commands that I need if this happens again.


 

Diacritics note

Windows PowerShell also handles diacritics well, so when I wanted to remove a diacritic from several filepaths, it was easy:

1. Check the effect of the replace:

gci -r -i '*Céline*' |%{echo ($_.fullname -replace 'é','e')}

2. Do it, with <code>Move-Item</code>:

gci -r -i '*Céline*' |%{mi $_.fullname ($_.fullname -replace 'é','e')}

– in the working directory, all filepaths containing Céline now have that é replaced with a less troublesome (for Git Bash on MS Windows) e.


 

Backtick note

1. List filepaths recursively in the current directory containing backticks:

gci -r -i '*``*' |%{echo $_.fullname}

2. simulate a replacement:

gci -r -i '*``*' |%{echo ($_.fullname -replace '`','')}

3. do the replacement:

gci -r -i '*``*' |%{mi $_.fullname ($_.fullname -replace '`','')}

Share Button