Page 1 of 1
Files with spaces passed to PowerShell script not found
Posted: 23 Oct 2018 04:30
by zBernie
If I run this powershell script from the command line it works just fine. The script wants a comma separated list of arguments, enclosed in quotes due to the spaces in the file names:
im-datestamp-selection.ps1 "C:\Users\bernie\Photo Staging\tmp\DSC_1032 - Copy.JPG", "C:\Users\bernie\Photo Staging\tmp\DSC_0859 - Copy.JPG"
But when I select the same two files then run the script from XYPlorer, it fails, because it cannot find the file, even though I've specified quoted and comma separated like:
Code: Select all
$var_file = quote(<get SelectedItemsPathNames '", "'>);
runret("%SystemRoot%\system32\WindowsPowerShell\v1.0\powershell.exe C:\Users\bernie\PowerShell\im-datestamp-selection.ps1 $var_file");
Stepping through the script, the value of $var_file appears correct:
"C:\Users\bernie\Photo Staging\tmp\DSC_1032 - Copy.JPG", "C:\Users\bernie\Photo Staging\tmp\DSC_0859 - Copy.JPG"
I don't understand why I am getting this error when running this from XYPlorer, when the parameters are quoted:
Call: get-item : Cannot find path 'C:\Users\Bernie\Photo'
Re: Files with spaces passed to PowerShell script not found
Posted: 23 Oct 2018 05:01
by jupe
There are a few ways to fix this, this is probably the easiest, and should work:
Code: Select all
$var_file = quote(<get SelectedItemsPathNames '", "'>);
runret("%SystemRoot%\system32\WindowsPowerShell\v1.0\powershell.exe C:\Users\bernie\PowerShell\im-datestamp-selection.ps1 " . $var_file);
You are also using runret, but not doing anything with the output, so you could be using run.
You can paste the following into the XY address bar for more information if you are interested:
Code: Select all
::help "idh_scripting.htm#idh_scripting_quotes";
Re: Files with spaces passed to PowerShell script not found
Posted: 23 Oct 2018 05:16
by zBernie
I just tried your suggesting and get the same exact error. Frustrating.
Re: Files with spaces passed to PowerShell script not found
Posted: 23 Oct 2018 05:52
by jupe
What about this then:
Code: Select all
$var_file = quote(<get SelectedItemsPathNames '", "'>);
run "%SystemRoot%\system32\WindowsPowerShell\v1.0\powershell.exe -command C:\Users\bernie\PowerShell\im-datestamp-selection.ps1 " . $var_file;
OR
Code: Select all
$var_file = quote(<get SelectedItemsPathNames '", "'>);
run "%SystemRoot%\system32\WindowsPowerShell\v1.0\powershell.exe -file C:\Users\bernie\PowerShell\im-datestamp-selection.ps1 " . $var_file;
Re: Files with spaces passed to PowerShell script not found
Posted: 24 Oct 2018 03:27
by zBernie
I tried both of your syntax suggestions, to no avail. I get an error output to the terminal, which disappears too fast for me to read.
Thanks for your help with this issue!
Re: Files with spaces passed to PowerShell script not found
Posted: 21 Nov 2018 22:46
by highend
Link to a copy of that ps script?
Re: Files with spaces passed to PowerShell script not found
Posted: 22 Nov 2018 06:06
by zBernie
highend wrote: ↑21 Nov 2018 22:46
Link to a copy of that ps script?
I could not get this working with PowerShell (and we have tried before). So, I've switched to Python 3.7 under Windows 10. I just want to be able to highlight some files, and pass them to the script. In order for this script to handle paths with spaces, the paths need to be quoted like this:
"C:\Users\bernie\CentOS Share\DSC_1441.jpg", "C:\Users\bernie\CentOS Share\DSC_1443.jpg"
I've tried syntax similar to that shown below, to no avail. How can I highlight the two files above, and pass them quoted to my Python script?
//$var_file = quote(<get SelectedItemsPathNames '", "'>);
$var_file = quote("<selitems ,>");
run """C:\Users\bernie\CentOS Share\myscript.py "" ""$var_file""";
-Thanks
Re: Files with spaces passed to PowerShell script not found
Posted: 22 Nov 2018 07:34
by highend
Do what I asked for?
Re: Files with spaces passed to PowerShell script not found
Posted: 23 Nov 2018 05:53
by zBernie
highend wrote: ↑22 Nov 2018 07:34
Do what I asked for?
Here's an example Python script. I need to be able to pass the script file names in folders which contain spaces, e.g., C:\Users\bernie\CentOS Share\pics\DSC_123.jpg.
file = open("testfile.txt","w+")
for jpgfile in sys.argv[1:]:
jpgfile = '"' + jpgfile + '"'
print ("Processing %s " % jpgfile)
file.write(jpgfile)
file.close()
Re: Files with spaces passed to PowerShell script not found
Posted: 23 Nov 2018 07:19
by highend
Thanks for posting a by default non-working script file and ignoring my request for the second time...
Code: Select all
$python = "R:\Python27\python.exe";
$script = "R:\python script file demonstration.py";
$items = quote(<get SelectedItemsPathNames '" "'>);
$command = <<<>>>
"$python" "$script" $items
>>>;
$result = runret($command, gpc($script, "path"));
text $result;
runret.png
Content of output file:
Code: Select all
"R:\folder with spaces\001.JPG""R:\folder with spaces\002.JPG""R:\folder with spaces\003.JPG"
Re: Files with spaces passed to PowerShell script not found
Posted: 23 Nov 2018 08:07
by zBernie
highend wrote: ↑23 Nov 2018 07:19
Thanks for posting a by default non-working script file and ignoring my request for the second time...
Code: Select all
$python = "R:\Python27\python.exe";
$script = "R:\python script file demonstration.py";
$items = quote(<get SelectedItemsPathNames '" "'>);
$command = <<<>>>
"$python" "$script" $items
>>>;
$result = runret($command, gpc($script, "path"));
text $result;
runret.png
Content of output file:
Code: Select all
"R:\folder with spaces\001.JPG""R:\folder with spaces\002.JPG""R:\folder with spaces\003.JPG"
No, what I want to do is be able to select files in XYplorer, and then click a user button which passes those file names to the script. I've tried various permutations of "$var_file" below, none of which work. XYplorer ALWAYS truncates the file name at the space in the path name, as shown below. The script that is passed the file names is not a factor here. I can only conclude that XYplorer is unable to perform this simple task.
"C:\Users\bernie\CentOS""Share\DSC_1443.jpg
"C:\Users\bernie\CentOS""Share\DSC_1441.jpg"
$var_file = quote(<get SelectedItemsPathNames>, "<crlf>");
run """C:\Users\bernie\CentOS Share\im-datestamp.py "" ""$var_file""";
//$var_file = quote(<get SelectedItemsPathNames '", "'>);
//$var_file = quote("<selitems ,>");
Re: Files with spaces passed to PowerShell script not found
Posted: 23 Nov 2018 09:49
by highend
No, what I want to do is be able to select files in XYplorer, and then click a user button which passes those file names to the script
Guess what, that is
exactly what my script does. Regardless if it's on a user button or anywhere else. It takes the selected files and calls the .py script with it and the output shows that the script is exactly doing what it is supposed to do
Re: Files with spaces passed to PowerShell script not found
Posted: 24 Nov 2018 20:17
by zBernie
highend wrote: ↑23 Nov 2018 09:49
No, what I want to do is be able to select files in XYplorer, and then click a user button which passes those file names to the script
Guess what, that is
exactly what my script does. Regardless if it's on a user button or anywhere else. It takes the selected files and calls the .py script with it and the output shows that the script is exactly doing what it is supposed to do
You're script does indeed work to pass space separated path names to an external script. Why on Earth you need to use a construct like this for such a simple task is beyond me:
$command = <<<>>>
At any rate, thanks for your help and have a drink on me!
Re: Files with spaces passed to PowerShell script not found
Posted: 24 Nov 2018 20:27
by highend
Nobody "needs" this construct. It's just easy to read and easy to understand.
This would have worked as well (and don't tell me it doesn't, I've tested it^^)
Code: Select all
$result = runret("""$python"" ""$script"" $items", gpc($script, "path"));