Stefan2 wrote: 2020-02-06, 20:05 UTC
On which operation system do you fail? Your server is an unix system. yes?
The WindowsTM API do not support trailing blanks on file names. Or did it work with WinExplorer?
While this seems to be correct on the surface, this is not entirely accurate. The Windows API allows you to create (and use) both directories and files(!) having names with trailing spaces. (Windows API != Windows Explorer)
More accurately, many (if not most/all, but who knows) Windows API with parameter(s) for path strings accept any directory/file name with trailing spaces as long such a name is not the very last(!) component in the path string.
A few simple .NET one-liners will demonstrate this (doesn't matter whether you use Core, .NET Framework, or execute it within a PS environment; if you so desire, feel free to translate it into the actual WinAPI calls, it will yield the same results when done correctly):
Code: Select all
System.IO.Directory.CreateDirectory(@"X:\SomeFolderWithTrailingSpaces ");
Note that the folder is created with a name without trailing space, because it is the last component in the path string.
The folder with a trailing space can be created by not letting it be the last component of the path string. Which can be achieved by adding a final trailing backslash to the path string:
Code: Select all
System.IO.Directory.CreateDirectory(@"X:\SomeFolderWithTrailingSpaces \");
Then, you can create another sub-directory, also with a trailing space:
Code: Select all
System.IO.Directory.CreateDirectory(@"X:\SomeFolderWithTrailingSpaces \OneMore \");
and so on...
Now, how about file names? A file name is usually the last component of a path string, so
Code: Select all
System.IO.File.Create(@"X:\SomeFileWithTrailingSpace.txt ");
wouldn't really work. Adding a backslash at the end of the file name obviously doesn't work here, either. Your claim seems to be true after all. But wait! You can exploit the data stream notation (which is supported at least by NTFS). Now, you could just some random data stream name like this:
Code: Select all
System.IO.File.Create(@"X:\SomeFileWithTrailingSpace.txt :SomeSecondaryDataStreamName");
But what if you want to write stuff into the normal default data stream (i.e., the "normal file content" stuff). Well just specify the default data stream instead of "SomeSecondaryDataStreamName":
Code: Select all
System.IO.File.Create(@"X:\SomeFileWithTrailingSpace.txt ::$DATA");
Et voila. This way you can create or open a file having a name with a trailing space by using nothing else than the Windows API.
I admit, it is a bit cheeky, but that doesn't mean it doesn't work.
Still, there is a fair agrument to be had about the difference between something that is possible to do vs. something that is (officially) supported. And since i expect several Windows components/programs having bad troubles dealing with such file/directory names, i absolutely do not recommend to anyone to ever utilize such bastardized file/directory names. At least not on Windows
