SOLVED

How include space in file path on PS command line?

Copper Contributor

Hi

I want get filesize that include space in file path.

I try that failed.

 

 

PS E:\> $size = [math]::Round($(Get-ChildItem "E:\MyDocuments\!Onedrive\OneDrive - Specified Nonprofit Organization Sample Japan\Sample.pdf").Length / 1MB, 3)
PS E:\> "$size MB"

 

 

 


Even if I surrounded it with ', it failed.

 

 

PS E:\> $size = [math]::Round($(Get-ChildItem "'E:\MyDocuments\!Onedrive\OneDrive - Specified Nonprofit Organization Sample Japan\Sample.pdf'").Length / 1MB, 3)
PS E:\> "$size MB"

 


How can I do it?

 
4 Replies
best response confirmed by pinefieldgene (Copper Contributor)
Solution

@pinefieldgene 

 

What is the exact error are you getting?

 

PowerShell has no issues with paths containing spaces, so with no error to refer to, I can't seen any issue.

 

I'd also point out that your final example isn't going to work. You can use double quotes or single quotes, but not both together as you have done in that example.

 

Double quotes are useful where you intend to use variables (and other special characters) inside of a string - as you have done with "$size MB", while single quotes are used where all PowerShell-specific special characters must be ignored.

 

What your second example is telling PowerShell to do is look for a single file in the current working directory that has an actual filename (not a path) of 'E:\MyDocuments\!Onedrive\OneDrive - Specified Nonprofit Organization Sample Japan\Sample.pdf', which isn't a valid filename.

 

Here's some examples of appropriate (first two commands) and inappropriate (final command) quote usage as well as a path containing spaces.

 

LainRobertson_0-1696078846485.png

 

Cheers,

Lain

Hi, can you explain a bit more what you’re trying to do? I’m a little bit confused.

 

If what you want to achieve is to get $size to be the value+" MB", then you can run this:

$size = ([math]::Round($(Get-ChildItem "E:\MyDocuments\!Onedrive\OneDrive - Specified Nonprofit Organization Sample Japan\Sample.pdf").Length / 1MB, 3)).tostring()+" MB"

But that will recast the $size variable to be a string, so you can't use it for comparisons without splitting it and making it back into a number.

 

-Ole

Hello.
I have tried several times to get the file size even if the path contains spaces in the file.

@randriksen_ 

Thanks for suggesting another way.
I was running the second line of the command

PS E:\> "$size MB"

with PowerAutomateDesktop and just listed it as it is. Sorry for the confusion.

Image_20231006_210721.png

1 best response

Accepted Solutions
best response confirmed by pinefieldgene (Copper Contributor)
Solution

@pinefieldgene 

 

What is the exact error are you getting?

 

PowerShell has no issues with paths containing spaces, so with no error to refer to, I can't seen any issue.

 

I'd also point out that your final example isn't going to work. You can use double quotes or single quotes, but not both together as you have done in that example.

 

Double quotes are useful where you intend to use variables (and other special characters) inside of a string - as you have done with "$size MB", while single quotes are used where all PowerShell-specific special characters must be ignored.

 

What your second example is telling PowerShell to do is look for a single file in the current working directory that has an actual filename (not a path) of 'E:\MyDocuments\!Onedrive\OneDrive - Specified Nonprofit Organization Sample Japan\Sample.pdf', which isn't a valid filename.

 

Here's some examples of appropriate (first two commands) and inappropriate (final command) quote usage as well as a path containing spaces.

 

LainRobertson_0-1696078846485.png

 

Cheers,

Lain

View solution in original post