Solved

VBscript to rename file based on SQL field value

Posted on 2009-05-14
4
790 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

The Orion Papers

Are you interested in becoming an AWS Certified Solutions Architect?

Discover a new interactive way of training for the exam.

Question has a verified solution.

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

If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

705 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