I need to remove all files and folders from a particular parent folder except those which have .svn as the foldername and those which have *.svn-base as their filename.

Is there a Windows script that can do this for me?


Edit1

@Mizo - your solution returned the following while wiping out everything:

C:\temp>clean-all-except-svn.bat
File not found - *.svn_base
0 File(s) copied
0 File(s) copied

I had changed the clean.bat reference in the file itself.


Edit2

I fixed the typo as follows but still the same error

XCOPY /Q /Y *.svn-base __cltmp


C:\temp>clean-all-except-svn.bat
File not found - *.svn-base
0 File(s) copied
0 File(s) copied

Edit3

(a) It keeps prompting me

Confirm
The item at C:\temp\f\a\d has children and the Recurse parameter was not specified. If you continue,
all children will be removed with the item. Are you sure you want to continue?
[Y] Yes  [A] Yes to All  [N] No  [L] No to All  [S] Suspend  [?] Help (default is "Y"): 

(b) It displays THOUSANDS of messages like:

At line:1 char:86
+ get-childitem C:\temp\ -exclude *.svn-base,".svn" -recurse | foreach ($_) {remove-item <<<<  $_.fullname}
    + CategoryInfo          : PermissionDenied: (change_email.php.svn-base:FileInfo) [Remove-Item], IOException
    + FullyQualifiedErrorId : RemoveFileSystemItemUnAuthorizedAccess,Microsoft.PowerShell.Commands.RemoveItemCommand
Remove-Item : Cannot remove item C:\temp\admin\.svn\text-base\change_email_submit.php.svn-base: Not Enough permission to
 perform operation.
link|improve this question

71% accept rate
feedback

2 Answers

up vote 0 down vote accepted

Using Powershell:

get-childitem C:\folder\ -exclude *.svn-base,".svn" -recurse | foreach ($_) {remove-item $_.fullname}

Update: the code below should do what you wanted. It removes all files except *.svn-base AND does not delete ANY folders.

Some notes:gci is an alias of get-childitem, just to save some space. Added -Force parameter to remove-item since you seem to have some kind of permissions problem.

gci C:\folder\ -exclude *.svn-base -recurse | foreach ($_) {if(!$_.PSIsContainer){remove-item -Force $_.fullname}}
link|improve this answer
@Siim - I didnt realize I had powershell installed. I am trying this solution and it does seem to be working, but it seems to be taking longer than operating through the Windows explorer interface!! (see Edit3 as well) – matt74tm Jan 20 '11 at 23:12
@Siim - one problem (which I see I didnt specify the question correctly) is that your solution removes folders as well. I actually do not need any subfolders removed, whether they are .svn or otherwise. How can I modify this script to do that? – matt74tm Jan 21 '11 at 2:50
@matt_tm I've updated the code, pls check – Siim K Jan 21 '11 at 9:47
@Siim - I ended up using a variation of the concept - get-childitem C:\temp\ -exclude *.svn-base,".svn" -recurse | foreach ($_) { if ($_.Attributes -ne "Directory") {remove-item -Force $_.fullname} } – matt74tm Jan 21 '11 at 10:02
@Siim - thank you for introducing me to the concept of powershell! Wasn't aware of it before! – matt74tm Jan 21 '11 at 10:02
show 1 more comment
feedback

clean.bat:

@ECHO OFF
FOR /F "tokens=*" %%d IN ('dir /B /AD') DO IF NOT %%d==.svn RMDIR /S /Q %%d
MD __cltmp
XCOPY /Q /Y *.svn_base __cltmp
FOR /F "tokens=*" %%f IN ('dir /B /A-D') DO IF NOT %%f==clean.bat DEL /F /Q %%f
XCOPY /Q /Y __cltmp\* .
RMDIR /S /Q __cltmp
link|improve this answer
@mizo - please see edit1 – matt74tm Jan 20 '11 at 15:41
@mizo - please see edit2 as well – matt74tm Jan 20 '11 at 20:41
@mizo - Issue1: this doesn't get into subfolders at all! – matt74tm Jan 20 '11 at 21:40
@matt_tm, no, it wipes out all subfolders whose name is not .svn. Do you need it to be recursive? – mizo Jan 20 '11 at 21:42
Issue2: The first FOR /F statement matches .svn folders anyway... I want to exclude all data which is within a .svn folder! – matt74tm Jan 20 '11 at 21:42
show 1 more comment
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.