[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 347
  • Last Modified:

Script to extract files from folders

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
murryc
Asked:
murryc
  • 3
  • 2
1 Solution
 
Bill PrewCommented:
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
 
murrycAuthor Commented:
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
 
murrycAuthor Commented:
So I can provide the path to the root folder for it to start its scanning of everything within that root folder.
0
 
Bill PrewCommented:
Okay, great, that's easy enough, working that up now...

~bp
0
 
Bill PrewCommented:
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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now