AutoHotkey Homepage AutoHotkey Community
Let's help each other out
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Move files after download - Leaves empty folders

 
Post new topic   Reply to topic    AutoHotkey Community Forum Index -> Ask for Help
View previous topic :: View next topic  
Author Message
bpmb



Joined: 13 Sep 2009
Posts: 22

PostPosted: Sat Nov 14, 2009 10:20 am    Post subject: Move files after download - Leaves empty folders Reply with quote

Hi there!

I have a script running which processes files I download using Newsleecher. Newsleecher downloads the files, AutoUnpack checks them and unpacks them and then my script copies music to the music folder, video to the video folder and leaves the rest in my 'Media' folder, which is the folder AutoUnpack unpacks to. Newsleecher is watching a folder for NZB's so I can export an NZB from my laptop to this watchfolder and Newsleecher/ Autounpack/ Autohotkey take care of the rest.

The files are copied on a file level so my Media folder contains a lot of (almost) empty folders. At the end of the script you can see my attempt to have those empty folder auto-deleted which works if they are completely empty but a lot of folders contain *.sfv of *.nfo files.

I can, of course, write a file loop which deletes those files first but for some folders I want to keep at least the *.nfo files.

I tried using FileSizeKB but can't seem to be able to combine that with turning the file loop 'inside-out' to start at the deepest level..

Below the full script and below that is the FileRemoveDir part, if anyone can point me in the right directon?

Code:
#Persistent
#InstallKeybdHook

IniRead, BrowseSource, %A_AppData%\BackupVideo\config.ini, Sourcefolder, key
IniRead, BrowseDest, %A_AppData%\BackupVideo\config.ini, Destinationfolder, key
SetWorkingDir, %BrowseSource%
Loop, *.avi, , 1
{
    CopySourcePattern = %A_LoopFileLongPath%
    CopyDest = %BrowseDest%
    copy_it = n
    IfNotExist, %CopyDest%\%A_LoopFileName%  ; Always copy if target file doesn't yet exist.
        copy_it = y
    else
    {
        FileGetTime, time, %CopyDest%\%A_LoopFileName%
        EnvSub, time, %A_LoopFileTimeModified%, seconds  ; Subtract the source file's time from the destination's.
        if time < 0  ; Source file is newer than destination file.
            copy_it = y
    }
    if copy_it = y
    {
        FileCopy, %A_LoopFileLongPath%, %CopyDest%\%A_LoopFileName%, 1   ; Copy with overwrite=yes
        if ErrorLevel
            MsgBox, Could not copy "%A_LoopFileFullPath%" to "%CopyDest%\%A_LoopFileName%".
    }
    FileDelete, %A_LoopFileLongPath%
}
SetWorkingDir, %BrowseSource%
Loop, VIDEO_TS, 2, 1
{
    CopySourcePattern = %BrowseSource%\%A_LoopFileDir%
    CopyDest = %BrowseDest%
    copy_it = n
    IfNotExist, %CopyDest%\%A_LoopFileDir%  ; Always copy if target file doesn't yet exist.
        copy_it = y
    else
    {
        FileGetTime, time, %CopyDest%\%A_LoopFileDir%
        EnvSub, time, %A_LoopFileTimeModified%, seconds  ; Subtract the source file's time from the destination's.
        if time < 0  ; Source file is newer than destination file.
            copy_it = y
    }
    if copy_it = y
    {
        FileCopyDir, %BrowseSource%\%A_LoopFileDir%, %CopyDest%\%A_LoopFileDir%, 1   ; Copy with overwrite=yes
        if ErrorLevel
            MsgBox, Could not copy "%CopySourcePattern%" to "%CopyDest%\%A_LoopFileDir%".
}
   FileRemoveDir, %BrowseSource%\%A_LoopFileDir%, 1
}
SetWorkingDir, %BrowseSource%
Loop, *.mpg, , 1
{
    CopySourcePattern = %A_LoopFileLongPath%
    CopyDest = %BrowseDest%
    copy_it = n
    IfNotExist, %CopyDest%\%A_LoopFileName%  ; Always copy if target file doesn't yet exist.
        copy_it = y
    else
    {
        FileGetTime, time, %CopyDest%\%A_LoopFileName%
        EnvSub, time, %A_LoopFileTimeModified%, seconds  ; Subtract the source file's time from the destination's.
        if time < 0  ; Source file is newer than destination file.
            copy_it = y
    }
    if copy_it = y
    {
        FileCopy, %A_LoopFileLongPath%, %CopyDest%\%A_LoopFileName%, 1   ; Copy with overwrite=yes
        if ErrorLevel
            MsgBox, Could not copy "%A_LoopFileFullPath%" to "%CopyDest%\%A_LoopFileName%".
    }
    FileDelete, %A_LoopFileLongPath%
}
SetWorkingDir, %BrowseSource%
Loop, *.mp4, , 1
{
    CopySourcePattern = %A_LoopFileLongPath%
    CopyDest = %BrowseDest%
    copy_it = n
    IfNotExist, %CopyDest%\%A_LoopFileName%  ; Always copy if target file doesn't yet exist.
        copy_it = y
    else
    {
        FileGetTime, time, %CopyDest%\%A_LoopFileName%
        EnvSub, time, %A_LoopFileTimeModified%, seconds  ; Subtract the source file's time from the destination's.
        if time < 0  ; Source file is newer than destination file.
            copy_it = y
    }
    if copy_it = y
    {
        FileCopy, %A_LoopFileLongPath%, %CopyDest%\%A_LoopFileName%, 1   ; Copy with overwrite=yes
        if ErrorLevel
            MsgBox, Could not copy "%A_LoopFileFullPath%" to "%CopyDest%\%A_LoopFileName%".
    }
    FileDelete, %A_LoopFileLongPath%
}
SetWorkingDir, %BrowseSource%
Loop, *.dv, , 1
{
    CopySourcePattern = %A_LoopFileLongPath%
    CopyDest = %BrowseDest%
    copy_it = n
    IfNotExist, %CopyDest%\%A_LoopFileName%  ; Always copy if target file doesn't yet exist.
        copy_it = y
    else
    {
        FileGetTime, time, %CopyDest%\%A_LoopFileName%
        EnvSub, time, %A_LoopFileTimeModified%, seconds  ; Subtract the source file's time from the destination's.
        if time < 0  ; Source file is newer than destination file.
            copy_it = y
    }
    if copy_it = y
    {
        FileCopy, %A_LoopFileLongPath%, %CopyDest%\%A_LoopFileName%, 1   ; Copy with overwrite=yes
        if ErrorLevel
            MsgBox, Could not copy "%A_LoopFileFullPath%" to "%CopyDest%\%A_LoopFileName%".
    }
    FileDelete, %A_LoopFileLongPath%
}
SetWorkingDir, %BrowseSource%
Loop, *.mov, , 1
{
    CopySourcePattern = %A_LoopFileLongPath%
    CopyDest = %BrowseDest%
    copy_it = n
    IfNotExist, %CopyDest%\%A_LoopFileName%  ; Always copy if target file doesn't yet exist.
        copy_it = y
    else
    {
        FileGetTime, time, %CopyDest%\%A_LoopFileName%
        EnvSub, time, %A_LoopFileTimeModified%, seconds  ; Subtract the source file's time from the destination's.
        if time < 0  ; Source file is newer than destination file.
            copy_it = y
    }
    if copy_it = y
    {
        FileCopy, %A_LoopFileLongPath%, %CopyDest%\%A_LoopFileName%, 1   ; Copy with overwrite=yes
        if ErrorLevel
            MsgBox, Could not copy "%A_LoopFileFullPath%" to "%CopyDest%\%A_LoopFileName%".
    }
    FileDelete, %A_LoopFileLongPath%
}
SetWorkingDir, %BrowseSource%
Loop, *.mkv, , 1
{
    CopySourcePattern = %A_LoopFileLongPath%
    CopyDest = %BrowseDest%
    copy_it = n
    IfNotExist, %CopyDest%\%A_LoopFileName%  ; Always copy if target file doesn't yet exist.
        copy_it = y
    else
    {
        FileGetTime, time, %CopyDest%\%A_LoopFileName%
        EnvSub, time, %A_LoopFileTimeModified%, seconds  ; Subtract the source file's time from the destination's.
        if time < 0  ; Source file is newer than destination file.
            copy_it = y
    }
    if copy_it = y
    {
        FileCopy, %A_LoopFileLongPath%, %CopyDest%\%A_LoopFileName%, 1   ; Copy with overwrite=yes
        if ErrorLevel
            MsgBox, Could not copy "%A_LoopFileFullPath%" to "%CopyDest%\%A_LoopFileName%".
    }
    FileDelete, %A_LoopFileLongPath%
}
SetWorkingDir, %BrowseSource%
Loop, *.wmv, , 1
{
    CopySourcePattern = %A_LoopFileLongPath%
    CopyDest = %BrowseDest%
    copy_it = n
    IfNotExist, %CopyDest%\%A_LoopFileName%  ; Always copy if target file doesn't yet exist.
        copy_it = y
    else
    {
        FileGetTime, time, %CopyDest%\%A_LoopFileName%
        EnvSub, time, %A_LoopFileTimeModified%, seconds  ; Subtract the source file's time from the destination's.
        if time < 0  ; Source file is newer than destination file.
            copy_it = y
    }
    if copy_it = y
    {
        FileCopy, %A_LoopFileLongPath%, %CopyDest%\%A_LoopFileName%, 1   ; Copy with overwrite=yes
        if ErrorLevel
            MsgBox, Could not copy "%A_LoopFileFullPath%" to "%CopyDest%\%A_LoopFileName%".
    }
    FileDelete, %A_LoopFileLongPath%
}

IniRead, BrowseSource, %A_AppData%\BackupVideo\music.ini, Sourcefolder, key
IniRead, BrowseDest, %A_AppData%\BackupVideo\music.ini, Destinationfolder, key
SetWorkingDir, %BrowseSource%
Loop, *.wav, , 1
{
    CopySourcePattern = %A_LoopFileLongPath%
    CopyDest = %BrowseDest%
    copy_it = n
    IfNotExist, %CopyDest%\%A_LoopFileName%  ; Always copy if target file doesn't yet exist.
        copy_it = y
    else
    {
        FileGetTime, time, %CopyDest%\%A_LoopFileName%
        EnvSub, time, %A_LoopFileTimeModified%, seconds  ; Subtract the source file's time from the destination's.
        if time < 0  ; Source file is newer than destination file.
            copy_it = y
    }
    if copy_it = y
    {
        FileCopy, %A_LoopFileLongPath%, %CopyDest%\%A_LoopFileName%, 1   ; Copy with overwrite=yes
        if ErrorLevel
            MsgBox, Could not copy "%A_LoopFileFullPath%" to "%CopyDest%\%A_LoopFileName%".
    }
    FileDelete, %A_LoopFileLongPath%
}
SetWorkingDir, %BrowseSource%
Loop, *.mp3, , 1
{
    CopySourcePattern = %A_LoopFileLongPath%
    CopyDest = %BrowseDest%
    copy_it = n
    IfNotExist, %CopyDest%\%A_LoopFileName%  ; Always copy if target file doesn't yet exist.
        copy_it = y
    else
    {
        FileGetTime, time, %CopyDest%\%A_LoopFileName%
        EnvSub, time, %A_LoopFileTimeModified%, seconds  ; Subtract the source file's time from the destination's.
        if time < 0  ; Source file is newer than destination file.
            copy_it = y
    }
    if copy_it = y
    {
        FileCopy, %A_LoopFileLongPath%, %CopyDest%\%A_LoopFileName%, 1   ; Copy with overwrite=yes
        if ErrorLevel
            MsgBox, Could not copy "%A_LoopFileFullPath%" to "%CopyDest%\%A_LoopFileName%".
    }
    FileDelete, %A_LoopFileLongPath%
}
SetWorkingDir, %BrowseSource%
Loop, *.flac, , 1
{
    CopySourcePattern = %A_LoopFileLongPath%
    CopyDest = %BrowseDest%
    copy_it = n
    IfNotExist, %CopyDest%\%A_LoopFileName%  ; Always copy if target file doesn't yet exist.
        copy_it = y
    else
    {
        FileGetTime, time, %CopyDest%\%A_LoopFileName%
        EnvSub, time, %A_LoopFileTimeModified%, seconds  ; Subtract the source file's time from the destination's.
        if time < 0  ; Source file is newer than destination file.
            copy_it = y
    }
    if copy_it = y
    {
        FileCopy, %A_LoopFileLongPath%, %CopyDest%\%A_LoopFileName%, 1   ; Copy with overwrite=yes
        if ErrorLevel
            MsgBox, Could not copy "%A_LoopFileFullPath%" to "%CopyDest%\%A_LoopFileName%".
    }
    FileDelete, %A_LoopFileLongPath%
}
SetWorkingDir, %BrowseSource%
Loop, *.aif, , 1
{
    CopySourcePattern = %A_LoopFileLongPath%
    CopyDest = %BrowseDest%
    copy_it = n
    IfNotExist, %CopyDest%\%A_LoopFileName%  ; Always copy if target file doesn't yet exist.
        copy_it = y
    else
    {
        FileGetTime, time, %CopyDest%\%A_LoopFileName%
        EnvSub, time, %A_LoopFileTimeModified%, seconds  ; Subtract the source file's time from the destination's.
        if time < 0  ; Source file is newer than destination file.
            copy_it = y
    }
    if copy_it = y
    {
        FileCopy, %A_LoopFileLongPath%, %CopyDest%\%A_LoopFileName%, 1   ; Copy with overwrite=yes
        if ErrorLevel
            MsgBox, Could not copy "%A_LoopFileFullPath%" to "%CopyDest%\%A_LoopFileName%".
    }
    FileDelete, %A_LoopFileLongPath%
}

IniRead, BrowseSource, %A_AppData%\BackupVideo\music2.ini, Sourcefolder, key
IniRead, BrowseDest, %A_AppData%\BackupVideo\music2.ini, Destinationfolder, key
SetWorkingDir, %BrowseSource%
Loop, *.wav, , 1
{
    CopySourcePattern = %A_LoopFileLongPath%
    CopyDest = %BrowseDest%
    copy_it = n
    IfNotExist, %CopyDest%\%A_LoopFileName%  ; Always copy if target file doesn't yet exist.
        copy_it = y
    else
    {
        FileGetTime, time, %CopyDest%\%A_LoopFileName%
        EnvSub, time, %A_LoopFileTimeModified%, seconds  ; Subtract the source file's time from the destination's.
        if time < 0  ; Source file is newer than destination file.
            copy_it = y
    }
    if copy_it = y
    {
        FileCopy, %A_LoopFileLongPath%, %CopyDest%\%A_LoopFileName%, 1   ; Copy with overwrite=yes
        if ErrorLevel
            MsgBox, Could not copy "%A_LoopFileFullPath%" to "%CopyDest%\%A_LoopFileName%".
    }
    FileDelete, %A_LoopFileLongPath%
}
SetWorkingDir, %BrowseSource%
Loop, *.mp3, , 1
{
    CopySourcePattern = %A_LoopFileLongPath%
    CopyDest = %BrowseDest%
    copy_it = n
    IfNotExist, %CopyDest%\%A_LoopFileName%  ; Always copy if target file doesn't yet exist.
        copy_it = y
    else
    {
        FileGetTime, time, %CopyDest%\%A_LoopFileName%
        EnvSub, time, %A_LoopFileTimeModified%, seconds  ; Subtract the source file's time from the destination's.
        if time < 0  ; Source file is newer than destination file.
            copy_it = y
    }
    if copy_it = y
    {
        FileCopy, %A_LoopFileLongPath%, %CopyDest%\%A_LoopFileName%, 1   ; Copy with overwrite=yes
        if ErrorLevel
            MsgBox, Could not copy "%A_LoopFileFullPath%" to "%CopyDest%\%A_LoopFileName%".
    }
    FileDelete, %A_LoopFileLongPath%
}
SetWorkingDir, %BrowseSource%
Loop, *.flac, , 1
{
    CopySourcePattern = %A_LoopFileLongPath%
    CopyDest = %BrowseDest%
    copy_it = n
    IfNotExist, %CopyDest%\%A_LoopFileName%  ; Always copy if target file doesn't yet exist.
        copy_it = y
    else
    {
        FileGetTime, time, %CopyDest%\%A_LoopFileName%
        EnvSub, time, %A_LoopFileTimeModified%, seconds  ; Subtract the source file's time from the destination's.
        if time < 0  ; Source file is newer than destination file.
            copy_it = y
    }
    if copy_it = y
    {
        FileCopy, %A_LoopFileLongPath%, %CopyDest%\%A_LoopFileName%, 1   ; Copy with overwrite=yes
        if ErrorLevel
            MsgBox, Could not copy "%A_LoopFileFullPath%" to "%CopyDest%\%A_LoopFileName%".
    }
    FileDelete, %A_LoopFileLongPath%
}
SetWorkingDir, %BrowseSource%
Loop, *.aif, , 1
{
    CopySourcePattern = %A_LoopFileLongPath%
    CopyDest = %BrowseDest%
    copy_it = n
    IfNotExist, %CopyDest%\%A_LoopFileName%  ; Always copy if target file doesn't yet exist.
        copy_it = y
    else
    {
        FileGetTime, time, %CopyDest%\%A_LoopFileName%
        EnvSub, time, %A_LoopFileTimeModified%, seconds  ; Subtract the source file's time from the destination's.
        if time < 0  ; Source file is newer than destination file.
            copy_it = y
    }
    if copy_it = y
    {
        FileCopy, %A_LoopFileLongPath%, %CopyDest%\%A_LoopFileName%, 1   ; Copy with overwrite=yes
        if ErrorLevel
            MsgBox, Could not copy "%A_LoopFileFullPath%" to "%CopyDest%\%A_LoopFileName%".
    }
    FileDelete, %A_LoopFileLongPath%
}

Loop, E:\Media\*, 2, 1
  FL .= ((FL<>"") ? "`n" : "" ) A_LoopFileFullPath
Sort, FL, R D`n ; Arrange folder-paths inside-out
Loop, Parse, FL, `n
{
  FileRemoveDir, %A_LoopField% ; Do not remove the folder unless is  empty
  If ! ErrorLevel
       Del := Del+1,  RFL .= ((RFL<>"") ? "`n" : "" ) A_LoopField
}

Loop, D:\Downloads\*, 2, 1
  FL .= ((FL<>"") ? "`n" : "" ) A_LoopFileFullPath
Sort, FL, R D`n ; Arrange folder-paths inside-out
Loop, Parse, FL, `n
{
  FileRemoveDir, %A_LoopField% ; Do not remove the folder unless is  empty
  If ! ErrorLevel
       Del := Del+1,  RFL .= ((RFL<>"") ? "`n" : "" ) A_LoopField
}


Code:
Loop, E:\Media\*, 2, 1
  FL .= ((FL<>"") ? "`n" : "" ) A_LoopFileFullPath
Sort, FL, R D`n ; Arrange folder-paths inside-out
Loop, Parse, FL, `n
{
  FileRemoveDir, %A_LoopField% ; Do not remove the folder unless is  empty
  If ! ErrorLevel
       Del := Del+1,  RFL .= ((RFL<>"") ? "`n" : "" ) A_LoopField
}

Loop, D:\Downloads\*, 2, 1
  FL .= ((FL<>"") ? "`n" : "" ) A_LoopFileFullPath
Sort, FL, R D`n ; Arrange folder-paths inside-out
Loop, Parse, FL, `n
{
  FileRemoveDir, %A_LoopField% ; Do not remove the folder unless is  empty
  If ! ErrorLevel
       Del := Del+1,  RFL .= ((RFL<>"") ? "`n" : "" ) A_LoopField
}


Here's the snippet of code (thanks to daorc) which I've been trying to modify for deleting the almost empty folders:

Code:
SetBatchLines, -1  ; Make the operation run at maximum speed.

folders_deleted = 0
FileSelectFolder, WhichFolder  ; Ask the user to pick a folder.
Loop, %WhichFolder%\*, 2 ;,1
   {
   currentfolder:=A_LoopFileFullPath
   FolderSizeKB = 0
   Loop, %CurrentFolder%\*.*, , 1
      {
      FolderSizeKB += %A_LoopFileSizeKB%
      }
   ;msgbox %CurrentFolder%`n%FolderSizeKB%
   if FolderSizeKB < 100
      {
      ;msgbox
      fileremovedir,%CurrentFolder%,1
      folders_deleted+=1
      }
   }
msgbox Deleted %folders_deleted% folders.
return


Ps. If anyone can tell me how to incorperate multiple file extensions in a single loop it would be much appreciated because this seems a bit long?
Back to top
View user's profile Send private message
Guest






PostPosted: Sat Nov 14, 2009 10:30 am    Post subject: Reply with quote

inside a folder loop *.* for all extension, u can add the following
Code:
if (A_LoopFileExt = "bmp" or A_LoopFileExt = "jpg" or A_LoopFileExt = "png")
{
msgbox, action here
}

to work only with the specific ext
Back to top
bpmb



Joined: 13 Sep 2009
Posts: 22

PostPosted: Sat Nov 14, 2009 11:15 am    Post subject: Reply with quote

Thanks! I'll give that a try! Anyone any ideas on the FoldeSize?
Back to top
View user's profile Send private message
bpmb



Joined: 13 Sep 2009
Posts: 22

PostPosted: Sat Nov 14, 2009 7:39 pm    Post subject: Reply with quote

Thanks! It made my script a WHOLE lot shorter!

For anyone interested:

Code:
SetBatchLines, -1
IniRead, BrowseSource, %A_AppData%\BackupVideo\config.ini, Sourcefolder, key
IniRead, BrowseDest, %A_AppData%\BackupVideo\config.ini, Destinationfolder, key
SetWorkingDir, %BrowseSource%
Loop, *.*, , 1
{
   if (A_LoopFileExt = "avi" or A_LoopFileExt = "mpg" or A_LoopFileExt = "mkv" or A_LoopFileExt = "mp4" or A_LoopFileExt = "dv" or A_LoopFileExt = "mov" or A_LoopFileExt = "wmv" or A_LoopFileExt = "ts")
   {
    CopySourcePattern = %A_LoopFileLongPath%
    CopyDest = %BrowseDest%
    copy_it = n
    IfNotExist, %CopyDest%\%A_LoopFileName%  ; Always copy if target file doesn't yet exist.
        copy_it = y
    else
    {
        FileGetTime, time, %CopyDest%\%A_LoopFileName%
        EnvSub, time, %A_LoopFileTimeModified%, seconds  ; Subtract the source file's time from the destination's.
        if time < 0  ; Source file is newer than destination file.
            copy_it = y
    }
    if copy_it = y
    {
        FileCopy, %A_LoopFileLongPath%, %CopyDest%\%A_LoopFileName%, 1   ; Copy with overwrite=yes
        if ErrorLevel
            MsgBox, Could not copy "%A_LoopFileFullPath%" to "%CopyDest%\%A_LoopFileName%".
    }
    FileDelete, %A_LoopFileLongPath%
}
}


Now only still the almost empy folder copy thingy..
Back to top
View user's profile Send private message
bpmb



Joined: 13 Sep 2009
Posts: 22

PostPosted: Wed Nov 18, 2009 7:38 pm    Post subject: Reply with quote

I figured it out, sometimes I just need some distraction to clear my head Very Happy

For anyone looking to do the same here my code, removes folders containing files with a total of less than 100 kB in size:

Code:
SetWorkingDir E:\Media
Loop, *.*, 2,
{
  Size = 0
  Index := A_LoopFileLongPath
  Loop, %Index%\*.*, , 1
  {
    Size += %A_LoopFileSize%
}
If Size < 100000
FileRemoveDir, %A_LoopFileLongPath%, 1
}


So simple really but I never made a nested loop before Wink
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    AutoHotkey Community Forum Index -> Ask for Help All times are GMT
Page 1 of 1

 
Jump to:  
You can post new topics in this forum
You can reply to topics in this forum


Powered by phpBB © 2001, 2005 phpBB Group