Solved

Extract  Picutres from directory structure to One folder

Posted on 2004-09-28
4
136 Views
Last Modified: 2010-05-02
vb6 sp5
win 2000 sp4

I need a routine that will copy ALL pictures .jpg  files from a folder that has Many subfolders and place the jpg. in ONE folder

Ther .jpg files start in a folder called  c:\pics\

I need to place ALL the jpg  files in a folder called  c:\Newjpg\


Thanks
fordraiders
0
Comment
Question by:fordraiders
  • 2
4 Comments
 
LVL 16

Expert Comment

by:JohnBPrice
ID: 12173608
in DOS, "XCOPY  C:\Source\*.jpg C:\Target\ /S"

The "/S" traverses all sub directories
0
 
LVL 16

Expert Comment

by:JohnBPrice
ID: 12173625
oops, that preserves the folder structure.
0
 
LVL 76

Expert Comment

by:David Lee
ID: 12173669
Here's a VBScript that'll do this.  If you want straight VB, then it'd be very easy to convert this.

Usage: CopyJPGs.vbs SourcePath
Example:  CopyJPGs.vbs C:\pics

---- Script CopyJPGs.Vbs
Dim objFSO, _
    intFolders, _
    intFiles, _
    strStartingPath

    strStartingPath = Wscript.Arguments.Item(0)
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    ProcessFolder strStartingPath
    Set objFSO = Nothing
    Wscript.Echo "Folders Processed: " & intFolders & " Files Changed: " & intFiles
    Wscript.Quit

Sub ProcessFolder(strFolderPath)
    Dim objFolder, _
        objSubFolders
    'Create the starting folder object
    Set objFolder = objFSO.GetFolder(strFolderPath)
    'Call the ProcessFiles subroutine and pass it the folder object
    ProcessFiles objFolder
    intFolders = intFolders + 1
    'Create the sub-folders object
    Set objSubFolders = objFolder.SubFolders
    'Loop through the sub-folders in the folder object
    For Each objFolder In objSubFolders
        'Call the ProcessFiles subroutine and pass it the sub-folder object
        ProcessFolder objFolder.Path
    Next
    'Dispose of the created objects
    Set objSubFolders = Nothing
    Set objFolder = Nothing
End Sub

Sub ProcessFiles(objFolder)
    Dim objFile, _
        strFileType
    'Loop through the files in the folder object
    For Each objFile In objFolder.Files
        strFileType = LCase(objFSO.GetExtensionName(objFile.Name))
        If strFileType = "jpg" Then
            objFSO.CopyFile objFile.Path, "C:\NewJpg", True
            intFiles = intFiles + 1
        End If
    Next
    'Dispose of the object created in this subroutine
    Set objFile = Nothing
End Sub
0
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 500 total points
ID: 12173698
Here is another way to do it:

Option Explicit

Private Sub Command1_Click()
    moveFiles "c:\pics\", "*.jpg", "c:\Newjpg\"
    MsgBox "Done"
End Sub

Private Sub moveFiles(ByVal basePath As String, ByVal filter As String, ByVal targetPath As String)
    Dim curFile As String
    Dim subdir As Variant
    Dim subDirs As Collection
       
    ' copy the files
    curFile = Dir(basePath)
    Do
        If curFile <> "" Then
            If curFile Like filter Then
                FileCopy basePath & curFile, targetPath & curFile
            End If
        End If
        DoEvents
        curFile = Dir()
    Loop While curFile <> ""
   
    ' build subDirs collection
    Set subDirs = New Collection
    curFile = Dir(basePath, vbDirectory)
    Do
        If curFile <> "" Then
            If (GetAttr(basePath & curFile) And vbDirectory) = vbDirectory Then
                If curFile <> "." And curFile <> ".." Then
                    subDirs.Add basePath & curFile & "\"
                End If
            End If
        End If
        DoEvents
        curFile = Dir()
    Loop While curFile <> ""
   
    ' recurse into each subdir
    For Each subdir In subDirs
        moveFiles subdir, filter, targetPath
    Next
End Sub
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

809 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