Solved

Script to extract files from folders

Posted on 2013-06-24
5
341 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 55

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 55

Expert Comment

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

~bp
0
 
LVL 55

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

Forrester Webinar: xMatters Delivers 261% ROI

Guest speaker Dean Davison, Forrester Principal Consultant, explains how a Fortune 500 communication company using xMatters found these results: Achieved a 261% ROI, Experienced $753,280 in net present value benefits over 3 years and Reduced MTTR by 91% for tier 1 incidents.

Question has a verified solution.

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

Not long ago I saw a question in the VB Script forum that I thought would not take much time. You can read that question (Question ID  (http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/Q_28455246.html)28455246) Here (http…
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.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

689 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