Solved

Extract  Picutres from directory structure to One folder

Posted on 2004-09-28
4
133 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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Computer crashes, following error message in event manager 5 75
MsgBox 2 36
using Access 8 52
vb6 connector to mongodb 2 26
Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

708 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

19 Experts available now in Live!

Get 1:1 Help Now