Solved

Script to extract files from folders

Posted on 2013-06-24
5
324 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 51

Expert Comment

by:Bill Prew
Comment Utility
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
Comment Utility
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
Comment Utility
So I can provide the path to the root folder for it to start its scanning of everything within that root folder.
0
 
LVL 51

Expert Comment

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

~bp
0
 
LVL 51

Accepted Solution

by:
Bill Prew earned 500 total points
Comment Utility
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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

This script will sweep a range of IP addresses (class c only, 255.255.255.0) and report to a log the version of office installed. What it does: 1.)      Creates log file in the directory the script is run from (if it doesn't already exist) 2.)      Sweep…
Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
Learn the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

772 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now