Page 1 of 1
Unable to pass arguments with spaces to PowerShell script
Posted: 02 Dec 2018 05:03
by zBernie
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?
Re: Unable to pass arguments with spaces to PowerShell script
Posted: 02 Dec 2018 08:08
by RalphM
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.
Re: Unable to pass arguments with spaces to PowerShell script
Posted: 02 Dec 2018 10:53
by highend
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
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");
Re: Unable to pass arguments with spaces to PowerShell script
Posted: 02 Dec 2018 19:29
by zBernie
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.
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:
test.ps "DSC_1443 - Copy.jpg", "DSC_1441 - Copy.jpg"
XYplorer is incapable of passing arguments with spaces in them, quoted and comma separated.
Re: Unable to pass arguments with spaces to PowerShell script
Posted: 02 Dec 2018 21:24
by highend
XYplorer is incapable of passing arguments with spaces in them, quoted and comma separated.
Will you ever understand that this is not a problem caused by XY?
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...
Re: Unable to pass arguments with spaces to PowerShell script
Posted: 03 Dec 2018 04:22
by zBernie
highend wrote: ↑02 Dec 2018 21:24
XYplorer is incapable of passing arguments with spaces in them, quoted and comma separated.
Will you ever understand that this is not a problem caused by XY?
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
Re: Unable to pass arguments with spaces to PowerShell script
Posted: 03 Dec 2018 06:56
by highend
You even quoted what I wrote:
Open a normal command prompt window ({WIN}cmd{ENTER})
And instead you opened a powershell console? oO
Re: Unable to pass arguments with spaces to PowerShell script
Posted: 04 Dec 2018 03:37
by zBernie
highend wrote: ↑03 Dec 2018 06:56
You even quoted what I wrote:
Open a normal command prompt window ({WIN}cmd{ENTER})
And instead you opened a powershell console? oO
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,
Re: Unable to pass arguments with spaces to PowerShell script
Posted: 04 Dec 2018 07:54
by highend
And, is
Code: Select all
C:\Users\bernie\Photo Staging\tmp\DSC_1441 - Copy.jpg,
the same as
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...
Re: Unable to pass arguments with spaces to PowerShell script
Posted: 04 Dec 2018 21:02
by zBernie
highend wrote: ↑04 Dec 2018 07:54
And, is
Code: Select all
C:\Users\bernie\Photo Staging\tmp\DSC_1441 - Copy.jpg,
the same as
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.
Re: Unable to pass arguments with spaces to PowerShell script
Posted: 04 Dec 2018 21:14
by highend
Okay, so you've proved that DOS and XYplorer are both incapable of handling arguments with spaces in the name
This is getting ridiculous...
I've already posted a clean and working solution.
Topic locked