VB.net file reading tricks? - vb.net

I'm rather new to VB and I was wondering if you could help me out with something. I've got a file that I need to read and edit, looks something like this:
Name|Ted
SetAttackBaseDamage(251,1)
{2|73|10|-1|20,40}
I need to be able to load the data here (What will be changing at least) into a variable so that I can change it, almost like what happens in XML except with this. The way the file is set up can't be changed as I'm edit files that are produced by another program, also written in VB.net though it isn't open source.

Assumed that you've 3 line by readline the file .. L1, L2, L3
dim sTmp as String
dim sName as String = L1.Split("|")(1) '--> this Name var
sTmp = L2.Split("(")(1)
dim sSA = split(sTmp.Replace(")", ""),",")
dim sSA1 as String = sSA(0) '---> this SetAttackBaseDamage - 1
dim sSA2 as String = sSA(1) '---> this SetAttackBaseDamage - 2
just wanna try to help you .. you have to de the rest ............

Related

For each loop keeps stopping on second cycle VB

The software I'm writing is being run in a service installed on a computer.
I want to read a text file, process it, and code it to a different path.
the software is doing exactly what it's supposed to do but it only processes 2 files and it stops. I believe that its something to do with the for each loop. I found some information online saying that its to do with the amount of memory being allocated to each cycle of the for each loop.
Any help is appreciated.
my code goes like this.
For Each foundFile As String In My.Computer.FileSystem.GetFiles("C:\Commsin\", FileIO.SearchOption.SearchTopLevelOnly, "ORDER-*.TXT")
Dim filenum As Integer
filenum = FreeFile()
FileOpen(filenum, foundFile, OpenMode.Input)
While Not EOF(filenum)
<do a bunch of stuff>
End While
<more code>
Dim arrayFileName() As String = GetFileName.Split("\")
Dim FileName As String = arrayFileName(2)
My.Computer.FileSystem.CopyFile(foundFile, "C:\Commsin\Done\" & FileName)
If IO.File.Exists("C:\Commsin\Done\" & FileName) Then
My.Computer.FileSystem.DeleteFile(foundFile, Microsoft.VisualBasic.FileIO.UIOption.AllDialogs, Microsoft.VisualBasic.FileIO.RecycleOption.SendToRecycleBin)
NoOfOrders -= NoOfOrders
End If
Next
Fundamental mistake: Don't modify the collection you are iterating over, i.e. avoid this pattern (pseudocode):
For Each thing In BunchOfThings:
SomeOperation()
BunchOfThings.Delete(thing)
Next thing
It's better to follow this pattern here (pseudocode again):
While Not BunchOfThings.IsEmpty()
thing = BunchOfThings.nextThing()
SomeOperation()
BunchOfThings.Delete(thing)
End While
I'll leave it as an exercise for you to convert your code from the first approach to the second.
It looks like you're trying to extract the filename from the full path using Split().
Why not just use:
Dim fileName As String = IO.Path.GetFileName(foundFile)
Instead of:
Dim arrayFileName() As String = GetFileName.Split("\")
Dim FileName As String = arrayFileName(2)
Thank you, everyone, for your suggestions, I have successfully implemented the recommended changes. It turned out that the issue wasn't with the code itself.
It was with one of the files I was using it had a text row that once split into an array it wasn't at a required length giving an error "Index was outside the bounds of the array."
It was a mistake on the file, I also added some check to prevent this error in the future.
Thank You.

Import semicolon separated CSV file using VBA without rename to txt

This is not a duplicate since I want a solution not constisting in reformatting file to txt:
my intention is to open a csv file using semicolon as delimiter. For that purpose I have used the following code:
Sub prueba2()
Dim sfile As String
Dim wb As Workbook
Dim Path As String
Dim Namefile As String
Path = "V:\evfilesce9i9\apps9\vbe9\dep4\KFTP\KFTP001D_FicherosCeca"
Namefile = "\QryCECARFSECTORIAL0239*.txt"
Set wb = Workbooks.Open(Filename:=Path & Namefile, Delimiter:=";")
End Sub
When I try it, it is opened using commas as delimiter instead of which I have specified (semicolon)
I have read in other questions that this is normal in post 2006 Excel versions, and that the fastest solution is to reformat file to a txt.
This does not fit into my needs because I have to do it without changing format. I don't find any solution.
Could someone help me?
Please see the MS documentation here.
I think you want to use the Format parameter, and not the delimiter parameter.
Try:
Set wb = Workbooks.Open(Filename:=Path & Namefile, Format:=4)
It seems like the Delimiter argument is only used if Format is set to 6, which signifies a custom delimiter character. Semi-colon is a standard delimiter.
Edit:
Hmm... so, this seems to be something that's been tricky in Excel/VBA for a while.
After some more research, the "Format" option may only be used when opening .txt files. Which is why the "reformat file to .txt" is one possible solution.
There are some things that can be done, however.
Excel will handle opening a semicolon delimited file well if the first line of the file is:
sep=;
I know you said you could not reformat the files, but is that something that you can do?
If not, the next things I would suggest would be to either: 1) use the Open Statement to open your file and then write it to a temporary file (perhaps as a .txt), to be reopened with the original Workbooks.Open(Format:=4), or 2) write your own text importer. A sample text importer can be found in this stackoverflow page.
Sub ImportCSVFile(filepath As String)
Dim line As String
Dim arrayOfElements
Dim linenumber As Integer
Dim elementnumber As Integer
Dim element As Variant
linenumber = 0
elementnumber = 0
Open filepath For Input As #1 ' Open file for input
Do While Not EOF(1) ' Loop until end of file
linenumber = linenumber + 1
Line Input #1, line
arrayOfElements = Split(line, ";")
elementnumber = 0
For Each element In arrayOfElements
elementnumber = elementnumber + 1
Cells(linenumber, elementnumber).Value = element
Next
Loop
Close #1 ' Close file.
End Sub

Vb.net unzip programme

I am trying to write an program to extract zipped files using vb.net. I am facing a problem - when I try to extract the files, it ask fro replacement agreement, but I need to extract that files without asking for my an agreement.
This is the code I use:
Dim shObj As Object = Activator.CreateInstance(Type.GetTypeFromProgID("Shell.Application"))
Dim outputFolder As String = appPath
Dim inputZip As String = appPath + "\patchFile.zip"
IO.Directory.CreateDirectory(outputFolder)
'Declare the folder where the items will be extracted.
Dim output As Object = shObj.NameSpace((outputFolder))
'Declare the input zip file.
Dim input As Object = shObj.NameSpace((inputZip))
'Extract the items from the zip file.
output.CopyHere((input.Items), 4)
I think you can use the second parameter of CopyHere to respond Yes To All for any dialog box displayed and it should avoid your dialog box.
output.CopyHere((input.Items), 16)
Edit :
And if you still need option 4 to not show progress, you can combine both so it would be 20 instead of 16

windows dropdown menu pass file name to vb.net program

I have a few files that pending on factors may require an alternate-variation to be used. The selection of the right file starts at standard windows directory C:\Drawings in my case, So I know that we can add items to the windows backbround context menu as follows:
Windows Registry Editor Version 5.00
[HKEY_CLASSES_ROOT\txtfile\shell\mymenu]
#="test123"
[HKEY_CLASSES_ROOT\txtfile\shell\mymenu\command]
#="%SystemRoot%\\system32\\NOTEPAD.EXE %1"
However (and im not sure even if this is possible) i would like to get the name of the file that was cliked and use it in my vb.net application for example a textbox wich displays that files name.
dose anybody know if i can do this? and how?
First you'll have to get the command line arguments, which can be done in a few different ways but I prefer to use Environment.GetCommandLineArgs():
Dim Arguments() As String = Environment.GetCommandLineArgs()
Then you must check that there's actually an argument to read. The very first argument (index 0) is always the path to your application, therefore we must check that it contains at least two arguments to be sure that there is also one passed to your app.
If Arguments.Length >= 2 Then
Finally you just get the path to the file from the second argument, and call IO.Path.GetFileName() on that:
Dim FilePath As String = Arguments(1) 'Second argument has index 1.
Dim FileName As String = IO.Path.GetFileName(FilePath)
If you don't want the file's path at all you can just go ahead and do:
Dim FileName As String = IO.Path.GetFileName(Arguments(1))
Full code:
Dim Arguments() As String = Environment.GetCommandLineArgs()
If Arguments.Length >= 2 Then
Dim FilePath As String = Arguments(1) 'Second argument has index 1.
Dim FileName As String = IO.Path.GetFileName(FilePath)
'Do your stuff here.
End If

.NET find .xls or.xlsx file with GetFiles using wildcard? example: .xls*

I need to find an Excel file. However, the extension of the file I"m looking for could be .xls or .xlsx. I was considering using FileExists but I can't use a wildcard with that. Here's my attempt at using GetFiles, however, the .xls* part of my code does not work. I've never used GetFiles before, can anyone give me some guidance on what I'm doing wrong?
Dim InputFormPath As String = "W:\TOM\ERIC\NET Dev\"
Dim wbNameXLSInputForm As String = StatVar.xlApp.Sheets("New Calculator Input").Range("D15").Text & ".xls*"
Dim XLSInputForm As String = wbNameXLSInputForm
Dim dirs As String() = Directory.GetFiles(InputFormPath, wbNameXLSInputForm)
If dirs.Length <> 0 Then
'do something
End If
Take a look at this documentation. It says: The following list shows the behavior of different lengths for the searchPattern parameter: "*.abc" returns files having an extension of.abc,.abcd,.abcde,.abcdef, and so on.