Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

VBscript to rename file based on SQL field value

Posted on 2009-05-14
4
Medium Priority
?
793 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 750 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

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.

Question has a verified solution.

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

You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
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.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Suggested Courses

650 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