Test-Path Weirdness

Part of this script I’m working on checks to confirm the existence of a set of directories. In order to make sure I didn’t type any of the directories wrong I ran dir /B > dir.txt and used the output for an array $DirList. For some reason the script tells me many of the directories don’t exist.

longer post deleted

I think I found the issue. The last directory, the project directory, contains a series of numbers separated by a dash, like this:

10-463527

Then in another part of that same string, there’s another dash between some words, like this:

CompanyXYZ - Engineering Study

(edit: so that final directory name could be \10-463527 CompanyXYZ - Engineering Study)

The paths copied out of dir.txt (using both Notepad and a text editor called ‘Ultraedit’) return FALSE, vs the ones that are copied out of an explorer window which return TRUE. I ran the good and bad paths through a unicode tester and I came up with this: that dash between the words has a Unicode value of “U+002D : HYPHEN-MINUS {hyphen or minus sign}” but the dash between the words that came out of explorer is “U+2013 : EN DASH”. That was the only difference between the two paths.

The dash between the numbers was the same in both strings, U+002D.

So it’s not that there’s a minus sign in the directory path. It’s where the minus sign is, apparently? If it’s between some numbers, that’s OK. If it’s between some words, failure.

Is there some way to prevent this from happening? I’ve been getting directory lists from the command prompt for decades, never had an issue before now.

Is this a known thing? How would I prevent it from happening in the future?

I created the path you mention and had no issues with Test-Path. Perhaps the issue is what you do with the contents of the file, can you share the code?

There’s no file. It’s just a directory, and I made those up in this post and typed them manually.

Try doing the same thing but use the Unicode characters I mentioned. When the dash is between words, for whatever reason I have to use U+2013.

I’ll paste the offending hyphen into a couple mockup directories here:
Bad:
10-13423 DINGOCO - HYDRAULIC CALC STUDY

Good:
10-13423 DINGOCO – HYDRAULIC CALC STUDY

Hopefully it comes through correctly.

Bad Hyphen (between words): -
Good Hyphen (between words): –

I can see in the message entry panel here that those two dashes are visually different. Don’t know if it’ll survive after hitting ‘reply’. :slight_smile:

Thank you for taking a look at this!

Sorry, the file I was referring to was the one you mentioned in your post, dir.txt … sorry for my confusion. Can you show how you are reading the contents of that file in your code and how you are using the results in your Test-Path call?

1 Like

Again, really appreciate you sticking with me on this! :slight_smile:

$DirList = @( "\\server\dir\dir\dir\10-13423 DINGOCO – HYDRAULIC CALC STUDY",
…more directories… last one is formatted:
“\server\dir\dir\dir\10-13423 DINGOCO – HYDRAULIC CALC STUDY”)

Then the test:
if(!(Test-Path $SourcePath)) {
Write-Host
Write-Host “Source directory does not exist!”
Write-Host $SourcePath
Exit
}

$SourcePath is the directory from the array with a “\” added on to the end.

All that being said, the ONLY difference between a string that is accepted, and one that is rejected, is that hyphen. The source of the “bad” hyphen came from a DOS command prompt into a text file created by that DOS command, and the good one came from me copying the directory out of a Windows Explorer window and pasting it into the PS script editor.

Lemme ask a different question, what’s another way of creating a directory list in a file? I’ve tried to navigate from the powershell window but I can’t get to a network directory because, heh, I don’t know how. I just type ‘net-use’ at the command prompt and my mapped drives are then “known” by the DOS window automagically.

OK, this is getting more interesting. I managed to Set-Location and get over to the directory and I used this to get a directory list:

dir -Directory | Select FullName | Export-Csv “dir.csv”

And wherever there was a “bad” hyphen, Excel interpreted that as a question mark, so I’m seeing this in Excel:

10-13423 DINGOCO ? HYDRAULIC CALC STUDY

Interesting…

edit: so the BAD directory names are the ones using “U+2013 : EN DASH” I think. I wonder how that happened?

So I think we can put this one to bed. Someone created a bunch of directory names, or restored them, modified them somehow, and the script they used created a weird hyphen that didn’t get interpreted correctly when I dumped the directory names. I don’t know what to do about this really, but at least it’s something to be aware of. Maybe I create a little routine to swap out stupid hyphens with good ones.

Sadly, I am unable to reproduce the behavior you are seeing, likely as you state, what you are really seeing is not coming through in this post. I created a folder using the string you provided for “bad” - “10-13423 DINGOCO - HYDRAULIC CALC STUDY” and Test-Path still returns true.

Sorry for not being any help. I wonder if this is related to what Word does with hyphens when you create tables in word or outlook and it “magically” changes them to the longer hyphen. Again, sorry for not being any help.

1 Like

That might be caused by not using the formatting as code.

So please @DADMODE1000 when you post code, console output, error messages or sample data - format it as code:
Simply place your cursor on an empty line, clickt the preformatted text button ( </> ) and paste your code or whatever. It’s really not that hard. :wink:

1 Like

No worries!! I really appreciate you trying! My problem is that I worry too much about copying something internal to the company and pasting it in a forum somewhere. I try to avoid being “tracked” although it’s probably stupid that I use my real name lol.

Olaf you are correct, I couldn’t figure out how to post the code and such. I figured it out a month ago but today the brain no workie so good. I sent a message to an admin-type person asking for a “formatting for n00bs” post but didn’t hear back yet.

So lemme try to reformat things, and, thanks again guys. Like I said, I think the issue is not the text editors, or powershell, but the way I’m copying directory names. If I copy from a window, it’s fine. If I get powershell or a command prompt to make a text list for me, it gets wonky.

I’ll just copy the bits that are throwing everything off.

Output from PS (Directory Export-CSV)

DISCOVERY ? PIPELINE

Output from command prompt

DISCOVERY - PIPELINE

Copied from a window

DISCOVERY – PIPELINE

I don’t know if the different codes will come through or not. The whole problem is that powershell needs the right character, and the different ways of copying a directory name into a ps script result in the correct, or not correct, character being used.

If what I pasted there isn’t working for you, just make a directory with Unicode U+2013, and a directory with Unicode U+002D, and then Test-Path using only Unicode U+002D. The directory with the hyphen that’s U+2013 will fail of course, and all of this I guess is pretty obvious at this point. It’s just… weird… that some directories have a different hyphen. idk what folks are doing around here… :slight_smile:

Hmmm … I think the recommendation for those kind of problems would be not to use any word processor with an automatic format system like for example MSFT Word. There are at least 4 different dash types and some of them are hard to distinguish visually.

You may tell us - without giving any sensitive information of course - what the actual task ist you want to achieve. We might be able to help you around this issue.

If it’s about comparing folder structures you may collect the folder lists of both sides with PowerShell and avoid misstakes with the formatting of the dashes. Maybe you could avoid using dashes in the future. Maybe an underscore is less error prone. :wink:

1 Like

duh! For the 1st time in my life I appreciate an _ over a - or – or even my favorite —.