Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Move files based on MS Access query match

Posted on 2014-11-27
7
Medium Priority
?
212 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

 
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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

In Part II of this series, I will discuss how to identify all open instances of Excel and enumerate the workbooks, spreadsheets, and named ranges within each of those instances.
We live in a world of interfaces like the one in the title picture. VBA also allows to use interfaces which offers a lot of possibilities. This article describes how to use interfaces in VBA and how to work around their bugs.
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
What’s inside an Access Desktop Database. Will look at the basic interface, Navigation Pane (Database Container), Tables, Queries, Forms, Report, Macro’s, and VBA code.
Suggested Courses

688 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