Illegal characters in path (Chinese characters) - vb.net

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.

Related

Visual Studio Code Snippet Variable Transform not working

I'm trying to make a snippet that inserts the last two directorys of the current filepath.
My code:
${TM_DIRECTORY/\\(.*)\\([a-zA-Z]+)\\([a-zA-Z]+)/$1\\$2/}
So when Filepath is
"...\htdocs\projectname\src"
the output should be
"projectname\src".
But instead I get this result:
${TM_DIRECTORY/(.*)\\([a-zA-Z]+)\\([a-zA-Z]+)/$1/}
What am I doing wrong?
Problem:
The issue is the code converts \\ to \. For example if you want to write \w, then you have to write \\win snippet.
The same way.. You have to write \\\\ in snippet json, so that it shall convert into //.
Solution:
${TM_DIRECTORY/.*?\\\\([a-zA-Z]+\\\\[a-zA-Z]+)$/$1/}
or, I think you should use \w instead of [a-zA-Z] because the directory name can contain some characters like - or _ etc.
${TM_DIRECTORY/.*?\\\\(\\w+\\\\\\w+)$/$1/}

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.

How to use escape character for a big string?

I have a big string, precisely - an XSLT code - that I would like to hardcode in my VB.net program. I tried with putting " before every quotation mark, but it still didn't work out, and it's pretty mocking to place it 100 times. Using Chr(34) is also not the best solution.
Is there some way, like to put # (or another character) before the string itself that will define and work for all the characters in the string that need to be escaped ?
If it is a large string. Why not save it to file and then read the file into memory before you want to use it. That way you don't have to do any escaping and it will be easy to modify if you decide to change it.

How to get all Filenames on Directory in 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

Pound sign (#) in filename causing error

I have a very simple file upload that allows users to upload PDF files. On another page I then reference those files through an anchor tag. However, it seems that when a user upload a file that contains the pound sign (#) it breaks the anchor tag. It doesn't cause any type of Coldfusion error, it just can't find the file. If I remove the #, it works just fine. I am sure there are a number of other characters that would have this same issue.
I've tried putting URLEncodedFormat() around the file name inside the anchor but that doesn't help. The only other thing I could think of was to rename the file each time it was uploaded and remove the "#" character (and any other "bad" character).
There has got to be an easier solution. Any ideas?
If you control the file upload code try validating the string with
IsValid("url",usersFileName) or
IsValid("regex",usersFileName,"[a-zA-Z0-9]")
Otherwise if you are comfortable with regex I would suggest something like the previous posters are commenting on
REReplace(usersfilename,"[^a-zA-Z0-9]","","ALL")
These samples assume you will add the ".pdf" and only allows letters and numbers. If you need underscores or the period it would look like this...
REReplace(usersfilename,"[^a-zA-Z0-9\._]","","ALL")
I am not a regex guru, if I have one of these wrong I am sure several will jump in and correct me :)
Pound signs are not legal within filenames on the web. They are used for in-page anchor targets:
<a name="target">
So if you have file#name.pdf, the browser is actually looking for the file "file" and the internal anchor "name.pdf".
Yes, you will have to rename your files on upload.
I can't comment yet,
but Kevink's solution is good unless you need to perserve what you're replacing.
We ran into an instance where we needed to rename the filename but the filename needed to be somewhat preserved (user requirement). Simply removing special characters wasn't an option. As a result we had to handle each replace individually, something like.
<cfset newName = replace(thisFile, "##", "(pound)", "All")>
<cfset newName = replace(newName , "&", "(amp)", "All")>
<cffile action="rename"source = "#ExpandPath("\uploads\#thisFolder#\#thisFile#")#" destination = "#newName#">
Probably you would have to replace # with ## to avoid this, I think this is caused because # is figured as Coldfusion keyword.