Hello,

I need to develop an executable file suing vb.net, that will take in an article number as a parameter. Then using this article number a search will be performed in a server directory folder for a file that (if exists) will be named by convention article_number.file_extension the article number should match the file name (excluding the extension)

If the file exists then a value will be returned specifying that the file for that article number was found otherwise the value "file not found"will be returned. for both the values "was found" or "not found"will be stored in a SQL server database.

I have develop small windows applications using vb.net, but usually forms or aspx pages.

my question is: how can an executable receive a parameter?

Thank you,

pelusa.

Hello Peluse, say that I have created a public global string var called sParam. In the form load event of your main form put;

Me.sParam = My.Application.CommandLineArgs(0)

This stores the recieved parameter in the variable.

Hi emurf and thank you for your reply so fast,

so if i am calling the executable from another application where the parameter for article number is generated, i still will be using the same method?

thanks again,

pelusa.

I'm guessing that you would be using the shell method in the calling program, if so it will look like this;

iId = Shell("c:\calledprog.exe Hello")

Where Hello is the parameter passed and iId is an integer to hold the value returned by the called program.

if you don't mind me asking a little bit more.

in vb.net how can I read a directory folder and match a file name to a value.

let's say that i pass article number an123 and i have two directories where i want to perform the search for a file.

directory folder: art_file_size_500
and
directory folder: art_file_size_1000

i have to find a file in either directory, but not in both. the file name would be an123.xsl if exists for that article.

if the file exists in directory art_file_size_500 then i have to write the value art_file_size_500 to the database if there is no file in either folder, then i need to write "file not found"

i can place the directory folder path in the app.config file but i don't know how to do the search/ match

thank you and sorry for the length of my question.

if you know any tutorial or sample code, that would be ok too.

thanks,

pelusa.

If I understand the question correctly, this is what I would do;

If System.IO.File.Exists("path\art_file_size_500\an123.xsl") And System.IO.File.Exists("path\art_file_size_1000\an123.xsl") Then
   'found in both directories, do code for that here

ElseIf System.IO.File.Exists("path\art_file_size_500\an123.xsl") Then
 'found in the art_file_size_500 directory, preform that stuff here

ElseIf System.IO.File.Exists("path\art_file_size_1000\an123.xsl") Then
'perform the art_file_size_1000 code

Else
'the file wasn't found, do that code

End If
Comments
useful recommendation to complete my task

i have started some of the code and now i am stuck in one part.

this is the directory path where the tow folders containing the different template sizes are located:

\\thisserver\development\Templates\

inside this directory there are 2 folders

500 (Templates500 )
750 (Templates750)

and inside each folder there are template files.


if my code to search for the article number matching file looks like this:

Private Function SearchLabelTemplate(ByVal strItemNumber As String) As String


If System.IO.File.Exists(m_Templates500 & strItemNumber & ".xsl") Then

ElseIf System.IO.File.Exists(m__Templates750 & strItemNumber & ".xsl") Then

Else
' File not found
End If


End Function

i think i need a for loop to iterate through all the files in each folder, but i don't know where exactly place this loop.

do you have any suggestions on how to achieve this?

thank you.

The code I posted earlier will check two paticular directories for a certain file. If you wanted to loop through each file in a directory you need to use readonly collections and the Computer.FileSystem.GetFiles function. The code looks like this;

'declare a collection to hold the filenames in the  dir
Dim colFiles As ReadOnlyCollection(Of String)
Dim strFile As String

'get the filenames
colFiles = My.Computer.FileSystem.GetFiles("\\thisserver\development\Templates\500\", FileIO.SearchOption.SearchTopLevelOnly, "*.xsl")

'get the path for each
For Each strFile In colFiles
    If strFile = "\\thisserver\development\Templates\500\an123.xsl" then 
         'put your code here
    End If
Next

This would take place of the earlier if statements and needs to be done for each dir.

hi again and sorry for bugging so much.

i came up with the code below a little before your post.

please let me know if you think this looks correctly done and or if i should try to do it differently....

ate Function SearchLabelTemplate(ByVal strItemNumber As String, ByVal strTemplateFoundInDirectory As String) As Boolean

Dim strTemplateName As String
Dim blnFound As Boolean

' make a reference to the 500 directory & folder
Dim di500 As New IO.DirectoryInfo(m_strDirectoryPath & m_Template_500)
Dim diar500 As IO.FileInfo() = di500.GetFiles()
Dim dra500 As IO.FileInfo

' make a reference to the 750 directory & folder
Dim di750 As New IO.DirectoryInfo(m_strDirectoryPath & m_Template_750)
Dim diar750 As IO.FileInfo() = di750.GetFiles()
Dim dra750 As IO.FileInfo


blnFound = False

strTemplateName = strItemNumber & m_strFileExtension

For Each dra500 In diar500

If (strTemplateName = dra500.Name) Then
strTemplateFoundInDirectory = "500"
blnFound = True
Else
blnFound = False
End If
Next

For Each dra750 In diar750

If (strTemplateName = dra750.Name) Then
strTemplateFoundInDirectory = "750"
blnFound = True
Else
blnFound = False
End If
Next


End Function

doing it this way where can i put the else in case that not file was found in either folder?

thanks much.

Hi again,

well i just finished all the code for my program and so far the function that searches a directory for a template has worked perfectly. I'm sure that someone with more experience could be able to do this a better way, but this is how I can do it right now.

I would like you if you don't mind, to take a look at my code and if you have any recommendations please let me know those too. (names have change)

Thank you so much for your kind help.

Private Function SearchLabelTemplate(ByVal strItemNumber As String, ByRef m_strMessage As String) As Boolean

Dim strTemplateName As String
Dim blnSuccess As Boolean

' Make a reference to the 4 x 4 directory
Dim di4x4 As New IO.DirectoryInfo(m_strDirectoryPath & m_str4x4TemplateFolderLocation)
Dim diar4x4 As IO.FileInfo() = di4x4.GetFiles()
Dim dra4x4 As IO.FileInfo

' Make a reference to the 4 x 6 directory
Dim di4x6 As New IO.DirectoryInfo(m_strDirectoryPath & m_str4x6TemplateFolderLocation)
Dim diar4x6 As IO.FileInfo() = di4x6.GetFiles()
Dim dra4x6 As IO.FileInfo

Try

blnSuccess = False

strTemplateName = strItemNumber & m_strFileExtension

' List the names of all files in the 4 x 4 directory
If Not blnSuccess Then
For Each dra4x4 In diar4x4

If (strTemplateName = dra4x4.Name) Then
m_strMessage = "4x4"
blnSuccess = True
Exit Try
Else
blnSuccess = False
m_strMessage = "NO TEMPLATE"
End If
Next
End If

' List the names of all files in the 4 x 6 directory
If Not blnSuccess Then
For Each dra4x6 In diar4x6

If (strTemplateName = dra4x6.Name) Then
m_strMessage = "4x6"
blnSuccess = True
Exit Try
Else
blnSuccess = False
m_strMessage = "NO TEMPLATE"
End If
Next
End If

blnSuccess = True

Catch ex As Exception
blnSuccess = False
If Trim(m_strMessage) = "" Then
m_strMessage = ex.Message
End If
' Log the error
Logger.WriteLog(ex.ToString, LogLevel.Errors, System.Reflection.MethodBase.GetCurrentMethod().Name)
End Try
Return blnSuccess

End Function

It is hard to know for sure when you can't test it yourself but everything looks good as far as I can see.

This question has already been answered. Start a new discussion instead.