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



## westie1871 (Apr 12, 2011)

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


----------



## Squashman (Apr 4, 2003)

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
&#9500;&#9472;&#9472;&#9472;birthday
&#9492;&#9472;&#9472;&#9472;christmas


----------



## westie1871 (Apr 12, 2011)

Hi Squashman,

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


----------



## Keebellah (Mar 27, 2008)

I found a vbs script at this site
http://www.tek-tips.com/viewthread.cfm?qid=947098&page=1


```
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.


----------



## westie1871 (Apr 12, 2011)

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


----------



## Keebellah (Mar 27, 2008)

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.


----------



## westie1871 (Apr 12, 2011)

Hi Mate yeh i changed it to mine, i just used that so you would understand the error


----------



## Keebellah (Mar 27, 2008)

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?


----------



## westie1871 (Apr 12, 2011)

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


----------



## Keebellah (Mar 27, 2008)

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*


----------



## westie1871 (Apr 12, 2011)

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


----------



## Keebellah (Mar 27, 2008)

No OS problem,
just a mistake

```
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  It worked on one folder but didn't go deeper


----------



## westie1871 (Apr 12, 2011)

Hi Hans,
is there a way off putting any logging into this into a text file? to say whats been copied from what directories?


----------



## Keebellah (Mar 27, 2008)

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


----------



## Keebellah (Mar 27, 2008)

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.


----------



## Keebellah (Mar 27, 2008)

I gave the vbs script I wrote for you a through test to see if it does everything I expevted it to do and came accross some issue which I think are interesting and have implemented in the attached version
1. I added the option to ignore Sub-folders, default is set to true but I can imagine that you do want a specific folder scanned and not all the sub-folders of that folder
2. I added a check to see if a file with the same name is already present in the target folder, if it is, the move action will not be carried out but will be mentioned in the logfile
See if it works for you.

I think it looks fine now.

Will you let me know?


----------



## westie1871 (Apr 12, 2011)

hi Hans,
this works a treat ;p


----------



## Keebellah (Mar 27, 2008)

Don't forget to mark it sloved


----------

