Jump to content
Sign in to follow this  
tfehring

Trouble with script to automatically move files by type

Recommended Posts

tfehring

Or, "I cobbled this together last night and it seems like it should work but it doesn't."

The intended purpose is to check the Downloads directory for music or video files (or subfolders that contain them) and move either the files themselves or the folders containing them to the appropriate directories, keeping the same hierarchy in the destination folder if applicable.

I realize that it shouldn't work at this point if the media files are more than one subdirectory deep, but I'll extend my script to handle that once I've got it working in the simpler case.

 
Right now, if I run the script, the tray icon just flashes briefly (kinda makes it hard to tray debug) and nothing happens with the test files I have in the downloads directory. 
 
Any help would be appreciated. Thanks in advance!  :)

 

$mainPath = "D:\Downloads"
$copyPathMusic = "D:\Music"
$copyPathVideo = "D:\Videos"
$firstSearch = FileFindFirstFile($mainPath&"\*")
If $firstSearch <> -1 Then
   While 1
 $firstFile = FileFindNextFile($firstSearch)
 If @error Then
Exit
;Note that incomplete downloads get stored in Downloads\Pending
 ElseIf @extended And $firstFile <> "Pending" Then
$secondSearch = FileFindFirstFile($firstFile&"\*")
If $secondSearch <> -1 Then
While 1
  $secondFile = FileFindNextFile($secondSearch)
  If $secondFile = "*.mp3" Or $secondFile = "*.wma" Or $secondFile = "*.flac" Then
 If DirCopy($mainPath&"\"&$firstFile,$copyPathMusic"\"&$firstFile) = 1 Then
DirRemove($mainPath&"\"&$firstFile,1)
 EndIf
  ElseIf $secondFile = "*.mp4" Or $secondFile = "*.avi" Or $secondFile = "*.mpg" Or $secondFile = "*.wmv" Or $secondFile = "*.mov" Or $secondFile = "*.m4v" Or $secondFile = "*.flv" Then
 If DirCopy($mainPath&"\"&$firstFile,$copyPathVideo"\"&$firstFile) = 1 Then
DirRemove($mainPath&"\"&$firstFile,1)
 EndIf
  EndIf
WEnd
EndIf
 ElseIf Not @extended Then
If $firstFile = "*.mp3" Or $secondFile = "*.wma" Or $secondFile = "*.flac" Then
If FileCopy($mainPath&"\"&$firstFile,$copyPathMusic"\"&$firstFile) = 1 Then
  FileDelete($mainPath&"\"&$firstFile)
EndIf
ElseIf $firstFile = "*.mp4" Or $secondFile = "*.avi" Or $secondFile = "*.mpg" Or $secondFile = "*.wmv" Or $secondFile = "*.mov" Or $secondFile = "*.m4v" Or $secondFile = "*.flv" Then
If FileCopy($mainPath&"\"&$firstFile,$copyPathVideo"\"&$firstFile) = 1 Then
  FileDelete($mainPath&"\"&$firstFile)
EndIf
EndIf
 EndIf
   WEnd
Else
   Exit
EndIf

Share this post


Link to post
Share on other sites
Melba23

tfehring,

I have a real aversion to debugging FileFindFirst/NextFile scripts when there are functions within AutoIt to do all the donkey work for you. ;)

If the England game tonight is not too interesting I will work on an example for you - if it is a good match you may have to wait until tomorrow! :D

M23

  • Like 1

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites
ravaged1

You might want to just use robocopy.

Share this post


Link to post
Share on other sites
mrider

As posted, lines 17, 21, 29, and 33 are all missing an ampersand ("&").   My ampersands are red where yours are missing...

Line 17: If DirCopy($mainPath&""&$firstFile,$copyPathMusic&""

Line 21: If DirCopy($mainPath&""&$firstFile,$copyPathVideo&""

Line 29: If FileCopy($mainPath&""&$firstFile,$copyPathMusic&""

Line 33: If FileCopy($mainPath&""&$firstFile,$copyPathVideo&""

 

Is this code exactly what you are running?  You might consider running your code from inside SciTE in the future, the console will point out the exact issues.


How's my riding? Dial 1-800-Wait-There

Trying to use a computer with McAfee installed is like trying to read a book at a rock concert.

Share this post


Link to post
Share on other sites
tfehring

tfehring,

I have a real aversion to debugging FileFindFirst/NextFile scripts when there are functions within AutoIt to do all the donkey work for you. ;)

If the England game tonight is not too interesting I will work on an example for you - if it is a good match you may have to wait until tomorrow! :D

M23

 

Yeah, I'm new to AutoIt (I've successfully written one script, and that was months ago and with a completely different purpose) and I dislike them already. I just Googled applications similar to the one I'm going for, and they came up often and seemed like they'd fit reasonably well. Meh.

 

You might want to just use robocopy.

 

Wouldn't I still need to use FileFindFirst/NextFile or something similar to check the file types though? I mean...

robocopy $mainPath&"\"&$subdirectory" $copyPathMusic&"\"&$subdirectory /mov 

 

would (should?) work the same as the copying functions I have now, but I still need to check the file extension to determine whether to move to $copyPathMusic or $copyPathVideo. I can't just use

/xf *.avi *.mpg *.wmv *.mov *.m4v *.flv

 

(or whatever the correct syntax for that would look like) because there's also the possibility of folders that contain neither music nor videos, which I just want to keep in D:Downloads. And I can't imagine

/xf Not *.mp3 Not *.wma Not *.flac

 

(again, just pretend I didn't butcher the syntax  :shifty:) would really work either.

Share this post


Link to post
Share on other sites
tfehring

As posted, lines 17, 21, 29, and 33 are all missing an ampersand ("&").   My ampersands are red where yours are missing...

Line 17: If DirCopy($mainPath&""&$firstFile,$copyPathMusic&""

Line 21: If DirCopy($mainPath&""&$firstFile,$copyPathVideo&""

Line 29: If FileCopy($mainPath&""&$firstFile,$copyPathMusic&""

Line 33: If FileCopy($mainPath&""&$firstFile,$copyPathVideo&""

 

Is this code exactly what you are running?  You might consider running your code from inside SciTE in the future, the console will point out the exact issues.

 

I can't believe I missed that, though I added the ampersands and it didn't seem to make a difference.

But yes, this is exactly what I'm running, other than the fact that I added the operator for tray icon debugging.

This probably isn't the root of the problem, but the @extended flags just show up in black in the editor, while @error shows up in pink. Anything wrong with my syntax there?

Share this post


Link to post
Share on other sites
kylomas

tfehring,

Perhaps I can save M23 some trouble.  First you need the "RecFileListToArray" UDF which can be downloaded from >here.

Once you have the UDF you can use a construct like the following to move your files...

#include <recfilelisttoarray.au3>
#include <array.au3>

local $lPath = 'c:\users\admin010\downloads'                            ; my DL folder, change to yours, obviously
local $fl_type                                                          ; will contain file extention from SRE

$aArray = _RecFileListToArray($lPath, "*",1,1)                          ; lists all files in $lPath and subfolders

;_arraydisplay($aArray)                                                 ; display the array of downloaded files

for $1 = 0 to ubound($aArray) - 1                                       ; loop through every file
    $fl_type = stringregexpreplace($aArray[$1],'.*\.(.*)','$1')         ; set $fl_type to the file extention
    switch $fl_type                                                     ; if file extention is...
        case 'mp3', 'wma', 'flac'                                       ;    one of these then...
            ;
            ; do your move to music routine
            ;
        case 'mp4', 'avi', 'mpg', 'wmv', 'mov', 'm4v', 'flv'            ;    or one of these then...
            ;
            ; do your movie move routine
            ;
    endswitch
Next

kylomas

edit: additional info

You can get the file name (minus path) like this

$fl_name = stringregexpreplace($aArray[$1],'.*\\(.*)','$1')         ; set $fl_name to the file name
Edited by kylomas

Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Share this post


Link to post
Share on other sites
ravaged1

 

Wouldn't I still need to use FileFindFirst/NextFile or something similar to check the file types though? 

Sorry, Robocopy has nothing to do with Autoit at all, though you could call it from an Autoit script.

Robocopy (the name is short for Robust File Copy) was introduced with the Windows Server 2003 Resource Kit and is included in all editions of Windows 7

Just open a cmd prompt and type robocopy /? Then make a batch file to move the files you want.

Edited by ravaged1

Share this post


Link to post
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
Sign in to follow this  

×