How to copy certain files from subfolders.

Hi,

 I need help on VB Script, which must be able to copy files by file extension that you specify
(*.doc, *.docx, *.ppsm, *.ppt, *.jpg).
The script has to copy the files (not folders) in the "\\S1\Innovation 02" to "\\S1\Report 1"
 The problem is that in the "Innovation 2" is regularly 10-15 different subfolders, and these were further divided into more. I need a script to search the folder "Innovation 02" while all the subfolders with the files to copy.
Absolutely ideal would be to display a MsgBox at the end, which would see the copied files.

 Thank you in advance for your help.

Regards,

TM
Thomas_MeyerAsked:
Who is Participating?
 
yehudahaConnect With a Mentor Commented:
hey

try this

edit line 1-5 as you need
notice on line 4 not remove the backslash at the end
save as vbs and run


' start edit here
arrex = Array("msi","ppt")
objStartFolder = "\\serverToCopyFrom\tools"
objDestinationFolder = "\\ServerToCopTo\share\" ' don't remove the "\" at the end
'end edit here

Set objFSO = CreateObject("Scripting.FileSystemObject")

For Each ex In arrex
Set objFolder = objFSO.GetFolder(objStartFolder)
Set colFiles = objFolder.Files
For Each objFile in colFiles
    If Right(objfile.Name,3) = ex Then
    FileList = FileList & vbCrLf & objFile.Path
    objfso.CopyFile objFile.Path,objDestinationFolder
    End if
Next

ShowSubfolders objFSO.GetFolder(objStartFolder)
next

MsgBox "done" & vbCrLf & vbCrLf & FileList

Sub ShowSubFolders(Folder)
    For Each Subfolder in Folder.SubFolders
        Set objFolder = objFSO.GetFolder(Subfolder.Path)
        Set colFiles = objFolder.Files
        For Each objFile in colFiles
            If Right(objfile.Name,3) = ex then
            FileList = FileList & vbCrLf & objFile.Path
            objfso.CopyFile objFile.Path,objDestinationFolder
            End if
        Next
        ShowSubFolders Subfolder
    Next
End Sub

Open in new window

0
 
yehudahaCommented:
wrote something in the past that may help you, write the result to csv file
Set objshell = CreateObject("wscript.shell")
strComputer = InputBox("enter computer name to search")
strEx = InputBox("enter file extension to search, example: pst")
strcsv = InputBox("enter you csv file name and path, example: c:\file.csv")

If ping(strComputer) Then
On Error Resume next
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
If Err.Number <> 0 Then
MsgBox "error connect to " & strcomputer & " maybe permission issue"
WScript.Quit
Else
On Error Goto 0
Set colFiles = objWMIService.ExecQuery _
    ("Select * from CIM_DataFile Where Extension = '" & strEx & "'")

If colFiles.Count = 0 Then
    MsgBox "No File Found"
    Wscript.Quit
End If

objshell.Popup "Please wait while the script is processing",5

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTextFile = objFSO.CreateTextFile(strcsv)

For Each objFile in colFiles
    objTextFile.Write(objFile.Drive & objFile.Path & ",")
    objTextFile.Write(objFile.FileName & "." & objFile.Extension & ",")
    objTextFile.Write(ConvertSize(objFile.FileSize))
Next

objTextFile.Close
End if
Else
MsgBox strComputer & " not reachable"
End if

MsgBox "done"

Function ping(strComputer)
wmiQuery = "Select * From Win32_PingStatus Where Address = '" & strComputer & "'"    
Set objWMIService = GetObject("winmgmts:\\.\root\cimv2")  
Set objPing = objWMIService.ExecQuery(wmiQuery)    
For Each objStatus in objPing      
If IsNull(objStatus.StatusCode) Or objStatus.Statuscode<>0 Then          
ping = False 
Else          
ping = True 
End If  
Next 
End Function



Function ConvertSize(Size) 
'http://sogeeky.blogspot.com/2007/04/vbscript-function-code-to-convert-bytes.html
Do While InStr(Size,",") 'Remove commas from size 
    CommaLocate = InStr(Size,",") 
    Size = Mid(Size,1,CommaLocate - 1) & _ 
        Mid(Size,CommaLocate + 1,Len(Size) - CommaLocate) 
Loop 

Suffix = " Bytes" 
If Size >= 1024 Then suffix = " KB" 
If Size >= 1048576 Then suffix = " MB" 
If Size >= 1073741824 Then suffix = " GB" 
If Size >= 1099511627776 Then suffix = " TB" 

Select Case Suffix 
    Case " KB" Size = Round(Size / 1024, 1) 
    Case " MB" Size = Round(Size / 1048576, 1) 
    Case " GB" Size = Round(Size / 1073741824, 1) 
    Case " TB" Size = Round(Size / 1099511627776, 1) 
End Select 

ConvertSize = Size & Suffix 
End Function

Open in new window

0
 
Thomas_MeyerAuthor Commented:
I need a script that I described, I'll sometimes run as a scheduled job - I can use the InputBox.
0
Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

 
Bill PrewCommented:
So, if you had the following structure:

\\S1\Innovation 02
\\S1\Innovation 02\Dir1
\\S1\Innovation 02\Dir1\Dir1A
\\S1\Innovation 02\Dir2
\\S1\Innovation 02\Dir2\Dir2A


with these files:

\\S1\Innovation 02\File1.doc
\\S1\Innovation 02\Dir1\File2.doc
\\S1\Innovation 02\Dir1\Dir1A\File3.doc
\\S1\Innovation 02\Dir2\File4.doc
\\S1\Innovation 02\Dir2\Dir2A\File5.doc


you would want all 5 of those files copied to \\S1\Report 1

\\S1\Report 1\File1.doc
\\S1\Report 1\File2.doc
\\S1\Report 1\File3.doc
\\S1\Report 1\File4.doc
\\S1\Report 1\File5.doc


~bp
0
 
Bill PrewCommented:
If my last post was correct for your needs, then you could do it with a simple BAT file as follows:

@echo off
for /R "\\S1\Innovation 02" %%A in (*.doc *.docx *.ppsm *.ppt *.jpg) do copy "%%A" "\\S1\Report 1\"

Open in new window

~bp
0
 
yehudahaCommented:
nice one bill

when our batch classes begin ? :-)
0
 
Thomas_MeyerAuthor Commented:
To BillPrew:
Can you please do the equivalent. bat script as VB Script?
Regards.
TM
0
 
yehudahaCommented:
hey thomas

you have my version to vbscript
0
 
Thomas_MeyerAuthor Commented:
For yehudaha:
It's almost perfect, but does not notice me, which files have been copied, only MsgBox "done "
0
 
yehudahaCommented:
mmm if you puted extension like i did in my example and didn't get any result
i think nothing found and nothing copyed can you check that ?
0
 
Thomas_MeyerAuthor Commented:
Copied is always around 25-30 files.
0
 
yehudahaCommented:
with my script ?
0
 
Thomas_MeyerAuthor Commented:
Yes...
0
 
Thomas_MeyerAuthor Commented:
I think it's a problem only in the number of files moved, if there is an amount greater than can be displayed in a MsgBox, so it appears.
TM
0
 
Thomas_MeyerAuthor Commented:
Thanks
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.