Jump to content

Using FileDelete but need it to act on many folder


Recommended Posts

I have a folder, Master, which contains about 50 subfolders, Subfolder 1-50, and need to delete the contents of the subfolders but leave them in the master folder, meaning not delete the whole subfolder, just trash the contents. If I put a FileDelete in each subfolder, I can successfully empty out the contents but it's a pain to go into each of the subfolders individually and do the FileDelete, I might as well just do a Ctrl+A, ya know. How can I put one FileDelete in the Master folder and have it go into each subfolder and delete the contents? Would I have to put in a FileDelete and path for each of the 50 subfolders?

FileDelete("H:\Master\Subfolder1\*.doc")

FileDelete("H:\Master\Subfolder2\*.doc")

ect.??

Thanks for any suggestions, they are appreciated.

Kyle

Link to comment
Share on other sites

Use FileFindFirstFile() and FileFindNextFile() to cycle through the file(s) in your master folder, and use FileGetAttributes() to see which ones are directories, and delete them.

Link to comment
Share on other sites

here ya go

#Region --- CodeWizard generated code Start ---
;MsgBox features: Title=Yes, Text=Yes, Buttons=OK and Cancel, Icon=Info
If Not IsDeclared("iMsgBoxAnswer") Then Dim $iMsgBoxAnswer
$iMsgBoxAnswer = MsgBox(65,"Master Cleaner","Press  *OK*  " & @CRLF & @CRLF & "to remove all *.doc files in the sub-directories of Master  " & @CRLF)
Select
    Case $iMsgBoxAnswer = 1;OK
        Call("Delete_Unused")

   Case $iMsgBoxAnswer = 2;Cancel
        Exit
EndSelect
#EndRegion --- CodeWizard generated code End ---


Func Delete_Unused()
        
    RunWait(@ComSpec & ' /c ' & 'dir "' & 'H:\Master\*.doc' & '" /a :h /b /s' & ' > "' & @TempDir & '\dir.txt"', '', @SW_HIDE)
    Sleep(2000)
    
    
    $hFile = FileOpen(@TempDir & "\dir.txt", 0)
    
; Check if file opened for reading OK
    If $hFile = -1 Then
        MsgBox(0, "Error", "Unable to open file.")
        Return
    EndIf
    
; Read in lines of text until the EOF is reached
    While 1
        $sLine = FileReadLine($hFile)
        If @error = -1 Then ExitLoop
        If $sLine <> "" Then
            FileDelete($sLine)
        EndIf
    WEnd
    FileClose($hFile)
EndFunc

*********** NOT TESTED ******************

I don't have an "H" drive or master folder

enjoy!

8)

You could also check-out the XPClean Menu link below... it has lots of cleaners!

Edited by Valuater

NEWHeader1.png

Link to comment
Share on other sites

@Valuater:

Why do it half DOS and half AutoIt? Why not just one or the other?

FOR /D %%A in (H:\Master\*) do DEL %%~fA\*.doc
Link to comment
Share on other sites

How can I put one FileDelete in the Master folder and have it go into each subfolder and delete the contents? Would I have to put in a FileDelete and path for each of the 50 subfolders?

<{POST_SNAPBACK}>

Could you simply delete the subfolder and then recreate it? This would do what you seem to have described.
Link to comment
Share on other sites

@Valuater:

Why do it half DOS and half AutoIt?  Why not just one or the other?

FOR /D %%A in (H:\Master\*) do DEL %%~fA\*.doc

<{POST_SNAPBACK}>

@blindwig

Nice code....

I understand the direction, but writing it is beyond my current capabilities as an Autoit hobbiest..

8)

good job!

NEWHeader1.png

Link to comment
Share on other sites

Blindwig's code is for a batch file. Throw it in a text file, save it as Cleanup.bat and double-click it or schedule it. Check "for /?" in a command prompt window if you want to know what the /D and %%~f stuff is. You don't need Autoit to do what you want... unless you want to count the number of files and show a progress bar or something ridiculous like that.

@LxP: Deleting the subfolders would destroy any custom permissions... which be fine if they just inherit the master folder's.

Link to comment
Share on other sites

... unless you want to count the number of files and show a progress bar or something ridiculous like that.

<{POST_SNAPBACK}>

Thats's exactly what my program *XPClean Menu* below does... let the user know how many files are being deleted. But i don't think it is "rediculous".

8)

NEWHeader1.png

Link to comment
Share on other sites

I understand the direction, but writing it is beyond my current capabilities as an Autoit hobbiest..

What I am saying is why do you wrap a DOS call with an AutoIt script? If you want to write an AutoIt script, why not just write the whole thing in AutoIt? That's why I suggested FileFindFirst() etc in my first post.

But then if you were suggesting using DOS commands, I was wondering why you wouldn't just write the whole thing in DOS, instead of wrapping DOS in AutoIt. So I took what you wrote and converted it to pure DOS:

dir "H:\Master\*.doc" /a :h /b /s > "%TEMP%\dir.txt"
FOR /F "Tokens=*" %%A in ("%TEMP%\dir.txt") do DEL "%%~A"

But then I figured why bother writing the dir out to a text file and parse it back in? Why not just parse the DIR command directly?

FOR /F "Tokens=*" %%A in ('dir "H:\Master\*.doc" /a :h /b /s > "%TEMP%\dir.txt"') do DEL "%%~A"

But then I thought why bother wrapping the DIR command, why not just use the FOR command for what it was originally intended for. Also I noticed that your syntax doesn't match the original posters requirements, so I change it to match:

FOR /D %%A in (H:\Master\*) do DEL %%~fA\*.doc

And that's how I ended up with that line.

Link to comment
Share on other sites

Thats's exactly what my program *XPClean Menu* below does... let the user know how many files are being deleted. But i don't think it is "rediculous".

I don't think showing a progress is ridiculous, but if it's going to be scheduled or you just want the files gone silently it's easier just to use a batch file. Believe me, I see the value in progress bars and other eye candy for the user and I use them in most of my scripts.

(By the way, I changed my username from rmorrow) :whistle:

Edited by c0deWorm

My UDFs: ExitCodes

Link to comment
Share on other sites

What I am saying is why do you wrap a DOS call with an AutoIt script?  If you want to write an AutoIt script, why not just write the whole thing in AutoIt? 

<{POST_SNAPBACK}>

Actually when i was writting XPClean Menu, i tried many different find file scripts in autoit... probably more than 10. I found that searching the entire hard drive with a dos cmd was considerably faster. My script is not time tested, but, the wait time for the find file was tremendiously slower.

and thats why i seem to mix dos and Autoit

8)

NEWHeader1.png

Link to comment
Share on other sites

@snooz_bar

Wow, this is all great help, thanks a lot guys. This looks the shortest and simplest

FOR /D %%A in (H:\Master\*) do DEL %%~fA\*.doc
can you explain what each part is doing? So save it as a .bat and schedule it?
Lemme break this down for ya:

"FOR" is a built-in command-line utility for looping, it supports many options, all of which can be seen by opening a command prompt and typing "for /?". A command prompt can be opened by clicking Start, Run, and typing "cmd".

"/D" tells "FOR" that we want to loop through a list of directories in the "IN" clause found later.

"%%A" will be our first variable for the command and can be modified (see "for /?").

"in (H:\Master\*)" is the dataset. We want to search in the path H:\Master\ for files named "*" (all files), but we used the "/D" option, so we only want directories, not files.

"do" means everything after this is what I want done for each directory found.

"DEL" is a separate built-in command to delete files (see "del /?").

"%%~fA" is our variable %%A, modified by the ~f part. This modification means to expand the entry to a fully-qualified path name. So if our directory name is "blah" it would expand to "H:\Master\blah".

"\*.doc" will be added to the path and passed to the DEL command for a command of "DEL H:\Master\blah\abc.doc" then "DEL H:\Master\blah\def.doc", etc.

Broken down enough for you? :whistle:

In the command prompt window, try "cmd /?", then look for the command extensions part. There is a list of internal, or built-in, commands... then do a "/?" for each of them. Now combine them to do what you want.

My UDFs: ExitCodes

Link to comment
Share on other sites

I found that searching the entire hard drive with a dos cmd was considerably faster.

I've seen many, many replies on this forum over the years that confirm this, and it's the way I almost always do it. Part of that is because I am much more familiar with DOS commands than I am at recursing a file structure using AutoIt commands. I will have to focus some time into correcting that.

I wonder if you've ever actually timed something like this using timers. I'd like to see metrics on a complicated file search involving several hundred files and folders using the FOR command, the DIR command, AutoIt's built-in commands, and WMI objects. I suspect that the DOS commands would beat the others significantly because it's been a part of Windows for much, much longer than the others and has been tweaked specifically for such operations.

I usually end up using DOS commands and robocopy for moving large numbers of files around a network because I've seen significant speed differences compared to Explorer. I've even seen quite a difference in some monitoring scripts I've made where a few DOS commands are quicker than using VBS and objects to perform the work. The scripts basically call some program and parse the output, and I've become accustomed to working with that type of data in DOS to the point that a few loops and FOR commands outperforms VBS objects. Most people can't read my batch files because of the multiple CALLs, but they should mind their own business anyway, right?

My UDFs: ExitCodes

Link to comment
Share on other sites

So save it as a .bat and schedule it?

Sorry, I forgot to elaborate on this.

Create a new plain-text file, copy & paste the code into it, save it as "wipemaster.bat" or whatever you want.

Then go to your Control Panel, Scheduled Tasks, and click on Add Scheduled Tasks. When it asks you for the task, browse to your batch file, then go through the rest of the dialogs to configure when and how to run it.

By the way, if you're using NT/2000/XP/2003, make sure you run it as a user who has permissions to delete those files. By default, it uses your ID.

My UDFs: ExitCodes

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...