Unable to pass arguments with spaces to PowerShell script
-
zBernie
- Posts: 181
- Joined: 08 Dec 2016 17:20
Unable to pass arguments with spaces to PowerShell script
I have a simple Powershell script, lets call it test.ps:
param([String[]] $jpgfiles)
foreach ($jpgfile in $jpgfiles) {
"$jpgfile" | out-file -filepath "C:\Users\bernie\Photo Staging\tmp\dump.txt" -append
}
If I run the script from the command line like this in C:\Users\bernie\Photo Staging\tmp:
test.ps "DSC_1443 - Copy.jpg", "DSC_1441 - Copy.jpg"
These file names with spaces in them are output to the file dump.txt which then looks like this:
DSC_1443 - Copy.jpg
DSC_1441 - Copy.jpg
If I select the two files in XYplorer, then run this script:
$var_file = quote(<get SelectedItemsPathNames '", "'>);
runret("%SystemRoot%\system32\WindowsPowerShell\v1.0\powershell.exe -command C:\Users\bernie\PowerShell\im-datestamp-selection.ps1 " . "$var_file");
This is all I get in dump.txt:
C:\Users\bernie\Photo
What do I need to do in XYplorer to highlight the two file names with spaces, and pass them to test.ps?
param([String[]] $jpgfiles)
foreach ($jpgfile in $jpgfiles) {
"$jpgfile" | out-file -filepath "C:\Users\bernie\Photo Staging\tmp\dump.txt" -append
}
If I run the script from the command line like this in C:\Users\bernie\Photo Staging\tmp:
test.ps "DSC_1443 - Copy.jpg", "DSC_1441 - Copy.jpg"
These file names with spaces in them are output to the file dump.txt which then looks like this:
DSC_1443 - Copy.jpg
DSC_1441 - Copy.jpg
If I select the two files in XYplorer, then run this script:
$var_file = quote(<get SelectedItemsPathNames '", "'>);
runret("%SystemRoot%\system32\WindowsPowerShell\v1.0\powershell.exe -command C:\Users\bernie\PowerShell\im-datestamp-selection.ps1 " . "$var_file");
This is all I get in dump.txt:
C:\Users\bernie\Photo
What do I need to do in XYplorer to highlight the two file names with spaces, and pass them to test.ps?
-
RalphM
- Posts: 2143
- Joined: 27 Jan 2005 23:38
- Location: Cairns, Australia
Re: Unable to pass arguments with spaces to PowerShell script
From your other posts re scripting and Powershell you should be aware by now that there's a whole lot more to know about quoting parameters in XY scripts and I'm sure you can find various examples on the forum and in the help file - or wait until another forum member comes up with a solution for exactly your problem. As I'm not an expert in scripting myself, I can only point you in the general direction.
On another note, what you're trying to achieve here - from what I understand, appending filenames to a text file - could easily be done within a XY script without an external call of Powershell.
On another note, what you're trying to achieve here - from what I understand, appending filenames to a text file - could easily be done within a XY script without an external call of Powershell.
Ralph 
(OS: W11 26H2 Home x64 - XY: Current x64 beta - Office 2024 64-bit - Display: 1920x1080 @ 125%)
(OS: W11 26H2 Home x64 - XY: Current x64 beta - Office 2024 64-bit - Display: 1920x1080 @ 125%)
-
highend
- Posts: 15016
- Joined: 06 Feb 2011 00:33
- Location: Win Server 2022 @100%
Re: Unable to pass arguments with spaces to PowerShell script
Why the hell do you recreate an already existing topic (of your own)?
Use $args in the .ps1 script and be done with the whole quoting stuff (which is a problem of powershell, not XY!)
test.ps1
Use $args in the .ps1 script and be done with the whole quoting stuff (which is a problem of powershell, not XY!)
test.ps1
Code: Select all
foreach ($item in $args) {
"$item" | out-file -filepath "C:\Users\bernie\Photo Staging\tmp\dump.txt" -append
}
Code: Select all
$files = quote(<get SelectedItemsPathNames '" "'>);
runret("""powershell.exe"" -file ""<some path>\test.ps1"" $files");
One of my scripts helped you out? Please donate via Paypal
-
zBernie
- Posts: 181
- Joined: 08 Dec 2016 17:20
Re: Unable to pass arguments with spaces to PowerShell script
I have tried various permutations of quoting arguments using XYplorer ad nauseam, NONE of which worked. This is really mind boggling. A simple PowerShell script that works like this on the command line:RalphM wrote: ↑02 Dec 2018 08:08 From your other posts re scripting and Powershell you should be aware by now that there's a whole lot more to know about quoting parameters in XY scripts and I'm sure you can find various examples on the forum and in the help file - or wait until another forum member comes up with a solution for exactly your problem. As I'm not an expert in scripting myself, I can only point you in the general direction.
On another note, what you're trying to achieve here - from what I understand, appending filenames to a text file - could easily be done within a XY script without an external call of Powershell.
test.ps "DSC_1443 - Copy.jpg", "DSC_1441 - Copy.jpg"
XYplorer is incapable of passing arguments with spaces in them, quoted and comma separated.
-
highend
- Posts: 15016
- Joined: 06 Feb 2011 00:33
- Location: Win Server 2022 @100%
Re: Unable to pass arguments with spaces to PowerShell script
Will you ever understand that this is not a problem caused by XY?XYplorer is incapable of passing arguments with spaces in them, quoted and comma separated.
You doubt that, right?
Fine. Open a normal command prompt window ({WIN}cmd{ENTER})
Show me the line that calls your ps script (from the first post with no changes) with 3+ comma-separated files (that include spaces) as arguments, that works correctly...
One of my scripts helped you out? Please donate via Paypal
-
zBernie
- Posts: 181
- Joined: 08 Dec 2016 17:20
Re: Unable to pass arguments with spaces to PowerShell script
highend wrote: ↑02 Dec 2018 21:24Will you ever understand that this is not a problem caused by XY?XYplorer is incapable of passing arguments with spaces in them, quoted and comma separated.
You doubt that, right?
Fine. Open a normal command prompt window ({WIN}cmd{ENTER})
Show me the line that calls your ps script (from the first post with no changes) with 3+ comma-separated files (that include spaces) as arguments, that works correctly...
Here it is. The arguments passed to the script need to be quoted, and separated by a comma.
PS C:\Users\bernie\Photo Staging\tmp> test.ps1 "C:\Users\bernie\Photo Staging\tmp\DSC_1441 - Copy.jpg", "C:\Users\bernie\P
hoto Staging\tmp\DSC_1443 - Copy.jpg"
PS C:\Users\bernie\Photo Staging\tmp> type dump.txt
C:\Users\bernie\Photo Staging\tmp\DSC_1441 - Copy.jpg
C:\Users\bernie\Photo Staging\tmp\DSC_1443 - Copy.jpg
-
highend
- Posts: 15016
- Joined: 06 Feb 2011 00:33
- Location: Win Server 2022 @100%
Re: Unable to pass arguments with spaces to PowerShell script
You even quoted what I wrote:
And instead you opened a powershell console? oOOpen a normal command prompt window ({WIN}cmd{ENTER})
One of my scripts helped you out? Please donate via Paypal
-
zBernie
- Posts: 181
- Joined: 08 Dec 2016 17:20
Re: Unable to pass arguments with spaces to PowerShell script
From a cmd prompt:
C:\Users\bernie\Photo Staging\tmp>powershell.exe -File C:\Users\bernie\PowerShell\test.ps1 "C:\Users\bernie\Photo Staging\tmp\DSC_1441 - Copy.jpg", "C:\Users\bernie\Photo Staging\tmp\DSC_1443 - Copy.jpg"
C:\Users\bernie\Photo Staging\tmp>type dump.txt
C:\Users\bernie\Photo Staging\tmp\DSC_1441 - Copy.jpg,
-
highend
- Posts: 15016
- Joined: 06 Feb 2011 00:33
- Location: Win Server 2022 @100%
Re: Unable to pass arguments with spaces to PowerShell script
And, is
the same as
?
Surely not...
Code: Select all
C:\Users\bernie\Photo Staging\tmp\DSC_1441 - Copy.jpg,Code: Select all
C:\Users\bernie\Photo Staging\tmp\DSC_1441 - Copy.jpg
C:\Users\bernie\Photo Staging\tmp\DSC_1443 - Copy.jpg
Surely not...
One of my scripts helped you out? Please donate via Paypal
-
zBernie
- Posts: 181
- Joined: 08 Dec 2016 17:20
Re: Unable to pass arguments with spaces to PowerShell script
highend wrote: ↑04 Dec 2018 07:54 And, isthe same asCode: Select all
C:\Users\bernie\Photo Staging\tmp\DSC_1441 - Copy.jpg,?Code: Select all
C:\Users\bernie\Photo Staging\tmp\DSC_1441 - Copy.jpg C:\Users\bernie\Photo Staging\tmp\DSC_1443 - Copy.jpg
Surely not...
Okay, so you've proved that DOS and XYplorer are both incapable of handling arguments with spaces in the name.
-
highend
- Posts: 15016
- Joined: 06 Feb 2011 00:33
- Location: Win Server 2022 @100%
Re: Unable to pass arguments with spaces to PowerShell script
This is getting ridiculous...Okay, so you've proved that DOS and XYplorer are both incapable of handling arguments with spaces in the name
I've already posted a clean and working solution.
Topic locked
One of my scripts helped you out? Please donate via Paypal
XYplorer Beta Club