Solved

VBscript to rename file based on SQL field value

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SQL query 4 47
SQL Server 2008 R2 - Sums/Grouping 7 51
Passing a Text Box name to a Sub 6 74
Problem to With line 4 39
This article describes how to use the timestamp of existing data in a database to allow Tableau to calculate the prior work day instead of relying on case statements or if statements to calculate the days of the week.
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
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…
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…

911 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now