Welcome, guest | Sign In | My Account | Store | Cart

Remove non-empty directories leaving specified directories.

Python, 18 lines
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
# remove all folders except admin*
# author
# version: 0.1
# date   : October 2005
#----------------------------------------------------------

import os, string, shutil

SETPATH = "c:\Documents and Settings"

files = os.listdir( SETPATH )

for x in files:
	if not x.lower().startswith( 'admin'):
		fullpath = os.path.join( SETPATH, x )
	        if os.path.isdir( fullpath ):
			shutil.rmtree( fullpath )
			print "Removed: ", fullpath, "\n"

In our Windows XP environment, with the policy settings in place, if the documents and settings file exists for a user, they are unable to login to the computer. I developed this script as a way to delete all folders other than the local administrator's and the network adminisrator's every time the computer boots.