import os
import re
def removeall(path):
if not os.path.isdir(path):
os.remove(path)
return
files=os.listdir(path)
for x in files:
fullpath=os.path.join(path, x)
if os.path.isfile(fullpath):
os.remove(fullpath)
elif os.path.isdir(fullpath):
removeall(fullpath)
os.rmdir(path)
unversionedRex = re.compile('^ ?[\?ID] *[1-9 ]*[a-zA-Z]* +(.*)')
for l in os.popen('svn status --no-ignore -v').readlines():
match = unversionedRex.match(l)
if match: removeall(match.group(1))
I couldn't get any of the above to work without additional dependencies I didn't want to have to add to my automated build system on win32. So I put together the following Ant commands - note these require the Ant-contrib JAR to be installed in (I was using version 1.0b3, the latest, with Ant 1.7.0).
If you are using tortoise svn there is a hidden command to do this. Hold shift whilst right clicking on a folder to launch the context menu in windows explorer. You will get a "Delete Unversioned Items" command.
If you don't want to write any code, svn2.exe from 你好 does this, also there's 一篇文章 on how it's implemented. Deleted folders and files are put in the recycle bin.
The options are described in the TortoiseSVN help for /command:cleanup:
使用/nui 可以防止弹出结果对话框
要么告诉清理正在完成,要么显示错误
message). /noprogressui also disables the progress dialog. /nodlg
disables showing the cleanup dialog where the user can choose what
应该在清理过程中完成。可用的操作可以是
在状态清理/清理/恢复选项中指定,
/解除版本控制(delunversion)、/忽略、/刷新 shell 和/外部。
FOR /F "tokens=1* delims= " %%G IN ('svn st %~1 ^| findstr "^?"') DO del /s /f /q "%%H"
FOR /F "tokens=1* delims= " %%G IN ('svn st %~1 ^| findstr "^?"') DO rd /s /q "%%H"
You must use two % marks in front of G and H
切换顺序: 首先删除所有文件,然后删除所有目录
(可选:)代替 %~1可以使用任何目录名,我在 bat 文件中使用这个函数,所以 %~1是第一个输入参数