Sign in to follow this  
Followers 0
aleph01

Difficulties deleting entire contents of a directory

9 posts in this topic

Hi forum!

The library where I work redirects all public My Documents and items to be burned to CD into a directory, C:CD.  We've been using a .vbs script to delete the contents successfully until Windows 8.  I need a script to simply delete the entire contents, files and folders within that CD directory.

FileDelete ("C:CD") deletes the files but leaves any subfolders a patron may have created.

I've tried

DirRemove ("C:\CD\", 1)
Sleep (1000)
DirCreate ("C:\CD\")

but the CD directory either isn't created, or is not accessible due to our patron profile restrictions.  This is true even if I use another script to run the intended script as an administrator (using RunAs).

I created another directory in which I created a CD directory to use in this way

DirCopy ("C:cdSourceCD", "C:CD", 1)

but I still get the same results.

Am I overlooking the ideal command for this or is there a better approach?

Thanks for any input.

_aleph_


Meds.  They're not just for breakfast anymore. :'(

Share this post


Link to post
Share on other sites



Try adding #RequireAdmin to the script, if the user is an admin you need the correct permissions to delete a folder from the root of the C: drive.

Easier way to do it, don't use the root of the C: drive to store the folder any longer, since Vista+ has restricted access to deleting/adding folders in that location it's no longer a good idea to do this.


If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

I would use first do a FileDelete(<directory> & "*.*") to get rid of all the files. Then I would do a _FileListToArrayRec, create an array of the folders, and delete wth recursion.

Edit: Missed the root of C: reference. If that is the case, then I would also add #RequireAdmin as BrewManNH suggests.

Edited by JLogan3o13

√-1 2^3 ∑ π, and it was delicious!

Share this post


Link to post
Share on other sites

I was unaware of restrictions on the root of C:.  I actually do have #RequireAdmin, it seems my copy and past skills are weak today >_< .  I guess we'll go with FileDelete until I get a chance to check out _FileListToArrayRec.  Thanks for the help.  I may be back if _FileListToArrayRec boggles my mind - I'm not the sharpest at working with arrays.

Thanks again,

_aleph_


Meds.  They're not just for breakfast anymore. :'(

Share this post


Link to post
Share on other sites

JLogan3o13,

I had a little trouble figuring how to reference the array elements, and I know it probably doesn't look like much to you, but I'm very happy with myself for figuring this out.  Thanks for the  _FileListToArrayRec tip.

#include <Array.au3>
#include <File.au3>
#RequireAdmin

FileDelete ("C:\CD\*.*")
Sleep (1000)

Local $aArray = _FileListToArrayRec("C:\CD\", "*", $FLTAR_FOLDERS, $FLTAR_RECUR, $FLTAR_SORT)
;    _ArrayDisplay($aArray, "Sorted tree")

If @error Then Exit

For $FLDR = 1 to $aArray[0]
    DirRemove ("C:\CD\" & $aArray[$FLDR], 1)
Next

Meds.  They're not just for breakfast anymore. :'(

Share this post


Link to post
Share on other sites

Sorry,

I spoke too soon.  While I am happy to have worked out how to reference the array, this works wonderfully on my system, but not on the public computers - the subfolders remain.  It must be a permissions thing.  Can someone think of any way I might be able to get this to work where I need it (on the public machines?)

Thanks,

_aleph_


Meds.  They're not just for breakfast anymore. :'(

Share this post


Link to post
Share on other sites

What you have looks good. One suggestion I would offer, if you set the last parameter ($iReturnPath) to $FLTAR_FULLPATH, you can do something like this in your For loop:

For $i = 1 to $aArray[0]
    DirRemove ($aArray[$i], 1)
Next

Other than that, it looks great.


√-1 2^3 ∑ π, and it was delicious!

Share this post


Link to post
Share on other sites

If you need to run it as a specific account, look at RunAs in the help file. You may be forced to do two scripts, one to run the delete, and one to run the first as your local administrator account.


√-1 2^3 ∑ π, and it was delicious!

Share this post


Link to post
Share on other sites

Sorry to flip-flop on ya'll so much.  I went back to using a RunAs script to start my Delete script and it's now working on the patron machines.  So, our computer reservation software couldn't call .vbs scripts, so we had it call a .bat file to start the .vbs.  Now we're in a mixed  environment, so calling the .bat file isn't going away soon, as it works great on the XP systems.  But on the Windows 8 systems, we have a .bat file calling an Autoit.exe calling another Autoit.exe.  This is how documentation becomes so important.

Thanks, all.

_aleph_


Meds.  They're not just for breakfast anymore. :'(

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0