Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Move files based on MS Access query match

Posted on 2014-11-27
7
Medium Priority
?
215 Views
Last Modified: 2014-11-27
Hi, I need to move and rename PDF files if the existing filename matches a database query. My query has a result called case_num. Where the case_num matched the name of a PDF file in a known folder, I want to move it to another location and add the suffix "_original", the .pdf.

I've been struggling with this for a little while now so your help would be most appreciated.

Regards, Jon
0
Comment
Question by:jonlake
  • 5
  • 2
7 Comments
 
LVL 26

Expert Comment

by:Nick67
ID: 40469351
VBA is good?

'pseudocode
'fire up a recordset of the existing filenames
'fire up a FilesystemObject
'walkdown the recordset
Do until rs.EOF
   if fs.FileExists(Path & rs!filename) then
         fs.movefile Path & rs!filename, newPathAndFileName
   End if
   rs.movenext
Loop

That's the idea, anyway.
Now, you haven't really given me enough detail to flesh more than pseudocode
Is it the logic you are struggling with, the syntax, or the whole ball of wax?
0
 

Author Comment

by:jonlake
ID: 40469364
Hi Nick, the query (qryCase_File_Docs) has a field called case_file_num. In a folder location (e.g. C:\temp\case_files\) there are several hundred PDF files, each of which will have a name which matches a case_file_num result in the query.

I want to loop through the query and where a file exists of the same name, move it to another folder (e.g. c:\temp\case_files\renamed\), with a new file name, for example:

File c:\temp\case_file_num\123456.pdf would become c:\temp\case_file_num\newfile\123456_original.pdf

I hope this is enough to work with, thanks.
0
 
LVL 26

Expert Comment

by:Nick67
ID: 40469383
is the value of [case_file_num] = 123456
or 123456.pdf
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 26

Expert Comment

by:Nick67
ID: 40469392
This will be very close to production code

Dim db As Database
Set db = CurrentDb

Dim rs As Recordset
Set rs = db.OpenRecordset("select * from qryCase_File_Docs;", dbOpenDynaset, dbSeeChanges)

Dim fs As Object 'our friend the filesystemobject
Set fs = CreateObject("Scripting.FileSystemObject")

Dim BuiltPath As String
Dim FinalPath As String

BuiltPath = "C:\temp\case_files\"
If fs.FolderExists(BuiltPath) = False Then
    MsgBox "The Source folder doesn't exist!"
    Exit Sub
End If

FinalPath = "c:\temp\case_files\newfile\"
If fs.FolderExists(FinalPath) = False Then
    MsgBox "The Destination folder doesn't exist!"
    Exit Sub
End If

'the loop
Do Until rs.EOF
    If fs.FileExists(BuiltPath & rs!case_file_num & ".pdf") Then 'does it exist
        fs.MoveFile BuiltPath & rs!case_file_num & ".pdf", FinalPath & rs!case_file_num & "_original.pdf" 'move it
    End If
    rs.MoveNext 'next record
Loop 'rinse and repeat

MsgBox "Done!"

Open in new window

0
 
LVL 26

Accepted Solution

by:
Nick67 earned 2000 total points
ID: 40469418
Note that I didn't put checking in to deal with a destination file that already exists.
If you are accidentally creating identically named files, things are going to go BANG!
0
 

Author Closing Comment

by:jonlake
ID: 40469431
Superb! Completely nailed it!

I've built it, tested it and it's working perfectly. It should only ever be a one-off each time so checking if a file already exists won't (shouldn't) be a problem. Thank you, thank you......clean and elegant solution.
0
 
LVL 26

Expert Comment

by:Nick67
ID: 40469437
Glad you liked it!
Hopefully, it'll be useful going forward.
It's always nice to know 'hey I've coded something like that before!'

Nick67
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Access developers frequently have requirements to interact with Excel (import from or output to) in their applications.  You might be able to accomplish this with the TransferSpreadsheet and OutputTo methods, but in this series of articles I will di…
This article describes a method of delivering Word templates for use in merging Access data to Word documents, that requires no computer knowledge on the part of the recipient -- the templates are saved in table fields, and are extracted and install…
Basics of query design. Shows you how to construct a simple query by adding tables, perform joins, defining output columns, perform sorting, and apply criteria.
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
Suggested Courses

885 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question