AutoHotkey Community

It is currently May 27th, 2012, 1:58 am

All times are UTC [ DST ]




Post new topic Reply to topic  [ 34 posts ]  Go to page Previous  1, 2, 3  Next
Author Message
 Post subject:
PostPosted: November 2nd, 2005, 11:46 pm 
Offline

Joined: February 14th, 2005, 4:05 pm
Posts: 4710
Location: Boulder, CO
If you need them, you put the FileGetTime + FileGetAttrib as the first 2 lines in the function, and use their data after FileRemoveDir. The 20 line version is a simple exercise left for the reader. Try it!


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: November 3rd, 2005, 1:12 am 
Offline

Joined: February 14th, 2005, 4:05 pm
Posts: 4710
Location: Boulder, CO
Only read further if you do not want to try to write the script yourself. Here it is in 17 lines:
Code:
DelEmpty(dir)
{
   FileGetAttrib Attrib,  %dir%
   FileGetTime ModTime,   %dir%, M
   FileGetTime CreateTime,%dir%, C

   Loop %dir%\*.*, 2
      DelEmpty(A_LoopFileFullPath)

   FileRemoveDir %dir%
   IfEqual ErrorLevel,1, Return

   MsgBox 4,,Remove directory %dir%?`nCreated:  %CreateTime%`nModified: %ModTime%
   IfMsgBox Yes
      Return
   FileCreateDir %dir%
   FileSetAttrib +%Attrib%, %dir%
   FileSetTime %CreateTime%,%dir%, C
   FileSetTime %ModTime%,   %dir%, M
}

Here is a version in 19 lines, which also sets the global variable "kept" to the number of empty directories not deleted.
Code:
DelEmpty(dir)
{
   Global kept
   FileGetAttrib Attrib,  %dir%
   FileGetTime ModTime,   %dir%, M
   FileGetTime CreateTime,%dir%, C

   Loop %dir%\*.*, 2
      DelEmpty(A_LoopFileFullPath)

   FileRemoveDir %dir%
   IfEqual ErrorLevel,1, Return

   MsgBox 4,,Remove directory %dir%?`nCreated:  %CreateTime%`nModified: %ModTime%
   IfMsgBox Yes
      Return
   kept += 1
   FileCreateDir %dir%
   FileSetAttrib +%Attrib%, %dir%
   FileSetTime %CreateTime%,%dir%, C
   FileSetTime %ModTime%,   %dir%, M
}

If you also count the total number of subdirectories encountered (global total) and the number of empty directories, which were deleted (global deleted), the number of lines in the function grows to 22, but 2 of them has no code, only the delimiting braces.
Code:
DelEmpty(dir)
{
   Global total, deleted, kept
   total += 1
   FileGetAttrib Attrib,  %dir%
   FileGetTime ModTime,   %dir%, M
   FileGetTime CreateTime,%dir%, C

   Loop %dir%\*.*, 2
      DelEmpty(A_LoopFileFullPath)

   FileRemoveDir %dir%
   IfEqual ErrorLevel,1, Return
   deleted += 1
   MsgBox 4,,Remove directory %dir%?`nCreated:  %CreateTime%`nModified: %ModTime%
   IfMsgBox Yes
      Return
   kept += 1
   deleted -= 1
   FileCreateDir %dir%
   FileSetAttrib +%Attrib%, %dir%
   FileSetTime %CreateTime%,%dir%, C
   FileSetTime %ModTime%,   %dir%, M
}


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: January 27th, 2010, 12:21 pm 
The first script by jballi is exactly what I was looking for, how can I make it work on current/open/active Dir.?

Can someone help? :)


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: January 27th, 2010, 2:42 pm 
Offline

Joined: May 17th, 2007, 12:07 pm
Posts: 1004
Location: Germany - Deutschland
Just add
SetWorkingDir, c:\your_dir
to the beginning of the script.
Shira wrote:
The first script by jballi is exactly what I was looking for, how can I make it work on current/open/active Dir.?

Can someone help? :)


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: January 27th, 2010, 2:49 pm 
aaffe wrote:
Just add
SetWorkingDir, c:\your_dir
to the beginning of the script.
Shira wrote:
The first script by jballi is exactly what I was looking for, how can I make it work on current/open/active Dir.?

Can someone help? :)



Thanks for the reply aaffe, but I don't want it be fixed on certain dir.
I want it to work on whatever dir. is open/active
I looked through the help file but can't figure it out
It should be something like

SetWorkingDir, currentdir.

Any idea?


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: January 27th, 2010, 3:15 pm 
Offline

Joined: May 17th, 2007, 12:07 pm
Posts: 1004
Location: Germany - Deutschland
Shira wrote:
aaffe wrote:
Just add
SetWorkingDir, c:\your_dir
to the beginning of the script.
Shira wrote:
The first script by jballi is exactly what I was looking for, how can I make it work on current/open/active Dir.?

Can someone help? :)



Thanks for the reply aaffe, but I don't want it be fixed on certain dir.
I want it to work on whatever dir. is open/active
I looked through the help file but can't figure it out
It should be something like

SetWorkingDir, currentdir.

Any idea?


Try this, untested!:
Code:
;RemoveEmptyDirectories.ahk

;-- Confirm
dir=%1%
If dir=
   dir:=A_WorkingDir
SetWorkingdir,%dir%
sleep 500
MsgBox 1,,About to remove all empty directories from:`r`n%Dir%\
IfMsgBox Cancel
    return

;-- Initialize
$MinimumSplashTime=850


;[========================]
;[                        ]
;[  Build Directory List  ]
;[                        ]
;[========================]
;-- Splash
SplashImage,, W258 B2 ,,Building directory list...
$SplashStartTime=%A_TickCount%
Sleep 10  ;-- Allow splash image to render

;-- Build directory list
$DirectoryList=
Loop %dir%\*.*,2,1
    $DirectoryList=%$DirectoryList%%A_LoopFileFullPath%`r`n

;-- Sort it
Sort $DirectoryList

;-- Compute elapsed time. Sleep if necessary
$SplashElapsedTime:=A_TickCount - $SplashStartTime
if $SplashElapsedTime<%$MinimumSplashTime%
    {
    $SleepTime:=$MinimumSplashTime - $SplashElapsedTime
    Sleep %$SleepTime%
    }


;[==================]
;[                  ]
;[  Remove Empties  ]
;[                  ]
;[==================]
;-- Splash
SplashImage,, W354 B2 ,,Searching for directories to delete...
$SplashStartTime=%A_TickCount%

;--- Loop until all empty directories have been deleted
$DeleteTotal=0
$LoopCount=0
Loop 99
    {
    $LoopCount:=$LoopCount + 1
    $DeleteCount=0
    Loop parse,$DirectoryList,`n,`r
        {
        if A_LoopField=  ;-- Ignore blank item
            continue
        IfExist %A_LoopField%
            {
            FileRemoveDir %A_LoopField%
            if errorlevel=0
               $DeleteCount:=$DeleteCount + 1
            }
        }
    $DeleteTotal:=$DeleteTotal + $DeleteCount
    If $DeleteCount=0
        break
    }

;-- Compute elapsed time. Sleep if necessary
$SplashElapsedTime:=A_TickCount - $SplashStartTime
if $SplashElapsedTime<%$MinimumSplashTime%
    {
    $SleepTime:=$MinimumSplashTime - $SplashElapsedTime
    Sleep %$SleepTime%
    }

;-- Splash off
SplashImage off


;[===========]
;[           ]
;[  Results  ]
;[           ]
;[===========]
If $DeleteTotal=0
    msgbox No empty directories found
 else
    msgbox %$DeleteTotal% empty directories removed


;-- Return to sender
return


You call it via "name_of_the_ahkfile.ahk c:\test"


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: January 27th, 2010, 3:51 pm 
aaffe,

This will make the dir of the ahk file that calls it the workingdir. :(
I want it to work on the current active dir and not the dir of the file that calls it or the dir of where its placed.

Any solution?


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: January 27th, 2010, 3:55 pm 
Offline

Joined: May 17th, 2007, 12:07 pm
Posts: 1004
Location: Germany - Deutschland
Shira wrote:
aaffe,

This will make the dir of the ahk file that calls it the workingdir. :(
I want it to work on the current active dir and not the dir of the file that calls it or the dir of where its placed.

Any solution?


No, youre wrong. Now you can give any dir to the script and it will perform on this.
try
"RemoveEmptyDirectories.ahk c:\tmp"
or
"RemoveEmptyDirectories.ahk c:\windows"


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: January 27th, 2010, 6:09 pm 
aaffe wrote:
No, youre wrong.


Well, you misunderstood then, I don't want to keep telling it where to remove
I want it to work in whatever opened explorer I have
I just want to browse around my computer and when I need it then I hit a hotkey and it will work in that active dir.

And why do I have to have a second AHK to call it anyway?
Can't it be done by having it running all the time and functioning via hotkey?


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: January 28th, 2010, 6:01 am 
Offline

Joined: October 1st, 2005, 9:55 pm
Posts: 775
Location: Texas, USA
Wow, this is an old post! One of the first things I wrote when joining this forum. Although my first version of the script worked, it wasn't very efficient as you would see if you read through all of the responses.

Shira wrote:
Well, you misunderstood then, I don't want to keep telling it where to remove
I want it to work in whatever opened explorer I have
I just want to browse around my computer and when I need it then I hit a hotkey and it will work in that active dir.

And why do I have to have a second AHK to call it anyway?
Can't it be done by having it running all the time and functioning via hotkey?

If you want to use a hotkey to do the dirty deed, you'll need to get the current Windows Explorer folder to use by your script. There are many many versions of this solution posted on the forum. This is mine.

Code:
;*****************************
;*                           *
;*    CurrentExplorerPath    *
;*                           *
;*****************************
;
;
;   Description
;   ===========
;   This function returns the current path from the active Windows Explorer
;   window.  See the "Return Codes" section for more information.
;
;
;
;   Parameters
;   ==========
;   (None)
;
;
;
;   Return Codes
;   ============
;   Information is returned via the function and through ErrorLevel (system
;   variable).
;
;   Possible ErrorLevel values:
;
;     0 = No errors
;     1 = Windows Explorer|Cabinet window not active
;     2 = Invalid path format
;     3 = Path not found
;
;   If ErrorLevel is 0, the current path of the active Windows Explorer window
;   is returned.  If ErrorLevel is not 0, "C:" (sans quotes) is returned.
;
;
;   Important: ErrorLevel is a system variable and is used by many commands.
;   If you are unable to test ErrorLevel immediate after calling this function,
;   assign the value to another variable so that the return value is retained.
;
;   
;
;   Programming Notes
;   =================
;   For Windows Explorer, this design assumes that the current folder name
;   can be extracted from the Edit1 control.  There are 2 problems with this
;   design.  First of all, if the user modifies the value of Edit1, the function
;   will return modified value.  Although the function is designed to deal
;   with this possibility, it's still a pain in the butt.  Next, on rare
;   occation, the control that displays the current folder name is not labeled
;   Edit1.  When this occurs, the function will always return an invalid or
;   (usually) blank value.  The "problem" will not be corrected until the
;   window with the invalid control name is closed and a new Explorer window
;   is opened.  I have been unable to identify if/when/how this occurs.
;
;   Although the design is flawed, it is still more accurate than collecting the
;   current folder name from the title which truncates long folder names.
;
;-------------------------------------------------------------------------------
CurrentExplorerPath()
    {
    ;-- Windows Explorer/Cabinet active?
    IfWinNotActive ahk_class ExploreWClass
        IfWinNotActive ahk_class CabinetWClass
            {
            ErrorLevel=1
            Return "C:"
            }

    ;-- Collect the current directory
    ControlGetText l_CurrentPath,Edit1

    ;-- Desktop?
    if l_CurrentPath=Desktop
        l_CurrentPath:=A_Desktop

    ;-- My Documents?
    if l_CurrentPath=My Documents
        l_CurrentPath:=A_MyDocuments
 
    ;-- Valid path?
    SplitPath l_CurrentPath,,,,,l_Drive
    if StrLen(l_Drive)=0
        {
        outputdebug,
           (ltrim join`s
            Function: %A_ThisFunc% -
            Unable to decode path from the Windows Explorer window.
           )

        ErrorLevel=2
        Return "C:"
        }
     else
        {
        IfNotExist %l_CurrentPath%
            {
            outputdebug,
               (ltrim join`s
                Function: %A_ThisFunc% -
                Target folder "%l_CurrentPath%" does not exist.
               )

            ErrorLevel=3
            Return "C:"
            }
        }

    ;-- Return to sender
    ErrorLevel=0
    return l_CurrentPath
    }

If you use this function, be sure to check the ErrorLevel before using the value of function to remove empty folders. The default return value if there is a problem is C:\. I'm sure you don't want to remove all empty folders from the the C: drive.

Although removing empty folders from via a hotkey is good, performing the dirty deed via a Windows Explorer context menu might be more appropriate. If you're interested, PM me and I'll send my version of the "Remove Empty Folders" script that is run via the Windows Explorer context menu.

Good luck!


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: January 28th, 2010, 10:59 am 
jballi wrote:
Good luck!


Hmmm...
This last one doesn't work, made a hotkey in it and called it from a second script, doesn't work
But whats wrong with the first (Old) one? Its working really good, I like it because it wont delete any folder that has a file in it, even if its an empty file.

And I'm not a fan of context menu hehe :D
I like to get things done silently

Can't you edit it to work on active dir.? Shouldn't be hard

Right now I'm using it and adding current path to it all the time via another script, which is lame and noob


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: January 28th, 2010, 11:10 am 
Offline

Joined: May 17th, 2007, 12:07 pm
Posts: 1004
Location: Germany - Deutschland
You should add the function from jballi to the first script, then call it via
Code:
curr_dir:=CurrentExplorerPath()
If not Errorlevel
  SetWorkingdir,%curr_dir%


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: January 28th, 2010, 8:49 pm 
aaffe wrote:
You should add the function from jballi to the first script, then call it via
Code:
curr_dir:=CurrentExplorerPath()
If not Errorlevel
  SetWorkingdir,%curr_dir%


Your solution is not working, at least not under W7

jballi,
Can you please try and edit it so it will work on current dir. and not where its placed or where its being called via?


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: January 29th, 2010, 9:18 am 
Offline

Joined: May 17th, 2007, 12:07 pm
Posts: 1004
Location: Germany - Deutschland
Hy Shira,
why dont you post your whole script so we can edit it?


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: January 29th, 2010, 9:02 pm 
aaffe wrote:
Hy Shira,
why dont you post your whole script so we can edit it?


I don't know what to post, there isn't much to post, I tried adding your part to the top of the first script and then called it via another script, like this..
Code:
curr_dir:=CurrentExplorerPath()
If not Errorlevel
  SetWorkingdir,%curr_dir%


$MinimumSplashTime=850

SplashImage,, W258 B2 ,,Building directory list...
$SplashStartTime=%A_TickCount%
Sleep 10
$DirectoryList=
Loop *.*,2,1
    $DirectoryList=%$DirectoryList%%A_LoopFileFullPath%`r`n
Sort $DirectoryList
$SplashElapsedTime:=A_TickCount - $SplashStartTime
if $SplashElapsedTime<%$MinimumSplashTime%
    {
    $SleepTime:=$MinimumSplashTime - $SplashElapsedTime
    Sleep %$SleepTime%
    }
SplashImage,, W354 B2 ,,Searching for directories to delete...
$SplashStartTime=%A_TickCount%
$DeleteTotal=0
$LoopCount=0
Loop 99
    {
    $LoopCount:=$LoopCount + 1
    $DeleteCount=0
    Loop parse,$DirectoryList,`n,`r
        {
        if A_LoopField=  ;-- Ignore blank item
            continue
        IfExist %A_LoopField%
            {
            FileRemoveDir %A_LoopField%
            if errorlevel=0
               $DeleteCount:=$DeleteCount + 1
            }
        }
    $DeleteTotal:=$DeleteTotal + $DeleteCount
    If $DeleteCount=0
        break
    }
;-- Compute elapsed time. Sleep if necessary
$SplashElapsedTime:=A_TickCount - $SplashStartTime
if $SplashElapsedTime<%$MinimumSplashTime%
    {
    $SleepTime:=$MinimumSplashTime - $SplashElapsedTime
    Sleep %$SleepTime%
    }
SplashImage off
If $DeleteTotal=0
    msgbox No empty directories found
 else
    msgbox %$DeleteTotal% empty directories removed
return

Also I tried adding the little part to the top of the script I use it to call this one, same, errors


Report this post
Top
  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 34 posts ]  Go to page Previous  1, 2, 3  Next

All times are UTC [ DST ]


Who is online

Users browsing this forum: Relayer and 13 guests


You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Powered by phpBB® Forum Software © phpBB Group