How to escape a space [Solved]

Discuss and share scripts and script files...
Post Reply
hitfq
Posts: 18
Joined: 14 Feb 2012 22:20

How to escape a space [Solved]

Post by hitfq »

Hi,

I want to write a script that make a hardlink of files I selected to a folder for synchronization purposes.
I need to specify the separator to be a space, I tried '\ ', '\s', all doesn't work, please help me.
Here is the script:

foreach($var, <selitems>, separator= ) {
goto "! mklink /h".$var."C:\sync"
}

The reference for mklink
mklink – a command line tool included with Vista and Server 2008 and above. The most current link creation tool included with Windows. It creates Hard Links, Symbolic Links and Junction Points.
usage: mklink [[/d] | [/h] | [/j]] <NameofLink> <Target>
/d – Creates a Symbolic Link for a directory. If no flag used, creates a Symbolic Link for a file (default)
/h – Creates a Hard Link
/j – Creates a Junction Point
<NameofLink> – The name for the Symbolic Link being created
<Target> – The relative or absolute path of the target
/? – Help

Thank you very much!
Last edited by hitfq on 17 May 2014 18:17, edited 1 time in total.

binocular222
Posts: 1419
Joined: 04 Nov 2008 05:35
Location: Win11, Win10, 100% Scaling

Re: How to escape a space

Post by binocular222 »

Quote it

Code: Select all

foreach($var, <selitems>, separator=" " ) {
But that script will break horribly if there're spaces inside file path of <selitems>. Use this instead:

Code: Select all

 $selitems = get("SelectedItemsPathNames", "|");
 foreach($item, $selitems) {
   goto "! mklink /h".$item."C:\sync";
 }
P.S: Should be posted at "Tips & Tricks, Questions & Answers"
P.S: IMO, you should use Run instead of goto. Also notice how I use 2 double-quotes around file paths (essentially, anything expect spaces will require 2 double-quotes):

Code: Select all

[code] $selitems = get("SelectedItemsPathNames", "|");
 foreach($item, $selitems) {
   run "cmd /c mklink /h ""$item"" ""C:\sync"" ";
 }
[/code]
Last edited by binocular222 on 16 May 2014 05:45, edited 2 times in total.
I'm a casual coder using AHK language. All of my xys scripts:
http://www.xyplorer.com/xyfc/viewtopic. ... 243#p82488

bdeshi
Posts: 4256
Joined: 12 Mar 2014 17:27
Location: Asteroid B-612
Contact:

Re: How to escape a space

Post by bdeshi »

binocular222, you beat me to it! :D
@hitfq, you shouldn't use space as separator as it will also split items that have spaces in name. Use some char that can't be used in filenames, for example |.
This code converts <selitems> to | separated list string, and then uses | in foreach.

Code: Select all

$selitems = trim(replace(<selitems>, '" "', ' '), '"');
foreach($var, $selitems, '|') {
goto "! mklink /h".$var."C:\sync";
}
Icon Names | Onyx | Undocumented Commands | xypcre
[ this user is asleep ]

hitfq
Posts: 18
Joined: 14 Feb 2012 22:20

Re: How to escape a space

Post by hitfq »

:biggrin:
It works!
Needs a little modification: use !! instead of ! so no popup and the parameter order meeds to be changed.

Code: Select all

$selitems = get("SelectedItemsNames", "|"); foreach($item, $selitems) {   goto ("!! mklink /h "."C:\sync\".$item." ".$item);}
The run command can't be used here, because mklink is a built in command, it will report "error: can't find path to mklink.exe" if we use run.
Thank you very much!

binocular222 wrote:Quote it
P.S: IMO, you should use Run instead of goto. Also notice how I use 2 double-quotes around file paths (essentially, anything expect spaces will require 2 double-quotes):

Code: Select all

[code] $selitems = get("SelectedItemsPathNames", "|");
 foreach($item, $selitems) {
   run "cmd /c mklink /h ""$item"" ""C:\sync"" ";
 }
[/code]

Post Reply