Solved

Script to extract files from folders

Posted on 2013-06-24
5
337 Views
Last Modified: 2013-06-24
I need a turnkey Windows script that will scan a bunch of folders and subfolders and extract windows media audio files *.wma and place them all into a single folder.  I am not sure how best to handle files with duplicate filenames, but I am pretty sure there are none.  Maybe the duplicates could get placed a folder named duplicate.
0
Comment
Question by:murryc
  • 3
  • 2
5 Comments
 
LVL 53

Expert Comment

by:Bill Prew
ID: 39272829
Will there need to be a list of base folders to scan into, or are you wanting to scan a single folder and all it's subfolders?

~bp
0
 

Author Comment

by:murryc
ID: 39272970
I have a single folder called 'Music' but inside of that I have 35 folders and then those 35 folders have various subfolder structures.  Some have a single subfolder, but others might have a folder within their subfolders.  So I have varying levels of subfolders.  I think the script will need to check for unlimited depths of folder levels.
0
 

Author Comment

by:murryc
ID: 39272975
So I can provide the path to the root folder for it to start its scanning of everything within that root folder.
0
 
LVL 53

Expert Comment

by:Bill Prew
ID: 39273006
Okay, great, that's easy enough, working that up now...

~bp
0
 
LVL 53

Accepted Solution

by:
Bill Prew earned 500 total points
ID: 39273109
Okay, here's a script for this job, adjust the paths as needed.

' File I/O constants
Const ForReading = 1, ForWriting = 2, ForAppending = 8

' Source and xdestination folder names, and extension of files to move
strBaseDir = "C:\EE\EE28166353\Files"
strDestDir = "C:\EE\EE28166353\Merge"
strBaseExt = ".wma"

' Log file for duplicate errors
strLogName = "C:\EE\EE28166353\logfile.txt"

' Create filesystem object
Set objFSO = CreateObject("Scripting.FileSystemObject")

' Open log file
Set objLog = objFSO.OpenTextFile(strLogName, ForWriting, True)

' Call recursive subroutine to move all files in a folder, and all child folders
MoveFiles objFSO.GetFolder(strBaseDir)

' Close log file, release objects
objLog.Close()
Set objLog = Nothing
Set objFSO = Nothing

Function MoveFiles(objFolder)
    ' Process all files in this folder
    For Each objFile in objFolder.Files
        ' Is it the extension we want to move
        If LCase(Right(objFile.Name, Len(strBaseExt))) = LCase(strBaseExt) Then
            ' Build new destination path
            strDestPath = strDestDir & "\" & objFile.Name
            ' Only move it the destination file doesn't exist, otherwise log and leave alone
            If objFSO.FileExists(strDestPath) Then
                objLog.WriteLine "Duplicate file:[" & objFile.Path & "], [" & strDestPath & "]"
            Else
                objFSO.MoveFile objFile.Path, strDestPath
            End If
        End If
    Next

    ' Process all subfolders in this folder
    For Each objSubFolder in objFolder.Subfolders
        MoveFiles objSubFolder
    Next
End Function

Open in new window

~bp
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This is pretty cool.  The purpose of this VB Script is to help you document where JAR (Java ARchive) files and specifically java class files are located so that you can address issues seen with a client or that you can speak intelligently with a dev…
Over the years I have built up my own little library of code snippets that I refer to when programming or writing a script.  Many of these have come from the web or adaptations from snippets I find on the Web.  Periodically I add to them when I come…
Learn the basics of strings in Python: declaration, operations, indices, and slicing. Strings are declared with quotations; for example: s = "string": Strings are immutable.: Strings may be concatenated or multiplied using the addition and multiplic…
Learn the basics of modules and packages in Python. Every Python file is a module, ending in the suffix: .py: Modules are a collection of functions and variables.: Packages are a collection of modules.: Module functions and variables are accessed us…

803 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question