Solved

Move files based on MS Access query match

Posted on 2014-11-27
7
203 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
The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

 
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 500 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

Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

Question has a verified solution.

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

When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
Describes a method of obtaining an object variable to an already running instance of Microsoft Access so that it can be controlled via automation.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
In Microsoft Access, learn how to “cascade” or have the displayed data of one combo control depend upon what’s entered in another. Base the dependent combo on a query for its row source: Add a reference to the first combo on the form as criteria i…

770 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