0

I have a script which allows the user to specify a location for a file which the script will write to. I want to check that the given string represents an absolute, rather than relative, file path. So this would pass:

C:\Folder\MyFile.txt

But these would fail:

.\Folder\MyFile.txt
..\MyFile.txt
MyFile.txt

Regardless of whether those files/folders exist already.

I'm not concerned with checking for e.g. invalid characters (which other questions I found while searching were concerned with). I could use a quick and dirty regular expression to check it starts with "^[a-zA-Z]:\" but I was wondering if the functionality already existed in PowerShell to do this.

If the script detects it's not an absolute path, the script will then convert it to a full path by inserting the full path of the current directory - so if there's a function which can do that directly then that will also be useful.

Scott
  • 185
  • 2
  • 11
  • You can use commands like [this](http://technet.microsoft.com/ru-RU/library/hh849858.aspx) or [this](http://technet.microsoft.com/ru-RU/library/hh849856.aspx) to perform path conversions. I also strongly recommend you to use `Get-Help **` to get all available commands and then you can use `Get-Help -online` to get full command description with examples. – kravasb Dec 16 '13 at 12:29
  • Resolve-Path and Convert-Path both require the specified file/folder to exist already, I want something which doesn't require that. I didn't know about the -online parameter though. – Scott Dec 16 '13 at 12:55
  • See this answer: http://stackoverflow.com/a/3040982/291709 – Rynant Dec 16 '13 at 14:04
  • Perfect, thanks. Have flagged as duplicate. – Scott Dec 16 '13 at 14:08

0 Answers0