Solved

VBscript to rename file based on SQL field value

Posted on 2009-05-14
4
789 Views
Last Modified: 2012-05-07
I have image files uploaded to my server with a consistent naming pattern. They all are named based on their unique MLS listing number (these are pictures of homes from realtors). That unique listing number is also present in the sql table along with other fields of data. Each record in the sql table has another unique field value called record number. I want to write a vbscript to convert those file names from their MLS listing number to their record number.
Obviously, I need to have a fso.moveFile oldFileName, newFileName type structure which I can figure out. I'm not sure how to loop the lookup portion of the script which will look for the pictures matching sql record and return the record number. Any help is greatly appreciated.
0
Comment
Question by:rbilbey
[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
  • 2
4 Comments
 
LVL 5

Accepted Solution

by:
satheeshm earned 250 total points
ID: 24388731
Dim objFSO
Set objFSO = Server.CreateObject("Scripting.FileSystemObject")

'Get the folder object associated with the directory
Dim objFolder
Set objFolder = objFSO.GetFolder("C:\InetPub\wwwroot")

Response.Write "The files found in " & objFolder.Name & ":<br>"

'Loop through the Files collection
Dim objFile
For Each objFile in objFolder.Files
  for each rec in recordset \\get your recrods from the db here
      if rec("filename") = objFile.Name then
            fso.moveFile objfile.Name, rec("record number")
      end if

  next
Next

'Clean up!
Set objFolder = Nothing
Set objFile = Nothing
Set objFSO = Nothing
0
 

Author Comment

by:rbilbey
ID: 24390569
thanks but I still having trouble. I'm not sure on the collecting a recordset. This is server side and I'm more experienced with client side. I follow what you have until the "for each rec in recordset\\get your records from the db here". Not sure if it should be a select statement on that line or what. Thanks
0
 
LVL 10

Expert Comment

by:Banthor
ID: 24392452
Basic Get a Recordset from a stored procedure.
dim cn
Set cn = server.CreateObject("adodb.connection")
cn.Connectionstring = "Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=DATABASE;Data Source=SERVER"
cn.open
 
dim rs
set rs = server.createobject("adodb.recordset")
 
dim sql
SQL = "storeprocedurename @Parameter=data"
 
SET RS = cn.Exectute(SQL,,1) 'Readonly Forward

Open in new window

0
 
LVL 10

Expert Comment

by:Banthor
ID: 24392469
SO This
  for each rec in recordset \\get your recrods from the db here
      if rec("filename") = objFile.Name then
            fso.moveFile objfile.Name, rec("record number")
      end if
BECOMES
 
SQL = "storeprocedurename @Listing='" & objFile.Name & "'"
SET RS = cn.Exectute(SQL,,1) 'Readonly Forward
if rs.bof = true and rs.eof = true then
  'Listing not found
 Else
             fso.moveFile objfile.Name, rs("recordnumber")
end if  
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Composite queries are used to retrieve the results from joining multiple queries after applying any filters. UNION, INTERSECT, MINUS, and UNION ALL are some of the operators used to get certain desired results.​
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

733 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