How to verify if a string is a valid filename and/or path

Trying to verify if a given string is a valid path and filename, I wrote a horrible regex only to learn that Go does not support positive look ahead. After reading the discussions on the topic, I understand the rationales why but I have still not been able to come up with a reasonable way of doing it.

I was hoping that path/filepath would be the place to be but as I am reluctant to try and open a file, that did not help me since all I want to know is if a string actually forms a valid path regardless of whether there’s a file at that path or not.

Here’s what I am trying to do:

  • Verify if a given string is a valid path without having to access a file or create one
  • Be able to do this under Linux and/or Windows without having to worry about the underlying os

Btw, here is a part of the horrible regex-code - enjoy:

case runtime.GOOS == "windows":
regExpString = `(^([a-z]|[A-Z]):(?=\\(?![\0-\37<>:"/\\|?*])|\/(?![\0-\37<>:"/\\|?*])|$)|^\\(?=[\\\/][^\0-\37<>:"/\\|?*]+)|^(?=(\\|\/)$)|^\.(?=(\\|\/)$)|^\.\.(?=(\\|\/)$)|^(?=(\\|\/)[^\0-\37<>:"/\\|?*]+)|^\.(?=(\\|\/)[^\0-\37<>:"/\\|?*]+)|^\.\.(?=(\\|\/)[^\0-\37<>:"/\\|?*]+))((\\|\/)[^\0-\37<>:"/\\|?*]+|(\\|\/)$)*()$`
reg, err := regexp.Compile(regExpString)
  if err != nil {
	return (err)
  }
 if reg.MatchString(fileString) {
	err := doSomethingWithTheFile(fileString)
	if err != nil {
	  return (err)
	}
	return (nil)
}

Needless to say, the error was "error parsing regexp: invalid or unsupported Perl syntax: (?="

This is a seemingly trivial problem, but…

br/brusan

Why actually check?

Modern filesystems can handle a lot of names… Let’s take NTFS, it can do much more than windows allows…

Writing on NTFS from a Linux can create pathes containing colons, those pathes can get very long while windows has this arbitrary restriction of 1024 or so characters.

So, just try to open the file for reading or writing and hope for success.

Yeah I wouldn’t suggest doing this either. On Windows you can easily check for the presence of the disallowed characters and reject names with those early. On Unix the only disallowed character is nul. However, there are other restrictions you can’t know beforehand. Apart from some otherwise valid names being disallowed on Windows (nul, aux, com1, …) the filesystem may enforce UTF-8 for example. A valid file name may also not work due to permissions, or a file being “in the way” of a required directory.

Calling os.Stat would be my way to check. Nil error or os.IsNotExist probably indicates a valid file name. Other errors indicate a problem.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.