change file location

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Need some help creating a routine in access that looks for a specific string
in a file name in a specific folder and moves that file to another folder,
somethig like: file as the string "report" in the midle of it's name and is
located in MyDocuments folder, and I need it to be moved to MyReports folder.
How do I do this automaticaly?
Thanks in advanced.
 
I actually did something similar to this recently. Here is a snippit for you:
<code>
Dim fs
Dim strFileName as String

Set fs = Application.FileSearch
With fs
'Setup
.LookIn = "C:\Documents and Settings\Username\My Documents" 'Path to
search folder
.FileName = "*Reports*.*"
If .Execute > 0 Then
For i = 1 To .FoundFiles.Count
strFileName=Right$(strSelectedItem, Len(strSelectedItem) -
InStrRev(strSelectedItem, "\"))
FileCopy .FoundFiles(i), "C:\Documents and Settings\Username\My
Documents\MyReports\" & strFileName
Next i
Else
'No files found
End If
End With

<End Code>

This is only a quick example. It will need some tweaking but you should get
it fixed easy.
 
I get the folowing message:
"Run Time error '75'
Path/File access error"

Did you use literally

FileCopy .FoundFiles(i), "C:\Documents and Settings\Username\My
Documents\MyReports\" & strFileName

in your code? This will work if you have a path containing this exact literal
string - "C:\Documents and Settings\Username\My Documents\MyReports\" in your
directory. I rather suspect that your username is different and that you have
a folder with a name other than MyReports. Nyx was suggesting that you edit
this sample code to your own path and filenames (which of course he cannot
see)... did you?

John W. Vinson [MVP]
 
Because you've got embedded spaces in the path, you need extra quotes:

FileCopy """" & .FoundFiles(i) & """", _
"""C:\Documents and Settings\Username\My Documents\MyReports\" & _
strFileName & """"
 
The lines work, but one day day work the other they don't.
Giving me the message that file was not found.
Code used:


Public Function Comando001_Click()

Dim fs
Dim strFileName As String

Set fs = Application.FileSearch
With fs

.LookIn = "D:\Documents and Settings\Diogo\My Documents"
.FileName = "Relatorio diario*.*"
If .Execute > 0 Then
For i = 1 To .FoundFiles.Count
strFileName = Right(.FoundFiles(i), 31)
FileCopy .FoundFiles(i), "D:\Documents and Settings\Diogo\My
Documents\Trabalho\" & strFileName
Kill .FoundFiles(i)
Next i
Else
MsgBox "Não foram encontrados ficheiros."
End If
End With

End Function
 
Back
Top