Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

110.1 Delete Empty Folders


  • Please log in to reply
12 replies to this topic
Decarlo110
  • Members
  • 303 posts
  • Last active: Feb 12 2006 02:15 AM
  • Joined: 15 Dec 2004
This script originated because of the following thread started by Enrica:
http://www.autohotke...opic.php?t=3699

It searches for, lists, and deletes upon approval, all empty folders within a user-specified location. Folders do not go to the Recycle Bin.

; DeleteEmptyFolders Utility 1.3.1
; by Decarlo

!#e::	; comment this line when using it for command line

; added support for commandline parameters and an auto-delete mode with logfile.
; with command line parameters, selectionEF corresponds to the first, and targetFolderEF to the second.
; note: sending function parameters thru a hotkey achieves a similar mode.
; streamlined the output code.
; added handling of Folders which contain only Empty Subfolders.***
; progress window: enabled dragging, added option to remove window (for faster run)
; detects if target folder doesnt exist, and handles if it is itself empty
; fixed initialization.

if 2 <>					; command-line parameters received
	DeleteEmptyFolders()
else
{
	selectionEF = ;1e		; input values here to bypass the Input prompt.
	targetFolderEF = C:\		; leave at least 1 param blank to show the Input prompt.

	DeleteEmptyFolders()
}
return


DeleteEmptyFolders()	; use of local function vars avoids possible naming conflicts with common variables of other routines

{

SetFormat, Float, 0.3 

global 0, 1, 2				; recognize command-line parameters
global selectionEF, targetFolderEF

emptytargetFolderEF=
backSlashCheck=
Splash=

possibleEmptySubFoldersFolders=
countFoldersEmptySubFolders=0
emptySubfoldersFolderList=
countTopmostEmptyFolders=0
topmostEmptyFoldersList=

countFilesFolders=
countFolders=
countEmptyFolders=
emptyFolderList=
FileFolderList=
FolderList=

if 2 <>
{
	selectionEF = %1%
	targetFolderEF = %2%
}
else if ( selectionEF AND targetFolderEF )	; function parameters received (from this same script)
	1 := 1	; this construct avoids the next Else (Input prompt) if the necessary values exist.
else
{
	InputBox, selectionEF, DeleteEmptyFolders Utility, Please select:`n1. list/delete empty folders`n2. list/delete empty folders and list all folders`n3. list/delete empty folders and list all folders and files`nInclude A (capital) to auto-delete empty folders (caution). no progress window.`nInclude w to run without progress window (better speed).`nInclude e to also find folders containing only empty subfolders.`nThese parameters also apply to command-line mode.,, 700, 240,,,,, 1e
	If (ErrorLevel<>0 OR selectionEF="")
		EXIT
	
	DEF_1 = Enter the complete path of the target folder.`nAll its empty folders will be listed and a prompt shown for approval to delete.`nFor long paths`, it is better to copy-paste from the AddressBar.
	ifInString, selectionEF, 1
		InputBox, targetFolderEF, DeleteEmptyFolders Utility, %DEF_1%,, 700, 170,,,,, C:\
	else ifInString, selectionEF, 2
		InputBox, targetFolderEF, DeleteEmptyFolders Utility, %DEF_1%`nAll subfolders of the target folder will also be listed.`n,, 700, 170,,,,, C:\
	else ifInString, selectionEF, 3
		InputBox, targetFolderEF, DeleteEmptyFolders Utility, %DEF_1%`nAll files and folders of the target folder will also be listed.`n,, 700, 170,,,,, C:\
	If (ErrorLevel<>0 OR targetFolderEF="")
		EXIT
}

StringCaseSense, on 
ifInString, selectionEF, A
{
	StringReplace, selectionEF, selectionEF, A, autoW
	StringCaseSense, off
}
if selectionEF not contains 1,2,3		; verify parameters
{
	ifInString, selectionEF, auto
	{
		GoSub CreateLogName
		FileAppend, selectionEF parameter did not contain 1, 2, or 3. Exiting., %logName%
	}
	else
		msgbox,,, invalid parameter, .7
	EXIT
}

ifNotInString, targetFolderEF, C:\
{
	ifInString, selectionEF, auto
	{
		GoSub CreateLogName
		FileAppend, %targetFolderEF%   is an invalid targetFolderEF parameter. Exiting., %logName%
	}
	else
		msgbox,,, invalid parameter, .7
	EXIT
}
ifInString, selectionEF, 1
{
	StringReplace, selectionEF, selectionEF, 2 
	StringReplace, selectionEF, selectionEF, 3
}
else ifInString, selectionEF, 2
	StringReplace, selectionEF, selectionEF, 3
ifNotInString, selectionEF, w
	Splash=1	; only built-in cmds can be used on same line above
StringRight, backSlashCheck, targetFolderEF, 1
if backSlashCheck=\
	StringTrimRight, targetFolderEF, targetFolderEF, 1
targetFolderEFLocation:=targetFolderEF . "\"
targetFolderEF:=targetFolderEF . "\*"
sleep 100	; because splash doesnt appear sometimes
startTimeEmptyFolders=%A_TickCount%

IfExist, %targetFolderEFLocation%
	Loop, %targetFolderEF%, 1, 1
	{
		if A_index=1
		{
			emptytargetFolderEF=N
			if Splash=1
			{
				SplashTextOn, 500, 100, DeleteEmptyFolders Utility, Text
				WinSet, AlwaysOnTop, off, DeleteEmptyFolders
				WinSet, Enable,, DeleteEmptyFolders
			}
			ifInString, selectionEF, 3
				GoSub DEF_SearchAll
			else
				GoSub DEF_SearchFolders

			ifInString, selectionEF, e
				GoSub DEF_FindEmptySubfoldersFolders

			BREAK
		}		
	}
else
{
	ifInString, selectionEF, auto
	{
		GoSub CreateLogName
		FileAppend, %targetFolderEFLocation%   does not exist. Exiting., %logName%
	}
	else
		msgbox, %targetFolderEFLocation%`n`ndoes not exist.
	EXIT
}

endTimeEmptyFolders=%A_TickCount%
timeTaken:=(endTimeEmptyFolders-startTimeEmptyFolders)/1000.
SplashTextOff

ifInString, selectionEF, auto
	GoSub DEF_Results_NoUserPrompt
else
	GoSub DEF_Results

RETURN			; return from the function


DEF_FindEmptySubfoldersFolders:

;msgbox,,, searching only-empty-subfolders folders, .7

Loop, Parse, emptyFolderList, `n, `r		; use previous results: generate checklist
{
	tracePathEmptyFolder = %A_LoopField%
	Loop
	{
		if A_LoopField =
			BREAK
		StringLen, DEF_Length_1, tracePathEmptyFolder
		StringGetPos, DEF_Loc1, tracePathEmptyFolder, \, R
		if DEF_Loc1 = 2			; path cannot be shorter than C:\
			BREAK
		StringTrimRight, tracePathEmptyFolder, tracePathEmptyFolder, % DEF_Length_1 - DEF_Loc1
		ifInString, tracePathEmptyFolder, %targetFolderEFLocation%	; item must be in target folder
		{
			ifNotInString, possibleEmptySubFoldersFolders, %tracePathEmptyFolder%`r`n
			{
				possibleEmptySubFoldersFolders = %possibleEmptySubFoldersFolders%%tracePathEmptyFolder%`r`n
				ControlSetText, Static1, %tracePathEmptyFolder%, DeleteEmptyFolders Utility
			}
		}
		else BREAK
	}
}
Sort, possibleEmptySubFoldersFolders, U

Loop, parse, possibleEmptySubFoldersFolders, `n, `r	; find all folders containing only empty subfolders
{
	flagEmptyFolder =			; reset for each loop field
	num=%A_Index%
	Loop, %A_LoopField%\*, 0, 1		; check files only, not folders
	{
		ControlSetText, Static1, %A_LoopFileFullPath%, DeleteEmptyFolders Utility
		if A_Index = 1
		{
			flagEmptyFolder = N
			BREAK
		}
	}
	if flagEmptyFolder=
	{
		emptySubfoldersFolderList=%emptySubfoldersFolderList%%A_LoopField%`r`n
		countFoldersEmptySubFolders+=1
	}
}

; narrow the list: find topmost folders containing only empty subfolders (they contain all the items)

Loop, Parse, emptySubfoldersFolderList, `n, `r		; use previous results
{
	if A_LoopField =
		BREAK
	tracePathEmptyFolder = %A_LoopField%
	ControlSetText, Static1, %tracePathEmptyFolder%, DeleteEmptyFolders Utility

	StringLen, DEF_Length_1, tracePathEmptyFolder
	StringGetPos, DEF_Loc1, tracePathEmptyFolder, \, R
	StringTrimRight, tracePathEmptyFolder, tracePathEmptyFolder, % DEF_Length_1 - DEF_Loc1
	ControlSetText, Static1, %tracePathEmptyFolder%, DeleteEmptyFolders Utility

	ifNotInString, emptySubfoldersFolderList, %tracePathEmptyFolder%`r`n
	{
		topmostEmptyFoldersList = %topmostEmptyFoldersList%%A_LoopField%`r`n
		countTopmostEmptyFolders+=1
	}
}

Sort, emptySubfoldersFolderList
Sort, topmostEmptyFoldersList, U

return


DEF_SearchAll:

Loop, %targetFolderEF%, 1, 1
{
	FileFolderList=%FileFolderList%%A_LoopFileFullPath%`r`n
	countFilesFolders+=1
	countFolders+=1
	flagEmptyFolder=
	FileGetAttrib, Attribs, %A_LoopFileFullPath%
	ifInString, Attribs, D
	{
		Loop, %A_LoopFileFullPath%\*, 1
		{
			ControlSetText, Static1, %A_LoopFileFullPath%, DeleteEmptyFolders Utility
			if A_index=1
			{
				flagEmptyFolder=N
				BREAK
			}
		}
		if flagEmptyFolder=
		{
			emptyFolderList=%emptyFolderList%%A_LoopFileFullPath%`r`n
			countEmptyFolders+=1
		}
	}
	else countFolders-=1
}

return


DEF_SearchFolders:

Loop, %targetFolderEF%, 2, 1
{
	ifInString, selectionEF, 2
		FolderList=%FolderList%%A_LoopFileFullPath%`r`n
	countFolders+=1
	flagEmptyFolder=
	Loop, %A_LoopFileFullPath%\*, 1
	{
		ControlSetText, Static1, %A_LoopFileFullPath%, DeleteEmptyFolders Utility
		if A_index=1
		{
			flagEmptyFolder=N
			BREAK
		}
	}
	if flagEmptyFolder=
	{
		emptyFolderList=%emptyFolderList%%A_LoopFileFullPath%`r`n
		countEmptyFolders+=1
	}
	; integrating the empty-subfolder search into this routine takes longer (almost 2x).
}
Sort, emptySubfoldersFolderList

return


DEF_EditorMsgStrings:

ifInString, selectionEF, e	; e = option to also find folders containing empty subfolders
{
	DEF_EmpSub1 = %countFoldersEmptySubFolders% folders containing only empty subfolders also found.`r`n`r`n%countTopmostEmptyFolders% topmost folders containing only empty subfolders.`r`n`r`n
	DEF_EmpSub2 = %countFoldersEmptySubFolders% Folders containing only empty subfolders:`r`n`r`n%emptySubfoldersFolderList%`r`n`r`n`r`n%countTopmostEmptyFolders% Topmost folders containing only empty subfolders:`r`n`r`n%topmostEmptyFoldersList%`r`n`r`n
}
else
{
	DEF_EmpSub1 =
	DEF_EmpSub2 =
}
DEF_1 = %DEF_TimeString%`r`n`r`n%countEmptyFolders% empty folders of %countFolders% total found inside %targetFolderEFLocation%`r`n`r`n%DEF_EmpSub1%time: %timeTaken% sec`r`n`r`n`r`n%countEmptyFolders% Folders without items:`r`n`r`n%emptyFolderList%`r`n`r`n`r`n%DEF_EmpSub2%---------------------------------`r`n`r`n
DEF_2 = The following folders could not be deleted:`r`n`r`n

ifInString, selectionEF, 1
	DEF_msg=%DEF_1%%DEF_2%
else ifInString, selectionEF, 2
	DEF_msg=%DEF_1%List of all %countFolders% subfolders:`r`n`r`n%FolderList%`r`n`r`n---------------------------------`r`n`r`n%DEF_2%
else
	DEF_msg=%DEF_TimeString%`r`n`r`n%countEmptyFolders% empty folders of %countFilesFolders% total files/folders found inside %targetFolderEFLocation%`r`n`r`n%DEF_EmpSub1%total folders: %countFolders%`r`n`r`ntime: %timeTaken% sec`r`n`r`n`r`n%countEmptyFolders% Folders without items:`r`n`r`n%emptyFolderList%`r`n`r`n`r`n%DEF_EmpSub2%---------------------------------`r`n`r`nList of all %countFilesFolders% files/folders:`r`n`r`n%FileFolderList%`r`n`r`n---------------------------------`r`n`r`n%DEF_2%

return


CreateLogName:

Time := A_Now
StringTrimRight, Time, Time, 2
StringRight, HourMin, Time, 4
StringTrimRight, Time, Time, 4
Time := Time . "." . HourMin
logName := "logfile_DeleteEmptyFolders_" . Time ".txt"

return


DEF_Results_NoUserPrompt:

Sort emptyFolderList
Sort FolderList
Sort FileFolderList

GoSub CreateLogName

if countEmptyFolders=
{
	if emptytargetFolderEF=
	{
		FileRemoveDir %targetFolderEFLocation%
		FileAppend, %DEF_TimeString%`r`n`r`n%targetFolderEFLocation% is an empty folder.  Deleted., %logName%
	}
	else ifInString, selectionEF, 1
		FileAppend, %DEF_TimeString%`r`n`r`nNo empty folders were found in %targetFolderEFLocation%., %logName%
	else
	{
		DEF_1 = %DEF_TimeString%`r`n`r`n0 empty folders of 
		DEF_2 = found inside %targetFolderEFLocation%`r`n`r`n
		DEF_3 = time: %timeTaken% sec`r`n`r`n%emptyFolderList%`r`n`r`n---------------------------------`r`n`r`nList of all%A_Space%

		ifInString, selectionEF, 2
			DEF_msg=%DEF_1%%countFolders% total %DEF_2%%DEF_3%%countFolders% subfolders:`r`n`r`n%FolderList%`r`n
		else
			DEF_msg=%DEF_1%%countFilesFolders% total files/folders %DEF_2%total folders: %countFolders%`r`n`r`n%DEF_3%%countFilesFolders% files/folders:`r`n`r`n%FileFolderList%`r`n

		FileAppend, %DEF_msg%, %logName%
	}
}
else
{
	GoSub DEF_EditorMsgStrings

	FileAppend, %DEF_msg%, %logName%

	listToDelete = %emptyFolderList%
	GoSub DeleteEmpty_NoUserPrompt

	if countFoldersEmptySubFolders
	{
		DEF_1 = 1
		listToDelete = %topmostEmptyFoldersList%
		GoSub DeleteEmpty_NoUserPrompt
	}
}

return


DeleteEmpty_NoUserPrompt:

errorList=
errorsOccurred=

Loop, Parse, listToDelete, `n, `r
{
	if A_LoopField=
		BREAK
	FileRemoveDir, %A_LoopField%, 1		; recurse to remove empty subfolders
	if ErrorLevel=1
	{
		errorList=%errorList%%A_LoopField%`r`n
		errorsOccurred=1
	}
}
if errorsOccurred=1
	FileAppend, %errorList%, %logName%
else
{
	if DEF_1 = 1
		FileAppend, of folders containing only empty subfolders:`n<none>`n`n, %logName%
	else
		FileAppend, of folders without items:`n<none>`n`n, %logName%
}

return		; end of auto-delete section
;-------------------------------------------

DEF_Results:

Sort emptyFolderList
Sort FolderList
Sort FileFolderList

if countEmptyFolders=
{
	if emptytargetFolderEF=
	{
		msgbox, 4097, DeleteEmptyFolders Utility, %targetFolderEFLocation%`n`nis an empty folder.`n`nDelete it?
		ifMsgBox, OK
			FileRemoveDir %targetFolderEFLocation%
	}
	else ifInString, selectionEF, 1
		msgbox, No empty folders were found inside`n`n%targetFolderEFLocation%
	else
	{
		Run, notepad,,, Npid
		WinActivate, ahk_pid %Npid%
		WinWaitActive, ahk_pid %Npid%
		FormatTime, DEF_TimeString

		DEF_1 = %DEF_TimeString%`r`n`r`n0 empty folders of 
		DEF_2 = found inside %targetFolderEFLocation%`r`n`r`n
		DEF_3 = time: %timeTaken% sec`r`n`r`n%emptyFolderList%`r`n`r`n---------------------------------`r`n`r`nList of all%A_Space%

		ifInString, selectionEF, 2
			DEF_msg=%DEF_1%%countFolders% total %DEF_2%%DEF_3%%countFolders% subfolders:`r`n`r`n%FolderList%`r`n
		else
			DEF_msg=%DEF_1%%countFilesFolders% total files/folders %DEF_2%total folders: %countFolders%`r`n`r`n%DEF_3%%countFilesFolders% files/folders:`r`n`r`n%FileFolderList%`r`n

		ControlSetText, Edit1, %DEF_msg%, ahk_pid %Npid%

		DEF_1 = found inside`n`n`t%targetFolderEFLocation%`n`n
		ifInString, selectionEF, 2
			msgbox, 4097, DeleteEmptyFolders Utility, 0 empty folders of %countFolders% total were %DEF_1%time: %timeTaken% sec`n`n
		else
			msgbox, 4097, DeleteEmptyFolders Utility, 0 empty folders of %countFilesFolders% total files/folders were %DEF_1%total folders: %countFolders%`n`ntime: %timeTaken% sec`n`n
	}
}
else
{
	Run, notepad,,, Npid
	WinActivate, ahk_pid %Npid%
	WinWaitActive, ahk_pid %Npid%
	FormatTime, DEF_TimeString

	GoSub DEF_EditorMsgStrings

	ControlSetText, Edit1, %DEF_msg%, ahk_pid %Npid%

	DEF_1 = %countEmptyFolders% empty folders of%A_Space%
	DEF_2 = were found inside`n`n`t%targetFolderEFLocation%`n`n%DEF_EmpSub1%
	DEF_3 = time: %timeTaken% sec`n`nKeep in mind that some empty folders might be `nused by the operating system, anti-virus, firewall, `nand various utilities, and even spyware.`n`nSave the log in case you are not sure what you are deleting.`n`n`nDo you wish to delete the %countEmptyFolders% empty folders?`n`nThe recycle bin will be bypassed.`n`nhold Ctrl Shift with OK to delete.`n

	ifInString, selectionEF, 3
		msgbox, 4097, DeleteEmptyFolders Utility, %DEF_1%%countFilesFolders% total files/folders %DEF_2%total folders: %countFolders%`n`n%DEF_3%
	else
		msgbox, 4097, DeleteEmptyFolders Utility, %DEF_1%%countFolders% total %DEF_2%%DEF_3%

	ifMsgBox, OK
	{
		if ( GetKeyState("Ctrl","p") AND GetKeyState("Shift","p") )
		{
			listToDelete = %emptyFolderList%
			GoSub DeleteEmpty

			if countFoldersEmptySubFolders
			{
				msgbox, 4097,, %countFoldersEmptySubFolders% folders containing only empty subfolders.`n`n%countTopmostEmptyFolders% topmost folders containing only empty subfolders.`n`n`n`nDo you wish to delete the %countTopmostEmptyFolders% topmost folders?`n`nThis will delete all %countFoldersEmptySubFolders% folders containing only empty subfolders.`n`nThe recycle bin will be bypassed.`n`nhold Ctrl Shift with OK to delete.`n
			
				ifMsgBox, OK
				if ( GetKeyState("Ctrl","p") AND GetKeyState("Shift","p") )
				{
					DEF_1 = 1
					listToDelete = %topmostEmptyFoldersList%
					GoSub DeleteEmpty
				}
			}
		}
	}
}

return


DeleteEmpty:

errorList=
errorsOccurred=

;SplashTextOn, 500, 100, DeleteEmptyFolders Utility, now deleting: ---
;WinSet, AlwaysOnTop, Off, DeleteEmptyFolders Utility 
Loop, Parse, listToDelete, `n, `r
{
	if A_LoopField=
		BREAK
;	ControlSetText, Static1, now deleting item %A_Index%:`n%A_LoopField%, DeleteEmptyFolders Utility
	FileRemoveDir, %A_LoopField%, 1		; recurse to remove empty subfolders
	if ErrorLevel=1
	{
		errorList=%errorList%%A_LoopField%`r`n
		errorsOccurred=1
		msgbox error deleting %A_LoopField%`n`nerrorList=`n`n%errorList%
	}
}
;SplashTextOff
if errorsOccurred=1
{
	ControlSend, Edit1, ^{end}, ahk_pid %Npid%
	Control, EditPaste, %errorList%, Edit1, ahk_pid %Npid%
	msgbox The following empty folders could not be deleted:`n`n%errorList%
}
else
{
	if DEF_1 = 1
	{
		ControlSend, Edit1, ^{end}of folders containing only empty subfolders:`n<none>`n`n, ahk_pid %Npid%
		msgbox %countEmptyFolders% folders containing only empty subfolders`n`nsuccessfully deleted.
	}
	else
	{
		ControlSend, Edit1, ^{end}of folders without items:`n<none>`n`n, ahk_pid %Npid%
		msgbox %countEmptyFolders% folders without items`n`nsuccessfully deleted. ; msgbox may interfere with deletion
	}
}
return		; for GoSub.  main return is at the top of function.


}	;--------------------------- end function

;-------------------------------------------------------



1) The Open Source Definition http://www.opensourc...ition_plain.php

2) Intuitive. Logical. Versatile. Adaptable. <>

Serenity
  • Members
  • 1271 posts
  • Last active:
  • Joined: 07 Nov 2004
This is a great script, thanks for sharing. :)
"Anything worth doing is worth doing slowly." - Mae West
Posted Image

enrica
  • Members
  • 117 posts
  • Last active: Feb 26 2016 11:15 PM
  • Joined: 21 Mar 2005
Very nice!
Thank you!
Ciao! :D

Decarlo110
  • Members
  • 303 posts
  • Last active: Feb 12 2006 02:15 AM
  • Joined: 15 Dec 2004
You're welcome, i enjoyed figuring out the script. :)

I've added a few things: a time counter for the scan, and options for listing all folders or listing all contents.
This is helpful for inspecting what's on the computer without having to click thru all those folders, or for just recording what's on it, for comparison purposes. Next will be to change the interface to gui and add a Cancel button, as well as checkbox selection support for found folders, a memory option to auto-check user-selected items, and also built-in log-comparison which will prove useful in determining what's been added after installation of new software (not including registry items).
edit: i changed my mind.. those things are probably better suited for a program of a different nature..

Edit: newest version in first post.
1) The Open Source Definition http://www.opensourc...ition_plain.php

2) Intuitive. Logical. Versatile. Adaptable. <>

thinkstorm
  • Members
  • 40 posts
  • Last active: Sep 10 2014 06:06 PM
  • Joined: 17 Aug 2004
Holy smokes!!! You people have to learn how to stop ;)

from http://www.pcmag.com...9,805015,00.asp:

I'm looking for a program that can delete any folders that are empty or that contain only empty subfolders. Can you help?

M. V. Weir

You don't need a program; you can do this at the command prompt. Open a command prompt window and navigate to the root folder of the drive in question. Enter this command:

DIR /AD/B/S | SORT /R > EMPTIES.BAT

The file EMPTIES.BAT now contains a list of all folders on your hard drive in reverse order. Use Word or another editor to put the filenames in quotes and add the prefix RD (with a space after RD) to every line in the file. In Word, you can do this easily by using Find and Replace to search for ^p (which represents the paragraph mark) and replace it with "^pRD " (quote, p, R, D, space quote), then hand-correct the first and last lines of the file if necessary. Save the modified EMPTIES .BAT file and exit your editor. Then simply launch the batch file. It will attempt the RD (remove directory) command on each folder, but the command will fail for any folder that is not empty.

How does it work? For the DIR command, the switch /AD means select files whose attributes include the Directory attribute (in other words, folders). The /B switch means give a "bare" listing—just the filename—and /S means look in subfolders, too (which, incidentally, modifies /B, so it shows the full pathname). The output is piped (|) as input to the SORT command. Not surprisingly, the switch /R means sort in reverse. Finally, the output of SORT is redirected (>) into the file EMPTIES.BAT. Because we're sorting in reverse, every folder's subfolders precede it in the list. If they are empty, then by the time the parent folder is processed, it too will be empty. You'd be surprised at what you can do with simple commands!

Decarlo110
  • Members
  • 303 posts
  • Last active: Feb 12 2006 02:15 AM
  • Joined: 15 Dec 2004

You don't need a program; you can do this at the command prompt.


An element of practicality is being able to do things with what is already there, and with this i certainly agree. However; it's better to invest effort where possible, when the inconveniences signal such occasion...

Use Word or another editor to put the filenames in quotes and add the prefix RD (with a space after RD) to every line in the file.


every line.... every line ! AutoHotkey to the rescue !! ;)
(By the way, i have over 1000 folders, and doing this manually does not appear inviting...)

And besides, i just improved my script (see my next post -- Edit: newest version in first post) to handle folders which contain only empty subfolders, and it was quite a mind-exercise in parsing, but worth it. Plus, it lists the full path of every empty folder of both types, which the reverse directory method it seems doesnt (perhaps someone can test it out if they have less than 1000 folders). 8)
1) The Open Source Definition http://www.opensourc...ition_plain.php

2) Intuitive. Logical. Versatile. Adaptable. <>

Decarlo110
  • Members
  • 303 posts
  • Last active: Feb 12 2006 02:15 AM
  • Joined: 15 Dec 2004
There is actually some interesting background as to why i coded this the way that i did. Back in the month of June this year, in the discussion thread, http://www.autohotke...opic.php?t=3815, corrupt made a very keen observation:

Almost... With your code the file may have been found but could not be deleted but would get reported as not found...

I immediately recognized there would be a problem with identifying locked or busy/in-use files. This situation could also apply to empty directories, although i was not sure of all possible scenarios.

Although i recognize that errorLevel can be used as a shortcut for coding a version of a solution, it has the limitation of not being able to detect locked/in-use/undeletable folders. This program lists any folders which could not be deleted, as well as identifies and handles folders with only empty subfolders.
1) The Open Source Definition http://www.opensourc...ition_plain.php

2) Intuitive. Logical. Versatile. Adaptable. <>

Venia Legendi
  • Members
  • 35 posts
  • Last active: Apr 04 2011 08:36 PM
  • Joined: 27 May 2005
See http://www.otbsw.com/Files/rmemp12.zip

nlucier
  • Members
  • 4 posts
  • Last active: Feb 25 2006 11:55 PM
  • Joined: 25 Feb 2006
Any reason that the script should fail on directories like those in the following list? Just using the FileRemoveDir on these same folders doesn't work and it's driving me nuts. I can move files from inside them to other locations, delete all files inside the folder, I just cannot delete the folder itself using a script. They are not locked, as manual deletion works fine through Explorer or command line.

H:\EtiVo\{Signing Time!}{2006-02-05}{My Favorite Things}{PN8}.show
H:\EtiVo\{Signing Time!}{2006-01-29}{ABCs}{PN8}.show
H:\EtiVo\{Signing Time!}{2006-01-22}{Family, Feelings _ Fun}{PN8}.show
H:\EtiVo\{Signing Time!}{2006-01-15}{Everyday Signs}{PN8}.show
H:\EtiVo\{Signing Time!}{2006-01-08}{Playtime Signs}{PN8}.show
H:\EtiVo\{Signing Time!}{2006-01-01}{My First Signs}{PN8}.show
H:\EtiVo\{Max _ Ruby}{2004-11-04}{Ruby Flies a Kite; Max's Pretend Friend; Fireman Max}{NIK1}.show
H:\EtiVo\{Max _ Ruby}{2003-09-08}{Max's Shadow; Max Remembers; Ruby's Candy Store}{NIK1}.show
H:\EtiVo\{Max _ Ruby}{2003-03-07}{Max's Bug Salad; Ruby's Beach Party; Super Max to the Rescue}{NOG}.show
H:\EtiVo\{Max _ Ruby}{2003-01-16}{Ruby's Panda Prize; Ruby's Rollerskates; Ghost Bunny}{NOG}.show
H:\EtiVo\{Max _ Ruby}{2003-01-10}{Max's Froggy Friend; Max's Music; Max Gets Wet}{NIK1}.show
H:\EtiVo\{Max _ Ruby}{2002-10-30}{Bunny Cakes; Bunny Party; Bunny Money}{NOG}.show
H:\EtiVo\{Maisy}{2000-10-31}{}{NOG}.show
H:\EtiVo\{Maisy}{2000-10-12}{}{NOG}.show
H:\EtiVo\{Maisy}{2000-10-10}{}{NOG}.show
H:\EtiVo\{Maisy}{1999-12-07}{Umbrella; Rollerskates; Feather; Cleaning}{NOG}.show
H:\EtiVo\{LazyTown}{2004-08-16}{Welcome to LazyTown}{PN5}.show
H:\EtiVo\{Good Eats}{2004-10-27}{Art of Darkness III}{FOOD}.show
H:\EtiVo\{Good Eats}{2002-11-13}{Choux Shine}{FOOD}.show
H:\EtiVo\{Good Eats}{2002-03-20}{For Whom the Cheese Melts 2}{FOOD}.show
H:\EtiVo\{Good Eats}{2001-03-14}{Seeing Red}{FOOD}.show
H:\EtiVo\{Good Eats}{2001-02-10}{The Art of Darkness}{FOOD}.show
H:\EtiVo\{Go, Diego, Go!}{2006-01-25}{A Blue Morpho Butterfly Is Born}{NIK2}.show
H:\EtiVo\{Dora the Explorer}{2003-04-28}{To the Monkey Bars}{NIK2}.show
H:\EtiVo\{Dora the Explorer}{2002-04-23}{The Big Storm}{NIK1}.show

I have tried to use the script provided at the top and get a quick message box that says "invalid parameter" that stays on screen for about 1/2 second. The script works great when I make test folders.


This is the code I am using to attempt do what I want. Basically it looks for a particular file type (.ty file in this case), moves the file to another location and then deletes the old parent folder (there is a FileDelete in there as part of my testing to make sure the folder is empty and that works fine).

Loop, H:\EtiVo\*.ty, , 1
{
	FileMove, %A_LoopFileFullPath%, h:\tivo\video
	FileDelete, %A_LoopFileDir%\*
	FileRemoveDir, %A_LoopFileDir%, 1
}


Is there something I'm missing here? Is it the braces that is throwing AutoHotKey for a loop?


EDIT: I am using my code for the initial attempt, but failing that was trying to use the thread author's script as a backup and it's not working on te above folders as well. Not all of those folders are empty, actually only about 5 or 6 will be at any one time.

Laszlo
  • Moderators
  • 4713 posts
  • Last active: Mar 31 2012 03:17 AM
  • Joined: 14 Feb 2005
I created a few of those directories on my C: drive, FileRemoveDir deleted them OK. Do you have an open explorer window, which shows the H:\EtiVo directory? FileRemoveDir is known not to work in this case.

nlucier
  • Members
  • 4 posts
  • Last active: Feb 25 2006 11:55 PM
  • Joined: 25 Feb 2006
I've attempted it without doing anything to open windows and then with nothing showing in my taskbar. Didn't help.

The H:\EtiVo folder is a virtual folder in IIS though for serving the EtiVo server stuff out to the web for remote viewing. I made sure to close all of the browsers that had been attached to it. Could it be IIS that has control? I have tried using Unlocker to see if the folder is locked and it sees nothing holding it back and since I can manually delete the folders, I believe Unlocker.

I am rebooting the machine to see if maybe I can do it immediately on a reboot before the EtiVo service even runs.

nlucier
  • Members
  • 4 posts
  • Last active: Feb 25 2006 11:55 PM
  • Joined: 25 Feb 2006

I am rebooting the machine to see if maybe I can do it immediately on a reboot before the EtiVo service even runs.


No dice. It must be that the EtiVo service is holding that folder hostage enough that AutoHotKey cannot remove it. It's just strange that manual methods work fine and sucks that I can't have AutoHotKey be the do all. Going to try and see if it'll work using cli commands instead of FileRemoveDir.

nlucier
  • Members
  • 4 posts
  • Last active: Feb 25 2006 11:55 PM
  • Joined: 25 Feb 2006
Laszlo,

Your script for deleting empty directories that I found in the Script catalog worked for removing the stuck folders. Now I'm busy trying to figure out why that is, including maybe timing since it was about 10 minutes later that I tried it.