How to get all Filenames on Directory in VB.NET - vb.net

I want to show all PDF filenames that has "33" in any position.
sample pdf list on PDFFiles Folder
1111.pdf
3311.pdf
2222.pdf
2331.pdf
1234.pdf
1233.pdf
I need to get result like this,that is something like wildcard %33% on sql
3311.pdf
2331.pdf
1233.pdf
I tried this one
Me.ListBox1.Items.AddRange(Directory.GetFiles("C:\PDFFiles", "*33*" & ".PDF", SearchOption.AllDirectories))
but it still displays all the pdf files.
1111.pdf
3311.pdf
2222.pdf
2331.pdf
1234.pdf
1233.pdf
and this
Me.ListBox1.Items.AddRange(Directory.GetFiles("C:\PDFFiles", "*33" & ".PDF", SearchOption.AllDirectories))
but it only get
1233.pdf //this get all filename that ends with 33
Thanks in Regards

The pattern matching algorithm for wildcards are rather strange if you are used to regular expressions. There's a lot of history behind it, going back through Windows 3, MS-DOS, CP/M (an operating system for 8-bit machines) and RSX (an operating system on 16-bit DEC machines). With heavy borrowing between them, including the wildcard behavior. Some accidental commonality btw, David Cutler was the principal architect behind the first and the last one.
Anyhoo, *33* isn't going to work. You'll need to apply your own filter. Search for *.* or *.pdf and use Path.GetFileNameWithoutExtension() and String.Contains() to find the matches.

I have noticed this behavior too when using more than one *.
I solved it by getting all file names and then filtering the correct names by using LINQ:
Dim allFileNames as String() = _
Directory.GetFiles("C:\PDFFiles", "*.PDF", SearchOption.AllDirectories)
Dim filtered As IEnumerable(Of String) = _
.Where(Function(fileName) Path.GetFileNameWithoutExtension(fileName).Contains("33"))

You are missing * at the end of "*33", put one more star at the end of like "*33*".
Your current expression : "*33" & ".PDF" means, All file names which ends with 33.PDF that is why you are getting 1233.pdf and not 2331.pdf
EDIT:
Directory.GetFileName()
Search pattern similar to "*1*.txt" may return unexpected file names.
For example, using a search pattern of "1.txt" returns
"longfilename.txt" because the equivalent 8.3 file name format is
"LONGFI~1.TXT".

We have found out that
Filter *___* works if the string has lenght greater than or equal to 4.
So if i want to get all records that has test name value:
test1234.pdf
abcdefg.pdf
123test45.pdf
12345678.pdf
My filter should be: "*test" & ".PDF" it will give the desired result
test1234.pdf
123test45.pdf
FYI

Related

Illegal characters in path (Chinese characters)

Getting an "Illegal characters in path" with Directory.GetFiles:
files = Directory.GetFiles(folderName & invoiceFile & "*.pdf")
Given the actual values, the filenames would be like so:
x:\folder1\請 010203.pdf
y:\foldera\folderb\請 040506.pdf
z:\xyz\abc\請 119906.pdf
Hence the * wildcard. Can I use Chinese characters with Directory.GetFiles? I think I can since I was able to use it on a separate VBA project before using ChrW(35531) so I think it shouldn't be a problem with .NET. Anyone know a fix for this?
You need to use Directory.GetFiles Method (String, String), like this:
files = Directory.GetFiles(folderName, invoiceFile & "*.pdf")
Note that the folder name and the filter are separate parameters.

How to use FILE_MASK parameter in FM EPS2_GET_DIRECTORY_LISTING

I am trying to filter files using FILE_MASK parameter in EPS2_GET_DIRECTORY_LISTING to reduce time searching all files in the folder (has thousands of files).
File mask I tried:
TK5_*20150811*
file name in the folder is;
TK5_Invoic_828243P_20150811111946364.xml.asc
But it exports all files to DIR_LIST table, so nothing filtered.
But when I try with;
TK5_Invoic*20150811*
It works!
What I think is it works if I give first 10 characters as it is. But in my case I do not have first 10 characters always.
Can you give me an advice on using FILE_MASK?
Haven’t tried, but this sounds plausible:
https://archive.sap.com/discussions/thread/3470593
The * wildcard may only be used at the end of the search string.
It is not specified, what a '*' matches to, when it is not the last non-space character in the FILE parameter value.

Extracting Directory String from Text

I have a program that I am making with visual basic 2010 that will pull logs of corrupted files and give the user the location of the corrupted file(s) to fix it. These logs are huge and vary depending on the amount of corruption.
I already have set in code to only pull the lines of text that are flagged as errors but, within these lines, there are directories that point to what file is corrupted. I need to know if there is any way to read these directories and put them into a RichTextBox. Here is an example of a line from a log file:
oa = #0x238282b270->OBJECT_ATTRIBUTES {s:48; rd:NULL; on:[100]"\??\C:\Windows\WinSxS\amd64_3ware.inf.resources_31bf3856ad364e35_10.0.10130.0_en-us_ca9e7cc7a071e60f"; a:(OBJ_CASE_INSENSITIVE)}, iosb = #0x238282b250, as = (null), fa = 0,
And here is the part that I need to pull from it:
C:\Windows\WinSxS\amd64_3ware.inf.resources_31bf3856ad364e35_10.0.10130.0_en-us_ca9e7cc7a071e60f from this string
I'm pretty new to all of this, so bear with me please.
RegEx provides great flexibility for this sort of thing, but you need to establish a known pattern that defines where the path begins and ends. For instance, if it always is prefixed by on:[100]"\??\ and always ends with ";, then you could extract it with this RegEx pattern:
on:[100]"\\?\?\(.*?)";
Here's what the pattern means:
on:\[100\]"\\\?\?\\ - Matches must begin with on:[100]"\??\ exactly
The extra backslashes are necessary to escape all of the special characters which would otherwise have special meaning. In this case, [, ], \, and ? all have special meaning to RegEx, so they each need to be preceded a the backslash to escape them.
(.*?) - Matches can contain any number of any characters between the preceding on:[100]"\??\ and the following ";. The value of this portion of the input is captured as an unnamed group (i.e. group 1).
( - Begins a capturing group
. - Matches any character
* - Any number of times
? - Matches in a non-greedy fashion (i.e. only captures up through the first instance of whatever follows it in the pattern)
) - Ends the capturing group
"; - Matches must end with these two characters exactly
So, for instance:
Dim input As String = "oa = #0x238282b270->OBJECT_ATTRIBUTES {s:48; rd:NULL; on:[100]""\??\C:\Windows\WinSxS\amd64_3ware.inf.resources_31bf3856ad364e35_10.0.10130.0_en-us_ca9e7cc7a071e60f""; a:(OBJ_CASE_INSENSITIVE)}, iosb = #0x238282b250, as = (null), fa = 0,"
Dim m As Match = Regex.Match(input, "on:\[100\]""\\\?\?\\(.*?)"";")
If m.Success Then
Dim path As String = m.Groups(1).Value
End If
Or, if the input can contain multiple matches, you can loop through them like this:
For Each m As Match In Regex.Matches(input, "on:\[100\]""\\\?\?\\(.*?)"";")
Dim path As String = m.Groups(1).Value
Next
That's just an example. Depending upon your needs, you could adjust the RegEx pattern as necessary. RegEx is very flexible, so as long as there's some logical way to recognize where the path is in the string, it should be possible to find it with a RegEx pattern. On a side note, since the pattern is, itself, just a string, it can be stored in a configuration setting outside of the code too, which is an added benefit.

Removing blank line at end of string before writing to text file?

Been searching around for this for a couple hours, can't find anything which will do this correctly. When writing a string to a text file, a blank line is outputted at the end.
writeString = New StreamWriter(path, False)
writeString.WriteLine("Hello World")
writeString.Flush()
writeString.Close()
This will write the following to file:
Hello World
(Blank Line)
I've tried removing last character of string (both as regular string with varString.Substring(0, varString.Length - 1) and also as a list of string with varList.RemoveAt(varList.Count - 1)) but it just removes the literal last character.
I've also tried using Replace(vbCrLf, "") and many variations of it but again, they only remove literal new lines created in the string, not the new line at the end that is magically created.
Preferably, I'm seeking a method which will be able to remove that magical newline before the string is ever written to the file. I found methods which read from the file and then write back to it which would require Write > Read > Write, but in all cases the magical new line still appeared. :(
If it's important to note: The file will contain a string which may contain actual new lines (it's 'Song Artist - Song Title', though can contain other information and new lines can be added if the user wishes). That text file is then read by other applications (such as mIRC etc) of which output the contents by various means depending on application.
Eg. If an application were to read it and output it into a textbox.. the new line will additionally output to that textbox.. which is a problem! I have no control of the applications which will read the file as input considering it's the client which decides the application, so the removal of the new line needs to be done when outputted.
Help is appreciated~!
Use the Write method instead of WriteLine. The WriteLine method is the one adding a blank 0 length line to the file because it is terminating the "Hello World" string with a newline.
writeString.Write("Hello World")

Tool to format lines of text into array

I frequently come across this problem. I have a file:
something
something2
something3
which I want output as:
"something","something2","something3"
any quick tool for this, preferably online?
If its just a one off thing, it'd be pretty easy to just do it with a search & replace in any advanced-ish text editor...
For example in notepad++:
Do a Replace (CTRL+H)
Set "Search Mode" to "Extended"
Find: \r\n
Replace with: ","
(of course you'll need an extra quote at the very start & very end of the file).
If you need to do it more than once, writing a small script/program that did a regular expression replace over the file would be fairly straight forward too.
Edit: If you really wanted to do it online, you could use an online regular expression tester (in this case you want to use \n as the regex and "," as your replace pattern, leaving the other settings alone).
A quick Python hack?
lines = open('input.txt').xreadlines()
txt = ','.join(['"%s"' % x for x in lines])
open('output.txt', 'w').write(txt)