-
Apr 7th, 2012, 02:47 PM
#1
Thread Starter
New Member
User Input Directed Search Of Folders and SubFolders, Copy File To Different Folder
I have searched for my answer, looked at little pieces here and there, but haven't hit the nail on the head.
Everyday we have 4 files (2 AM, and 2 PM), both divided into two different work locations. We need to archive those 4 files into our workgroups local drive. Now each day the filename contains that days date. So for today AM we have: Time Exception A 4-7-2012 and Time Exception B 4-7-2012, and PM we have Time Exception A 4-7-2012 and Time Exception B 4-7-2012. All four files are located in different folders. Folder paths:
AM:
Time Exception A 4-7-2012:
S:\ABCSharedFolders\OOC\spOvA\ES\ABC\PSD\AM Time Exception\PP #7\
Time Exception B 4-7-2012:
S:\ABCSharedFolder\OOC\spOvA\ES\ABC\BSD\AM FILES\AM Time Exception\PP-07\
PM:
Time Exception A 4-7-2012
S:\ABCSharedFolders\OOC\spOvA\ES\ABC\PSD\PM Time Exception\PP 7-12\
Time Exception B 4-7-2012
S:\ABCSharedFolders\OOC\spOvA\ES\ABC\BSD\PM FILES\PM Time Exceptions\PP #07 - 2012\
One problem that we run into as you can see is that the consistancy is lost when these workgroups name their folders. That is why having some code that will search the subdirectories is critical since we do have consistancy all the way up to PSD and BSD folders.
Once we can confirm that the file exists, it will have to be copied and pasted to our workgroups folder. The path for this is:
AM:
Time Exception A 4-7-2012:
Z:\ABC\ABC Field\COSA\Time Exception Archive\2012 Archive\A\April\AM\
Time Exception B 4-7-2012:
Z:\ABC\ABC Field\COSA\Time Exception Archive\2012 Archive\B\April\AM\
PM:
Time Exception A 4-7-2012
Z:\ABC\ABC Field\COSA\Time Exception Archive\2012 Archive\A\April\PM\
Time Exception B 4-7-2012
Z:\ABC\ABC Field\COSA\Time Exception Archive\2012 Archive\B\April\PM\
If possible, we would like the "user input" or search function to only look for the date in the filename just in case someone gets the need to save the file with a funky name, at least we know the date will always be there.
Here is what I have found so far. I know it is nowhere near what I need, but overall I liked the functionallity of it. It will only search for specific, whole, filenames and that we wont have.
Code:
Sub CopySomeFiles()
Dim FSO, sourceFolder, currentFile, filesInSourceFolder
Dim strSourceFolderPath As String
Dim strDestinationFolderPath As String
Dim strUserInput As String
Set FSO = CreateObject("Scripting.FileSystemObject")
' Figure out which file to copy from where to where
strUserInput = InputBox("Please enter name of file to copy.")
strSourceFolderPath = "C:\MySourceFolder"
strDestinationFolderPath = "C:\MyDestinationFolder"
Set sourceFolder = FSO.GetFolder(strSourceFolderPath)
Set filesInSourceFolder = sourceFolder.Files
' Look at all files in source folder. If name matches,
' copy to destination folder.
For Each currentFile In filesInSourceFolder
If currentFile.Name = strUserInput Then
currentFile.Copy (FSO.BuildPath(strDestinationFolderPath, _
currentFile.Name))
End If
Next
End Sub
I hope I explained this in a manner that can be followed. It is a lot of information. Please let me know if I can elaborate any more.
-
Apr 7th, 2012, 03:18 PM
#2
Thread Starter
New Member
Re: User Input Directed Search Of Folders and SubFolders, Copy File To Different Fold
My apologies, I forgot to mention that the files to be copied are word document files, either .doc or .docx. We are applying this macro to our Excel Spreadsheet though.
Thanks
-
Apr 8th, 2012, 12:28 AM
#3
Re: User Input Directed Search Of Folders and SubFolders, Copy File To Different Fold
i would look at using the searchtreeforfile API to locate the correct file regardless of folder
copying the file to correct destination is then straightforward
i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next
dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part
come back and mark your original post as resolved if your problem is fixed
pete
-
Apr 8th, 2012, 07:02 AM
#4
Thread Starter
New Member
Re: User Input Directed Search Of Folders and SubFolders, Copy File To Different Fold
Thanks westconn1 for your reply. I am not familiar with the searchtreeforfile API, so I'll have to look it up. Any pointers in the meantime?
-
Apr 8th, 2012, 03:42 PM
#5
Thread Starter
New Member
Re: User Input Directed Search Of Folders and SubFolders, Copy File To Different Fold
Or can anyone else chime in?
-
Apr 8th, 2012, 08:21 PM
#6
Re: User Input Directed Search Of Folders and SubFolders, Copy File To Different Fold
So if I understand you correctly, you simply want to know if a file exists in a subdirectory?
A good exercise for the Heart is to bend down and help another up...
Please Mark your Thread "Resolved", if the query is solved
MyGear:
★ CPU ★ Ryzen 5 5800X
★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
★ Keyboard ★ TVS Electronics Gold Keyboard
★ Mouse ★ Logitech G502 Hero
-
Apr 8th, 2012, 08:31 PM
#7
Thread Starter
New Member
Re: User Input Directed Search Of Folders and SubFolders, Copy File To Different Fold
Originally Posted by koolsid
So if I understand you correctly, you simply want to know if a file exists in a subdirectory?
I need to confirm they are in the subdirectory, and then copy them to our workgroups specified folder.
-
Apr 8th, 2012, 08:34 PM
#8
-
Apr 9th, 2012, 03:24 AM
#9
Re: User Input Directed Search Of Folders and SubFolders, Copy File To Different Fold
try like
vb Code:
Private Const MAX_PATH As Long = 260 Private Declare Function SearchTreeForFile Lib "imagehlp" _ (ByVal sRootPath As String, _ ByVal InputPathName As String, _ ByVal OutputPathBuffer As String) As Boolean sub copyfromtree() sfname = Space(MAX_PATH) If SearchTreeForFile("S:\ABCSharedFolder\OOC\spOvA\ES\ABC", "Time Exception A 4-7-2012" , sfname) Then sfname = Left(sfname, InStr(sfname, vbNullChar) - 1) filecopy sfname, destfile end if end sub
you may need to supply more filepath, to differenciate am and pm files, and destfile is a fully qualified path to the destination file
you could store some part or the file path in an array for the different files then use a loop, els build the copy into a procedure, that can be called from the main code
i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next
dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part
come back and mark your original post as resolved if your problem is fixed
pete
-
Apr 11th, 2012, 08:37 PM
#10
Thread Starter
New Member
Re: User Input Directed Search Of Folders and SubFolders, Copy File To Different Fold
Originally Posted by westconn1
try like
vb Code:
Private Const MAX_PATH As Long = 260
Private Declare Function SearchTreeForFile Lib "imagehlp" _
(ByVal sRootPath As String, _
ByVal InputPathName As String, _
ByVal OutputPathBuffer As String) As Boolean
sub copyfromtree()
sfname = Space(MAX_PATH)
If SearchTreeForFile("S:\ABCSharedFolder\OOC\spOvA\ES\ABC", "Time Exception A 4-7-2012" , sfname) Then
sfname = Left(sfname, InStr(sfname, vbNullChar) - 1)
filecopy sfname, destfile
end if
end sub
you may need to supply more filepath, to differenciate am and pm files, and destfile is a fully qualified path to the destination file
you could store some part or the file path in an array for the different files then use a loop, els build the copy into a procedure, that can be called from the main code
I cannot get that code to work, even when I add the filepath. Is more required that I should know, or should that have worked?
-
Apr 12th, 2012, 05:55 AM
#11
Re: User Input Directed Search Of Folders and SubFolders, Copy File To Different Fold
Is more required that I should know, or should that have worked?
it should have just worked (tested) as far as retrieving a full path to file, but destfile (destination file path\name) would have to be assigned a value for the filecopy to work
the first 6 lines must be at the top of the code pane, not within some procedure
I cannot get that code to work
what happens?
error? wrong result? nothing?
i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next
dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part
come back and mark your original post as resolved if your problem is fixed
pete
-
Apr 12th, 2012, 03:59 PM
#12
Thread Starter
New Member
Re: User Input Directed Search Of Folders and SubFolders, Copy File To Different Fold
Originally Posted by westconn1
it should have just worked (tested) as far as retrieving a full path to file, but destfile (destination file path\name) would have to be assigned a value for the filecopy to work
the first 6 lines must be at the top of the code pane, not within some procedure
what happens?
error? wrong result? nothing?
Sorry for the late reply, just had another chance to run it at work. It seems to be doing nothing. When the sub is run, the hourglass comes up for a split second and then its gone. No errors, but no results. I have even specified the SearchTreeForFile all the way to the bottom folder and still nothing.
-
Apr 12th, 2012, 04:56 PM
#13
Re: User Input Directed Search Of Folders and SubFolders, Copy File To Different Fold
is the destination file created?
insert at line 14
if the file is not found, you should get empty messagebox, else the full path of the file should be in the messagebox
even without the messagebox the filecopy statement would produce an error if the file is not found by searchtreeforfile
i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next
dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part
come back and mark your original post as resolved if your problem is fixed
pete
-
Apr 12th, 2012, 07:25 PM
#14
Thread Starter
New Member
Re: User Input Directed Search Of Folders and SubFolders, Copy File To Different Fold
Originally Posted by westconn1
is the destination file created?
insert at line 14
if the file is not found, you should get empty messagebox, else the full path of the file should be in the messagebox
even without the messagebox the filecopy statement would produce an error if the file is not found by searchtreeforfile
I did as you suggested and entered:
Code:
MsgBox sfname, destfile
in line 14 and it seems like it's doing something, but then the code stops running and nothing comes up. No message box, no error.
-
Apr 13th, 2012, 04:42 AM
#15
Re: User Input Directed Search Of Folders and SubFolders, Copy File To Different Fold
just in case it was a network thing, i tested on a network drive, it worked fine, but was slower
i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next
dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part
come back and mark your original post as resolved if your problem is fixed
pete
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|