0

My application has objects called "Instruments" which are representations of Windows machines connected to some lab equipment. A couple properties of these Instruments are filepath-related. My application therefore needs to check the validity of a Windows path, but I want my application to run on both Windows and Linux environments.

I'm aware of the Path class and its methods are almost what I want, but they are OS-dependent. The checks fail when my application is running on a Linux environment. I need something that checks for Windows path validity (and absoluteness) regardless of the host OS. Is there a class that can help me or do I have to create my own implementation?

John Kugelman
  • 349,597
  • 67
  • 533
  • 578
dsd24
  • 23
  • 3
  • This is basically what you are looking for (just not in Java): https://stackoverflow.com/questions/6416065/c-sharp-regex-for-file-paths-e-g-c-test-test-exe – ControlAltDel Mar 09 '20 at 17:47
  • 1
    Have you considered `System.getProperty("os.name")`? That will allow you to determine which os you're on anyway. – Elliott Frisch Mar 09 '20 at 17:49
  • Could you give an example? So you are checking windows file paths on linux? – ZGorlock Mar 09 '20 at 17:51
  • @ControlAltDel Yeah, I'm thinking I'll probably just have to use a regex to check. Thanks for linking. – dsd24 Mar 09 '20 at 17:51
  • 1
    Define "validity". Do you mean to check if the given path exists on the current machine? On a remote machine? That it's generally in the appropriate syntax for a Windows machine, such as `C:\My Path That Does Not\Really Exist\ `? Which methods you think would work for you in the `Path` class? – RealSkeptic Mar 09 '20 at 17:52
  • @ZGorlock Yes, I'm checking Windows paths on both Linux and Windows. An example would be my application is running on Linux and I want to check that the path "C:\Instrument Data" is a valid and absolute path. Using java's built-in Path.isAbsolute(), this would return false. – dsd24 Mar 09 '20 at 17:53
  • @RealSkeptic No, the given path does not have to exist, it just has to be a syntactically correct Windows path to be "valid" in my case. The methods that would work are Paths.get() and Path.isAbsolute() – dsd24 Mar 09 '20 at 17:54
  • 1
    I do not have a linux machine to test on right now, but I assume you could just take the windows file and remove the drive name and replace the \\ with / to check on linux. File a = new File("C:\\Instrument Data"); File b = new File(a.getAbsolutePath().replaceAll("^[A-Z]:", "").replace("\\", "/")); System.out.println(b.toPath().isAbsolute()); – ZGorlock Mar 09 '20 at 17:58
  • 1
    @ZGorlock there may be characters (such as `nul`) which are invalid in Linux filesystems and valid in Windows or vice versa. I would not rely on `Path` at all, but formulate an expression for what a valid path is. I basically think this is probably useless, as the path may not exist on the machine where it is intended to be used, and still be valid. But if just checking validity is required, then do not rely on the local filesystem to resolve it in any form. – RealSkeptic Mar 09 '20 at 18:06
  • I don’t understand what you need. An absolute path has to be different on Windows from an absolute path on Linux. Does the path has a lot of directory components which are the same on both systems, but relative to a root that depends on the OS? – VGR Mar 09 '20 at 19:13
  • @VGR Not sure how I can explain it more clearly. I need to check that a string is a valid windows path (and an absolute windows path) with code that can be executable on both windows and linux machines. The file path in question is not tied to the machine that executes the code. – dsd24 Mar 09 '20 at 20:30
  • 1
    I understand now. I don’t think there is any way to make use of the Windows FileSystem provider on non-Windows systems, so you’ll probably have to make do with a regular expression. – VGR Mar 09 '20 at 20:32
  • Maybe I'm missing something here. Isn't this what `new File(pathString).isAbsolute();` is used for? – DevilsHnd - 退職した Mar 09 '20 at 20:39

1 Answers1

1

It took me a while to understand what you’re asking for. You want to check whether a path is a valid absolute path for Windows, even when running on non-Windows systems.

I don’t think there is any way to access to the FileSystem provider for Windows when running on non-Windows systems. So I would just make do with a regular expression:

boolean validWindowsAbsolutePath =
    path.matches(
        "([a-zA-Z]:|\\\\)" +
        "(\\\\[^<>:\"/\\\\|?*\u0000-\u001f]+)+")
    &&
    !path.matches(
        "(|.*\\\\)(?i:con|prn|aux|nul|com[1-9]|lpt[1-9])" +
        "(\\.[^.]*)?");

Reference: Naming Files, Paths, and Namespaces

VGR
  • 40,506
  • 4
  • 48
  • 63