ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   .Foundfiles Question (https://www.excelbanter.com/excel-programming/346472-foundfiles-question.html)

Shane

.Foundfiles Question
 
Hi all,

Would you be able to advise how I could copy the files found by the
following macro to a another directory - say D:\Data

Set fs = Application.FileSearch
With fs
.NewSearch
.LookIn = "C:\My Documents"
.Filename = "*.doc"
.SearchSubFolders = True
.MatchTextExactly = True
.FileType = msoFileTypeAllFiles


If .Execute(SortBy:=msoSortByFileName, _
SortOrder:=msoSortOrderAscending) 0 Then
' MsgBox "There were " & .FoundFiles.Count & _
' " file(s) found."
For i = 1 To .FoundFiles.Count
MsgBox .FoundFiles(i)
Next i
Else
MsgBox "There were no files found."
End If
Thanks



Bob Phillips[_6_]

.Foundfiles Question
 
Not tested

Set fs = Application.FileSearch
With fs
.NewSearch
.LookIn = "C:\My Documents"
.Filename = "*.doc"
.SearchSubFolders = True
.MatchTextExactly = True
.FileType = msoFileTypeAllFiles


If .Execute(SortBy:=msoSortByFileName, _
SortOrder:=msoSortOrderAscending) 0 Then
' MsgBox "There were " & .FoundFiles.Count & _
' " file(s) found."
For i = 1 To .FoundFiles.Count
Workbooks.Open .FoundFiles(i)
Activeworkbook.SaveAs "D:\" & Activeworkbook.Name
Activeworkbook.Close Savechanges:=False
Next i
Else
MsgBox "There were no files found."
End If


--

HTH

RP
(remove nothere from the email address if mailing direct)


"Shane" wrote in message
...
Hi all,

Would you be able to advise how I could copy the files found by the
following macro to a another directory - say D:\Data

Set fs = Application.FileSearch
With fs
.NewSearch
.LookIn = "C:\My Documents"
.Filename = "*.doc"
.SearchSubFolders = True
.MatchTextExactly = True
.FileType = msoFileTypeAllFiles


If .Execute(SortBy:=msoSortByFileName, _
SortOrder:=msoSortOrderAscending) 0 Then
' MsgBox "There were " & .FoundFiles.Count & _
' " file(s) found."
For i = 1 To .FoundFiles.Count
MsgBox .FoundFiles(i)
Next i
Else
MsgBox "There were no files found."
End If
Thanks





Shane

.Foundfiles Question
 
thanks bob,
can we simply copy the files without actually having to open them. much like
a copy and past behind the scene?


"Bob Phillips" wrote in message
...
Not tested

Set fs = Application.FileSearch
With fs
.NewSearch
.LookIn = "C:\My Documents"
.Filename = "*.doc"
.SearchSubFolders = True
.MatchTextExactly = True
.FileType = msoFileTypeAllFiles


If .Execute(SortBy:=msoSortByFileName, _
SortOrder:=msoSortOrderAscending) 0 Then
' MsgBox "There were " & .FoundFiles.Count & _
' " file(s) found."
For i = 1 To .FoundFiles.Count
Workbooks.Open .FoundFiles(i)
Activeworkbook.SaveAs "D:\" & Activeworkbook.Name
Activeworkbook.Close Savechanges:=False
Next i
Else
MsgBox "There were no files found."
End If


--

HTH

RP
(remove nothere from the email address if mailing direct)


"Shane" wrote in message
...
Hi all,

Would you be able to advise how I could copy the files found by the
following macro to a another directory - say D:\Data

Set fs = Application.FileSearch
With fs
.NewSearch
.LookIn = "C:\My Documents"
.Filename = "*.doc"
.SearchSubFolders = True
.MatchTextExactly = True
.FileType = msoFileTypeAllFiles


If .Execute(SortBy:=msoSortByFileName, _
SortOrder:=msoSortOrderAscending) 0 Then
' MsgBox "There were " & .FoundFiles.Count & _
' " file(s) found."
For i = 1 To .FoundFiles.Count
MsgBox .FoundFiles(i)
Next i
Else
MsgBox "There were no files found."
End If
Thanks







Bruno Campanini[_3_]

.Foundfiles Question
 
"Shane" wrote in message
...

Would you be able to advise how I could copy the files found by the
following macro to a another directory - say D:\Data

[...]

Simply replace your IF clause with the following:
====================================
If .Execute(SortBy:=msoSortByFileName, _
SortOrder:=msoSortOrderAscending) Then
For i = 1 To .FoundFiles.Count
FileCopy .FoundFiles(i), "C:\Prova\" & _
Mid(.FoundFiles(i), InStrRev(.FoundFiles(i), "\") + 1)
Next i
Else
MsgBox "There were no files found."
End If
====================================
Ciao
Bruno



Bob Phillips[_6_]

.Foundfiles Question
 
Hi Shane,

Yes, this alternative way

'---------------------------------------------------------------------------
Sub CopyFolder()
'---------------------------------------------------------------------------
Dim oFSO As Object
Dim sFolderSource As String
Dim sFolderTarget As String

Set oFSO = CreateObject("Scripting.FileSystemObject")
sFolderSource = "C:\My Documents"
sFolderTarget = "D:\Data"
If oFSO.FolderExists(sFolderSource) Then
If Not oFSO.FolderExists(sFolderTarget) Then
MkDir sFolderTarget
End If
oFSO.CopyFolder sFolderSource, sFolderTarget & "\"
End If
Set oFSO = Nothing
End Sub


--

HTH

RP
(remove nothere from the email address if mailing direct)


"Shane" wrote in message
...
thanks bob,
can we simply copy the files without actually having to open them. much

like
a copy and past behind the scene?


"Bob Phillips" wrote in message
...
Not tested

Set fs = Application.FileSearch
With fs
.NewSearch
.LookIn = "C:\My Documents"
.Filename = "*.doc"
.SearchSubFolders = True
.MatchTextExactly = True
.FileType = msoFileTypeAllFiles


If .Execute(SortBy:=msoSortByFileName, _
SortOrder:=msoSortOrderAscending) 0 Then
' MsgBox "There were " & .FoundFiles.Count & _
' " file(s) found."
For i = 1 To .FoundFiles.Count
Workbooks.Open .FoundFiles(i)
Activeworkbook.SaveAs "D:\" & Activeworkbook.Name
Activeworkbook.Close Savechanges:=False
Next i
Else
MsgBox "There were no files found."
End If


--

HTH

RP
(remove nothere from the email address if mailing direct)


"Shane" wrote in message
...
Hi all,

Would you be able to advise how I could copy the files found by the
following macro to a another directory - say D:\Data

Set fs = Application.FileSearch
With fs
.NewSearch
.LookIn = "C:\My Documents"
.Filename = "*.doc"
.SearchSubFolders = True
.MatchTextExactly = True
.FileType = msoFileTypeAllFiles


If .Execute(SortBy:=msoSortByFileName, _
SortOrder:=msoSortOrderAscending) 0 Then
' MsgBox "There were " & .FoundFiles.Count & _
' " file(s) found."
For i = 1 To .FoundFiles.Count
MsgBox .FoundFiles(i)
Next i
Else
MsgBox "There were no files found."
End If
Thanks









Shane

.Foundfiles Question
 
thank you all so much!!!


"Bruno Campanini" wrote in message
...
"Shane" wrote in message
...

Would you be able to advise how I could copy the files found by the
following macro to a another directory - say D:\Data

[...]

Simply replace your IF clause with the following:
====================================
If .Execute(SortBy:=msoSortByFileName, _
SortOrder:=msoSortOrderAscending) Then
For i = 1 To .FoundFiles.Count
FileCopy .FoundFiles(i), "C:\Prova\" & _
Mid(.FoundFiles(i), InStrRev(.FoundFiles(i), "\") + 1)
Next i
Else
MsgBox "There were no files found."
End If
====================================
Ciao
Bruno






All times are GMT +1. The time now is 10:13 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com