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?