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

this script opens a file called "folders.txt" which contains foldername each in a separate line , and then passes by these folders one by one in a loop and takes ownership of these folders and subfolders and deletes them

Python, 18 lines
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
'''
Created on Jun 25, 2009
this script reads from a txt file having folder names , where each folder name is a  new line
and take owner ship of the folder , then deletes the folder with all its sub directories and files
@author: Mohamed Garrana
'''
import os
fv=open("folders.txt","r")
for line in fv:
    folder=line.strip()
    print folder
    takeown="takeown /f %s /r /d y" %(folder,)
    deletefolder="rd /s /q %s" %(folder,)
    try:
        tk=os.popen(takeown)
        dl=os.popen(deletefolder)
    except:
        pass

3 comments

Gabriel Genellina 12 years, 4 months ago  # | flag

What if the folder name contains a space? Why os.popen and not os.system, if you're not interested in the output? Why not subprocess? Why a bare except clause - what if the user presses Ctrl-C? Why the hardcoded name "folders.txt"?

Looks too risky for me to execute...

Ivan Koblik 12 years, 4 months ago  # | flag

I think for such a simple task you don't need Python:

FOR /F "usebackq delims=''" %G IN (folders.txt) DO takeown /r /d y /f "%G"

To delete all the sub folders and files, use this:

takeown /r /d y /f *
cd ..
rd /s /q [the_folder_name]

Anyway, thanks for sharing! I used Cygwin's chown before, but takeown seems to work better.

mgarrana Garrana (author) 11 years, 9 months ago  # | flag

hello sorry for the late reply actually i customized this script for a certian purpose , it wasn't meant to work in any situation you are 100% right , about the hardcoded name ,you can use sys.argv or something and pass the file as an argment , as i said this was designed for just a specific purpose and i thought of just sharing the idea by posting it , ivan , i agree ....now i 'd most probably use powershell for such task