How to use Variable in IF...ELSE...ENDIF

Posted by Firstlast775 under VB.NET on 1/14/2011 | Points: 10 | Views : 2401 | Status : [Member] | Replies : 7
Here is my VB DOTNet Code, which does not work properly

Dim sTempString As String = ""
Dim sExtensionValue As String= "TXT,DBF"
Dim strFilename As String = "My_Doc.docx"
Dim ss As String = ""
Dim X As Int16 = 0

If UCase(sExtensionValue) <> "ALL" Then
Dim aFileExtension() As String = Split(sExtensionValue, ",")
For X = LBound(aFileExtension) To UBound(aFileExtension) - 1
ss = UCase(aFileExtension(X).ToString)
sTempString = sTempString & "UCase(Mid(" & Chr(34) & strFilename & Chr(34) & ",InStrRev(" & Chr(34) & strFilename _
& Chr(34) & "," & Chr(34) & Chr(46) & Chr(34) & ",Len(" & Chr(34) & strFilename & Chr(34) & "))" & Chr(43) & Chr(49) & _
"," & Chr(51) & "))=" & Chr(34) & ss & Chr(34) & " Or "
Next
ss = UCase(aFileExtension(X).ToString)
sTempString = sTempString & "UCase(Mid(" & Chr(34) & strFilename & Chr(34) & ",InStrRev(" & Chr(34) & strFilename _
& Chr(34) & "," & Chr(34) & Chr(46) & Chr(34) & ",Len(" & Chr(34) & strFilename & Chr(34) & "))" & Chr(43) & Chr(49) & _
"," & Chr(51) & "))=" & Chr(34) & ss & Chr(34)

If Chr(32) & sTempString & Chr(32) Then
' In this routine, it comes here which is wrong while I am using Variables to Create the value of sTempString
MsgBox("Here I Am, Which Is wrong")
Else

MsgBox("Here I Am, Which Is Right")
End If


' While I am using the actual Code like below, It works fine and it goes to correct location

If UCase(Mid("My_Doc.docx", InStrRev("My_Doc.docx", ".", Len("My_Doc.docx")) + 1, 3)) = "DBF" Or _
UCase(Mid("My_Doc.docx", InStrRev("My_Doc.docx", ".", Len("My_Doc.docx")) + 1, 3)) = "TXT" Then

MsgBox("Here I Am, Which Is wrong")
Else
' It comes here .
MsgBox("Here I Am, Which Is Right")
End If
End If

I, spend lot of hours and couldn't find my mistake that I did for myself.
If anybody can find my mistake towards my solution, will be appriciated.

Thanks.




Responses

Posted by: Karthikanbarasan on: 1/14/2011 [Member] [Moderator] [Microsoft_MVP] [MVP] Silver | Points: 25

Up
0
Down
Hi,

Check this code... I made some alteration

Dim sTempString As String = ""

Dim sExtensionValue As String = "TXT,DBF"
Dim strFilename As String = "My_Doc.docx"
Dim ss As String = ""
Dim X As Int16 = 0

If UCase(sExtensionValue) <> "ALL" Then
Dim aFileExtension() As String = Split(sExtensionValue, ",")
For X = LBound(aFileExtension) To UBound(aFileExtension) - 1
ss = UCase(aFileExtension(X).ToString)
sTempString = sTempString & "UCase(Mid(" & Chr(34) & strFilename & Chr(34) & ",InStrRev(" & Chr(34) & strFilename _
& Chr(34) & "," & Chr(34) & Chr(46) & Chr(34) & ",Len(" & Chr(34) & strFilename & Chr(34) & "))" & Chr(43) & Chr(49) & _
"," & Chr(51) & "))=" & Chr(34) & ss & Chr(34) & " Or "
Next
ss = UCase(aFileExtension(X).ToString)
sTempString = sTempString & "UCase(Mid(" & Chr(34) & strFilename & Chr(34) & ",InStrRev(" & Chr(34) & strFilename _
& Chr(34) & "," & Chr(34) & Chr(46) & Chr(34) & ",Len(" & Chr(34) & strFilename & Chr(34) & "))" & Chr(43) & Chr(49) & _
"," & Chr(51) & "))=" & Chr(34) & ss & Chr(34)

If Chr(32) & sTempString & Chr(32) Is Nothing Then
' In this routine, it comes here which is wrong while I am using Variables to Create the value of sTempString
MsgBox("Here I Am, Which Is wrong")
Else

MsgBox("Here I Am, Which Is Right")
End If
End If


Thanks
Karthik
www.f5Debug.net

Firstlast775, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Firstlast775 on: 1/14/2011 [Member] Starter | Points: 25

Up
0
Down
Hi Karthik,

Thanks for your interest and quick reply with some modification, appriciated.

I am changing the variable
1. Dim strFilename As String = "My_Doc.docx" OR
2. Dim strFilename As String = "My_Doc.dbf" OR
3. Dim strFilename As String = "My_Doc.txt"

for File extension, I considered
Dim sExtensionValue As String = "TXT,DBF"
where I didn't include the extension "DOC" OR "DOCX"

When I run the following modied code, always it goes through
MsgBox("Here I Am, Which Is Right")

It is true that if we assigned the value
Dim strFilename As String = "My_Doc.docx"
for
Dim sExtensionValue As String = "TXT,DBF"

Here extension "docx" is not included in sExtensionValue

Still it goes to
MsgBox("Here I Am, Which Is Right") which is not right.

It should go through
MsgBox("Here I Am, Which Is Wrong")

Is it not?
====================================================================================================================
Dim sTempString As String = ""

Dim sExtensionValue As String = "TXT,DBF"

Dim strFilename As String = "My_Doc.docx"

Dim ss As String = ""

Dim X As Int16 = 0



If UCase(sExtensionValue) <> "ALL" Then

Dim aFileExtension() As String = Split(sExtensionValue, ",")

For X = LBound(aFileExtension) To UBound(aFileExtension) - 1

ss = UCase(aFileExtension(X).ToString)

sTempString = sTempString & "UCase(Mid(" & Chr(34) & strFilename & Chr(34) & ",InStrRev(" & Chr(34) & strFilename _

& Chr(34) & "," & Chr(34) & Chr(46) & Chr(34) & ",Len(" & Chr(34) & strFilename & Chr(34) & "))" & Chr(43) & Chr(49) & _

"," & Chr(51) & "))=" & Chr(34) & ss & Chr(34) & " Or "

Next

ss = UCase(aFileExtension(X).ToString)

sTempString = sTempString & "UCase(Mid(" & Chr(34) & strFilename & Chr(34) & ",InStrRev(" & Chr(34) & strFilename _

& Chr(34) & "," & Chr(34) & Chr(46) & Chr(34) & ",Len(" & Chr(34) & strFilename & Chr(34) & "))" & Chr(43) & Chr(49) & _

"," & Chr(51) & "))=" & Chr(34) & ss & Chr(34)



If Chr(32) & sTempString & Chr(32) Is Nothing Then
' In this routine, it comes here which is wrong while I am using Variables to Create the value of sTempString
MsgBox("Here I Am, Which Is wrong")
Else
MsgBox("Here I Am, Which Is Right")
End If

End If

Thanks again for your time and effort.

Firstlast775, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Karthikanbarasan on: 1/15/2011 [Member] [Moderator] [Microsoft_MVP] [MVP] Silver | Points: 25

Up
0
Down
Hi,

Can you explain your requirement on why you have written this code... so that i can suggest you some best solution... since im not getting you properly.

Thanks
Karthik
www.f5Debug.net

Firstlast775, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Firstlast775 on: 1/15/2011 [Member] Starter | Points: 25

Up
0
Down
Hello:

I am trying to read all the files from any pre-defined folder (C:\TEMP\A1) and its all the Sub-Directories
(C:\TEMP\A1\A21; C:\TEMP\A1\A21\A31; C:\TEMP\A1\A22; C:\TEMP\A1\A22\A32 ) copying those filtered Extensions as defined in
Dim sExtensionValue As String = "TXT,DBF"
to another location (J:\TEMP). The copying procedure is copying to Folder to Folder and its sub-folder to sub-folder.

However, if I use Variable to create the IF..Else..Endif, then it does not work. But if I take the Final value ot the Variable and put into like

Dim sTempString As String = ""
Dim sExtensionValue As String = "txt,dbf"

Dim strFilename As String = "My_Doc.DOCX"
Dim ss As String = ""

If " " & sTempString & " " Then
' In this routine, it comes here which is wrong while I am using Variables to Create the value of sTempString
MsgBox("Here I Am, Which Is Wrong")
Else
MsgBox("Here I Am, Which Is Right")
End If

Where
sTempString =UCase(Mid("My_Doc.DOCX", InStrRev("My_Doc.DOCX", ".", Len("My_Doc.DOCX")) + 1, 3)) = "TXT" Or _
UCase(Mid("My_Doc.DOCX", InStrRev("My_Doc.DOCX", ".", Len("My_Doc.DOCX")) + 1, 3)) = "DBF"

Again, I am using the value of sTempString and puto the following fashion, then it works
If UCase(Mid("My_Doc.DOCX", InStrRev("My_Doc.DOCX", ".", Len("My_Doc.DOCX")) + 1, 3)) = "TXT" Or _
UCase(Mid("My_Doc.DOCX", InStrRev("My_Doc.DOCX", ".", Len("My_Doc.DOCX")) + 1, 3)) = "DBF" Then
MsgBox("Here I Am, Which Is Wrong")
Else
' In this routine, it comes here which is right
MsgBox("Here I Am, Which Is Right")
End If

My assumption is to update somewhere into the following code in such a manner that the value of sTempString would work in If...Else..Endif

Dim aFileExtension() As String = Split(sExtensionValue, Chr(44))
For X = LBound(aFileExtension) To UBound(aFileExtension) - 1
ss = UCase(aFileExtension(X).ToString)

sTempString = sTempString & "UCase(Mid(" & Chr(34) & strFilename & Chr(34) & ",InStrRev(" & Chr(34) & strFilename _
& Chr(34) & "," & Chr(34) & Chr(46) & Chr(34) & ",Len(" & Chr(34) & strFilename _
& Chr(34) & "))" & Chr(43) & Chr(49) & "," & Chr(51) & "))=" & Chr(34) & ss & Chr(34) & " Or "
Next
ss = UCase(aFileExtension(X).ToString)
sTempString = sTempString & "UCase(Mid(" & Chr(34) & strFilename & Chr(34) & ",InStrRev(" & Chr(34) & strFilename _
& Chr(34) & "," & Chr(34) & Chr(46) & Chr(34) & ",Len(" & Chr(34) & strFilename _
& Chr(34) & "))" & Chr(43) & Chr(49) & "," & Chr(51) & "))=" & Chr(34) & ss & Chr(34)
Next

I need to get through this problem.

Thanks for your time and effort for anticipation.

Regards.

Firstlast775, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Karthikanbarasan on: 1/15/2011 [Member] [Moderator] [Microsoft_MVP] [MVP] Silver | Points: 25

Up
0
Down
Hi,

I got your requirement. if im not wrong, u want to copy files from one folder to the other based on the file extension. For this u can use a simple solution instead of going on like this... This is a straight forward tool available u can download and use it or take the code for your requirement.

http://www.codeproject.com/KB/dotnet/FileSegregator.aspx

Thanks
Karthik
www.f5Debug.net

Firstlast775, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Karthikanbarasan on: 1/15/2011 [Member] [Moderator] [Microsoft_MVP] [MVP] Silver | Points: 25

Up
0
Down
Hi,

You can also use this straight forward method of getting the files with based on the file extension.

Dim files() As String

files = System.IO.Directory.GetFiles("C:\windows", "*.bmp")
Dim file as string
For Each file In files
System.IO.File.Copy(file, "c:\destination\" & System.IO.Path.GetFileName(file))
Next


Thanks
Karthik
www.f5Debug.net

Firstlast775, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Firstlast775 on: 1/15/2011 [Member] Starter | Points: 25

Up
0
Down
Hi Karthik,

Thank you very much for your time and efforts.

I have already very close to your code, but I was trying to use full variable driven using in Class Module. But some unknown reason, we couldn't get there.
I will not give uo on this issue.

However, I will design my exercise with an old fashion way.

Thanks again for your interest. I am going to download now from your supplied link for further information.

Regards.

Firstlast775, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response