EDN Admin
Well-known member
Hello,
I hope this is in the correct section. I apologize if it is not. I am trying to write a script in Visual Studio 2008 (using a VB project) that will, upon the clicking of a button:
- A file object will be created.
-Said file object will read in a number of files from a specified file path.
-A dataset will be created and populated with fields from two columns within a database table; app_id and company_name.
-The script will read through the file names and fields recorded in the dataset, renaming the file to the app_id if the original file name contained the company_name.
The code I have so far to do this:
<pre>Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click<br/>
Create the file object<br/>
Dim objFSO<br/>
objFSO = Server.CreateObject("Scripting.FileSystemObject")<br/>
<br/>
Get the folder object associated with the directory<br/>
Dim objFolder<br/>
In this case all the files I want to rename are in pdf format.<br/>
objFolder = objFSO.GetFolder("Cocuments and SettingsUser1My DocumentsApplications")<br/>
<br/>
Label1.Text = "The files found in " & objFolder.Name & ":<br>"<br/>
Create the dataset and populate it<br/>
Dim ds As DataSet = New DataSet<br/>
connection string "String" is configured in the web.config file of the application.<br/>
Dim cn As SqlConnection = New SqlConnection(ConfigurationManager.ConnectionStrings("String").ConnectionString)<br/>
Dim cmd As SqlCommand = New SqlCommand("SELECT company_name, app_id FROM app_BusinessDirectory", cn)<br/>
Dim _select As String<br/>
_select = "SELECT company_name, app_id FROM app_BusinessDirectory"<br/>
Dim appAdapter As SqlDataAdapter = New SqlDataAdapter(_select, cn)<br/>
appAdapter.TableMappings.Add("Table", "app_BusinessDirectory")<br/>
cn.Open()<br/>
<br/>
appAdapter.SelectCommand = cmd<br/>
appAdapter.Fill(ds)<br/>
cn.Close()<br/>
Loop through the Files collection<br/>
Dim objFile<br/>
For Each objFile In objFolder.Files<br/>
<br/>
Dim row As DataRow<br/>
For Each row In ds.Tables(0).Rows<br/>
If row("company_name") = objFile.Name Then<br/>
objFSO.moveFile(objFile.Name, row("app_id"))<br/>
<br/>
End If<br/>
Next<br/>
Next<br/>
<br/>
End Sub[/code]
<br/>
The problem is that currently this code will only rename the file if the file name and company_name match exactly, ideally Id need to include a clause to make it rename if the file name is "LIKE" or "contains" the company_name. To be honest
Im not even sure if what I want is possible, or if what I have is even on the right track. So far my own research into the issue has been less than conclusive. Im shooting in the dark here and any input would be greatly appreciated.<br/>
View the full article
I hope this is in the correct section. I apologize if it is not. I am trying to write a script in Visual Studio 2008 (using a VB project) that will, upon the clicking of a button:
- A file object will be created.
-Said file object will read in a number of files from a specified file path.
-A dataset will be created and populated with fields from two columns within a database table; app_id and company_name.
-The script will read through the file names and fields recorded in the dataset, renaming the file to the app_id if the original file name contained the company_name.
The code I have so far to do this:
<pre>Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click<br/>
Create the file object<br/>
Dim objFSO<br/>
objFSO = Server.CreateObject("Scripting.FileSystemObject")<br/>
<br/>
Get the folder object associated with the directory<br/>
Dim objFolder<br/>
In this case all the files I want to rename are in pdf format.<br/>
objFolder = objFSO.GetFolder("Cocuments and SettingsUser1My DocumentsApplications")<br/>
<br/>
Label1.Text = "The files found in " & objFolder.Name & ":<br>"<br/>
Create the dataset and populate it<br/>
Dim ds As DataSet = New DataSet<br/>
connection string "String" is configured in the web.config file of the application.<br/>
Dim cn As SqlConnection = New SqlConnection(ConfigurationManager.ConnectionStrings("String").ConnectionString)<br/>
Dim cmd As SqlCommand = New SqlCommand("SELECT company_name, app_id FROM app_BusinessDirectory", cn)<br/>
Dim _select As String<br/>
_select = "SELECT company_name, app_id FROM app_BusinessDirectory"<br/>
Dim appAdapter As SqlDataAdapter = New SqlDataAdapter(_select, cn)<br/>
appAdapter.TableMappings.Add("Table", "app_BusinessDirectory")<br/>
cn.Open()<br/>
<br/>
appAdapter.SelectCommand = cmd<br/>
appAdapter.Fill(ds)<br/>
cn.Close()<br/>
Loop through the Files collection<br/>
Dim objFile<br/>
For Each objFile In objFolder.Files<br/>
<br/>
Dim row As DataRow<br/>
For Each row In ds.Tables(0).Rows<br/>
If row("company_name") = objFile.Name Then<br/>
objFSO.moveFile(objFile.Name, row("app_id"))<br/>
<br/>
End If<br/>
Next<br/>
Next<br/>
<br/>
End Sub[/code]
<br/>
The problem is that currently this code will only rename the file if the file name and company_name match exactly, ideally Id need to include a clause to make it rename if the file name is "LIKE" or "contains" the company_name. To be honest
Im not even sure if what I want is possible, or if what I have is even on the right track. So far my own research into the issue has been less than conclusive. Im shooting in the dark here and any input would be greatly appreciated.<br/>
View the full article