Solved

VBscript to rename file based on SQL field value

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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Confronted with some SQL you don't know can be a daunting task. It can be even more daunting if that SQL carries some of the old secret codes used in the Ye Olde query syntax, such as: (+)     as used in Oracle;     *=     =*    as used in Sybase …
Introduction: Recently, I got a requirement to zip all files individually with batch file script in Windows OS. I don't know much about scripting, but I searched Google and found a lot of examples and websites to complete my task. Finally, I was ab…
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 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…

707 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

14 Experts available now in Live!

Get 1:1 Help Now