1. Computer problem? Tech Support Guy is completely free -- paid for by advertisers and donations. Click here to join today! If you're new to Tech Support Guy, we highly recommend that you visit our Guide for New Members.

Solved: help :) Script for Moving jpg files from multiple directories into one

Discussion in 'Software Development' started by westie1871, Aug 17, 2011.

Thread Status:
Not open for further replies.
Advertisement
  1. westie1871

    westie1871 Thread Starter

    Joined:
    Apr 12, 2011
    Messages:
    16
    Hi Folks,
    i wonder if anyone could help me / point me in the right direction.

    i am looking for a script that will allow me to run as a scheduled service that will scan multiple directories for *.jpg files and move them to one directory.

    thanks in advance :)
     
  2. Squashman

    Squashman Trusted Advisor

    Joined:
    Apr 4, 2003
    Messages:
    19,786
    When you say multiple directories do you mean:
    C:\Images
    C:\Pictures

    Or is their a common source directory like C:\Images with a bunch of sub directories under that.
    H:\IMAGES
    ├───birthday
    └───christmas
     
  3. westie1871

    westie1871 Thread Starter

    Joined:
    Apr 12, 2011
    Messages:
    16
    Hi Squashman,

    it would be c:\images
    c:\pictures
    etc and maybe some of them will be c:\images\date\
     
  4. Keebellah

    Keebellah Trusted Advisor

    Joined:
    Mar 27, 2008
    Messages:
    6,601
    First Name:
    Hans
    I found a vbs script at this site
    http://www.tek-tips.com/viewthread.cfm?qid=947098&page=1

    Code:
    Dim objFSO, objFolder, colFiles, objSubFolder, objSubFile
    strFolder = "J:\Temporary"
    strTar = "P:\_2Burn\"
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    Set objFolder = objFSO.GetFolder(strFolder)
    Set colFiles = objFolder.Files
    
    For Each File in colFiles
    set objFile = objFSO.GetFile(strFolder & "\" & File.Name)
    Next
    
    If lcase(Right(objFile.Name,3)) = "jpg" Then
    objFSO.CopyFile objFile,(strTar)
    objFSO.DeleteFile objFile, true
    End If
    
    ScanSubFolders(objFolder)
    
    Sub ScanSubFolders(objFolder)
    
    Set colFolders = objFolder.SubFolders
    	For Each objSubFolder In colFolders
    		Set colFiles = objSubFolder.Files
    		For Each objFile in Colfiles
    			If lcase(Right(objFile.Name,3)) = "jpg" Then
    				objFSO.CopyFile objFile,(strTar)
    				objFSO.DeleteFile objFile, true
    			End If
    		Next
    		ScanSubFolders(objSubFolder)
    	Next
    End Sub
    
    strComputer="."
    set objWMIService = GetObject("winmgmts:\\" & strComputer)
    Set colOperatingSystems = objWMIService.ExecQuery _
             ("Select * from Win32_OperatingSystem")
    
    For Each objOS in colOperatingSystems
    	dtmLastBootUpTime = ConvWMIDateTime(objOS.LocalDateTime, "ISO8601")
    	WScript.Echo "Completed at " & dtmLastBootUpTime
    Next
    
    Function ConvWMIDateTime(sDMTFformat, iNamedFormat)
    
       Dim sYear, sMonth, sDay, sHour, sMinutes, sSeconds
       sYear = mid(sDMTFformat, 1, 4)
       sMonth = mid(sDMTFformat, 5, 2)
       sDay = mid(sDMTFformat, 7, 2)
       sHour = mid(sDMTFformat, 9, 2)
       sMinutes = mid(sDMTFformat, 11, 2)
       sSeconds = mid(sDMTFformat, 13, 2)
    
       ConvWMIDateTime = sHour & ":" & sMinutes & ":" & sSeconds & " " _
                    & sMonth & "-" & sDay & "-" & sYear
    
       If IsNumeric(iNamedFormat) Then
         If iNamedFormat >= 0 And iNamedFormat <= 4 Then
           ' FormatDateTime will set date format to specified format
           ConvWMIDateTime = FormatDateTime(ConvWMIDateTime, iNamedFormat)
         End If
       End If
    End Function 
    
    It works for the variables I entered:

    strFolder = "J:\Temporary"
    strTar = "P:\_2Burn\"

    It starts in J:\Temporary and searches thorugh all the folders and sub folders of a file with the extension jpg

    The srtTar is where it will move the found files to

    It's just a question of editting if need another extension and make sure the target folder is not include in the loop.
    If it's on the same disk you will have to include a line of code testing for that folder to skip it.
     
  5. westie1871

    westie1871 Thread Starter

    Joined:
    Apr 12, 2011
    Messages:
    16
    Hi Hans,

    ive tested this and it almost works if you create a folder under j:\temporary i get and place an jpg image in there i get a script error: line 12 char 1 error: object required `objfile` code 800a01a8
     
  6. Keebellah

    Keebellah Trusted Advisor

    Joined:
    Mar 27, 2008
    Messages:
    6,601
    First Name:
    Hans
    Do you have a folder named J:\Temporary ?

    I mean you have to edit it to your requirements, this is just the sample working on my structure.
     
  7. westie1871

    westie1871 Thread Starter

    Joined:
    Apr 12, 2011
    Messages:
    16
    Hi Mate yeh i changed it to mine, i just used that so you would understand the error ;)
     
  8. Keebellah

    Keebellah Trusted Advisor

    Joined:
    Mar 27, 2008
    Messages:
    6,601
    First Name:
    Hans
    Dit you put a backslash at the end of the foleder name?
    That is not allowed (as you could have noticed from my posting)
    If it still doesn't work could you post your editted version?
     
  9. westie1871

    westie1871 Thread Starter

    Joined:
    Apr 12, 2011
    Messages:
    16
    Dim objFSO, objFolder, colFiles, objSubFolder, objSubFile
    strFolder = "k:\images"
    strTar = "c:\scan\"
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    Set objFolder = objFSO.GetFolder(strFolder)
    Set colFiles = objFolder.Files
    For Each File in colFiles
    set objFile = objFSO.GetFile(strFolder & "\" & File.Name)
    Next
    If lcase(Right(objFile.Name,3)) = "jpg" Then
    objFSO.CopyFile objFile,(strTar)
    objFSO.DeleteFile objFile, true
    End If
    ScanSubFolders(objFolder)
    Sub ScanSubFolders(objFolder)
    Set colFolders = objFolder.SubFolders
    For Each objSubFolder In colFolders
    Set colFiles = objSubFolder.Files
    For Each objFile in Colfiles
    If lcase(Right(objFile.Name,3)) = "jpg" Then
    objFSO.CopyFile objFile,(strTar)
    objFSO.DeleteFile objFile, true
    End If
    Next
    ScanSubFolders(objSubFolder)
    Next
    End Sub
    strComputer="."
    set objWMIService = GetObject("winmgmts:\\" & strComputer)
    Set colOperatingSystems = objWMIService.ExecQuery _
    ("Select * from Win32_OperatingSystem")
    For Each objOS in colOperatingSystems
    dtmLastBootUpTime = ConvWMIDateTime(objOS.LocalDateTime, "ISO8601")
    WScript.Echo "Completed at " & dtmLastBootUpTime
    Next
    Function ConvWMIDateTime(sDMTFformat, iNamedFormat)
    Dim sYear, sMonth, sDay, sHour, sMinutes, sSeconds
    sYear = mid(sDMTFformat, 1, 4)
    sMonth = mid(sDMTFformat, 5, 2)
    sDay = mid(sDMTFformat, 7, 2)
    sHour = mid(sDMTFformat, 9, 2)
    sMinutes = mid(sDMTFformat, 11, 2)
    sSeconds = mid(sDMTFformat, 13, 2)
    ConvWMIDateTime = sHour & ":" & sMinutes & ":" & sSeconds & " " _
    & sMonth & "-" & sDay & "-" & sYear
    If IsNumeric(iNamedFormat) Then
    If iNamedFormat >= 0 And iNamedFormat <= 4 Then
    ' FormatDateTime will set date format to specified format
    ConvWMIDateTime = FormatDateTime(ConvWMIDateTime, iNamedFormat)
    End If
    End If
    End Function
     
  10. Keebellah

    Keebellah Trusted Advisor

    Joined:
    Mar 27, 2008
    Messages:
    6,601
    First Name:
    Hans
    The code works fine here, althoug I had to change the drivelteers.
    Could it be that you do not have enough rigths to delete the file?
    Line 12 is objFSO.DeleteFile objFile, true

    If you do not have the rigths it thows the error, there is no error trapping here.

    You could add two lines of code (the red lines


    On Error resume next
    objFSO.DeleteFile objFile, true
    On Error GoTo 0
     
  11. westie1871

    westie1871 Thread Starter

    Joined:
    Apr 12, 2011
    Messages:
    16
    Hi Hans,
    what os are you using? i have full permissions on the directories did you try making a directory and putting jpgs in it under the k:\images then k:\images\test\1.jpg k:\images\test3\2.jpg etc
     
  12. Keebellah

    Keebellah Trusted Advisor

    Joined:
    Mar 27, 2008
    Messages:
    6,601
    First Name:
    Hans
    No OS problem,
    just a mistake
    Code:
    Dim objFSO, objFolder, colFiles, objSubFolder, objSubFile
    strFolder = "k:\images"
    strTar = "c:\scan\"
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    Set objFolder = objFSO.GetFolder(strFolder)
    Set colFiles = objFolder.Files
    For Each File in colFiles
    set objFile = objFSO.GetFile(strFolder & "\" & File.Name)
    If lcase(Right(objFile.Name,3)) = "jpg" Then
    objFSO.CopyFile objFile,(strTar)
    objFSO.DeleteFile objFile, true
    End If
    [B][COLOR="Red"]Next[/COLOR][/B]
    ScanSubFolders(objFolder)
    Sub ScanSubFolders(objFolder)
    Set colFolders = objFolder.SubFolders
    For Each objSubFolder In colFolders
    Set colFiles = objSubFolder.Files
    For Each objFile in Colfiles
    If lcase(Right(objFile.Name,3)) = "jpg" Then
    objFSO.CopyFile objFile,(strTar)
    objFSO.DeleteFile objFile, true
    End If
    Next
    ScanSubFolders(objSubFolder)
    Next
    End Sub
    strComputer="."
    set objWMIService = GetObject("winmgmts:\\" & strComputer)
    Set colOperatingSystems = objWMIService.ExecQuery _
    ("Select * from Win32_OperatingSystem")
    For Each objOS in colOperatingSystems
    dtmLastBootUpTime = ConvWMIDateTime(objOS.LocalDateTime, "ISO8601")
    WScript.Echo "Completed at " & dtmLastBootUpTime
    Next
    Function ConvWMIDateTime(sDMTFformat, iNamedFormat)
    Dim sYear, sMonth, sDay, sHour, sMinutes, sSeconds
    sYear = mid(sDMTFformat, 1, 4)
    sMonth = mid(sDMTFformat, 5, 2)
    sDay = mid(sDMTFformat, 7, 2)
    sHour = mid(sDMTFformat, 9, 2)
    sMinutes = mid(sDMTFformat, 11, 2)
    sSeconds = mid(sDMTFformat, 13, 2)
    ConvWMIDateTime = sHour & ":" & sMinutes & ":" & sSeconds & " " _
    & sMonth & "-" & sDay & "-" & sYear
    If IsNumeric(iNamedFormat) Then
    If iNamedFormat >= 0 And iNamedFormat <= 4 Then
    ' FormatDateTime will set date format to specified format
    ConvWMIDateTime = FormatDateTime(ConvWMIDateTime, iNamedFormat)
    End If
    End If
    End Function
    
    The Red Next was too high.
    This is the correct script, my mistake :eek: It worked on one folder but didn't go deeper
     
  13. westie1871

    westie1871 Thread Starter

    Joined:
    Apr 12, 2011
    Messages:
    16
    Hi Hans,
    is there a way off putting any logging into this into a text file? to say whats been copied from what directories? ;)
     
  14. Keebellah

    Keebellah Trusted Advisor

    Joined:
    Mar 27, 2008
    Messages:
    6,601
    First Name:
    Hans
    Everything is possible. You will have to write an extra routine that passes the information you want logged to let's say a text file saved someplace.
    You will have to specify exactly:
    1. Log file location
    2. Log file name
    3. Actions that need to be logged
    4. Create new log for every time the script is run or one cumulative log
    etc etc
     
  15. Keebellah

    Keebellah Trusted Advisor

    Joined:
    Mar 27, 2008
    Messages:
    6,601
    First Name:
    Hans
    I noticed you haven't answrede but I went on and editted the vbs a little more.
    It will now prompt for 4 parameters:
    1. Source disk/folder
    2. Target disk/folder
    3. File extension to look for (default jpg)
    4. Location where log file is to be written to

    A Dialog indicating the made choice and if you decide to continue Yes / No

    The results are loog in a logfile named yyyymmdd-hh-mm-move-ext.log

    I also added a test in case the target folder is in the same as the source to ignore it

    Just give it a run and let me know if this meets your needs.
     

    Attached Files:

  16. Sponsor

As Seen On
As Seen On...

Welcome to Tech Support Guy!

Are you looking for the solution to your computer problem? Join our site today to ask your question. This site is completely free -- paid for by advertisers and donations.

If you're not already familiar with forums, watch our Welcome Guide to get started.

Join over 733,556 other people just like you!

Loading...
Thread Status:
Not open for further replies.

Short URL to this thread: https://techguy.org/1012925

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice