Multi Commander > Script

Create hard link

(1/1)

Lukas:
Hello,
I try to create a hard link to a file using script, but it doesn't do anythink. Can someone look at where I am wrong, please?
Thank you
Lukas


--- Code: ---@var $selectedPaths = GetSourceSelectedPaths();
@var $targetPath = GetTargetPath();
//LogDumpArray($selectedPaths);
//LogDump($targetPath);

@var $sourcePathParts = StrSplit(PathGetPathPart($selectedPaths[0]), "\\");
@var $targetPathParts = StrSplit($targetPath, "\\");

//LogDumpArray( $sourcePathParts );
//LogDumpArray( $targetPathParts );

@var $counts = arrayCount($sourcePathParts);
@var $countt = arrayCount($targetPathParts);
@var $count = $countt;
if ($counts < $countt)
{
  $count = $counts;
}

@var $i;
for( $i = 0; $i < $count; $i++ )
{
  @var $cmp = StrIsEqualNoCase($sourcePathParts[$i], $targetPathParts[$i]);
  if ($cmp == 0)
  {
    break;
  }
}

@var $diffInx = $i;

@var $sourcePathRel = ".";
if ($i == 0)
{
  $sourcePathRel = $sourcePath;
}
else
{
  for( $i = $diffInx; $i < $counts; $i++ )
  {
    $sourcePathRel += "\\" + $sourcePathParts[$i];
  }

  $sourcePathRel = StrTrimLeft($sourcePathRel, ".\\");
}

@var $targetPathRel = ".";
if ($i == 0)
{
  $targetPathRel = $targePath;
}
else
{
  for( $i = $diffInx; $i < $countt; $i++ )
  {
    $targetPathRel += "\\" + $targetPathParts[$i];
  }

  $targetPathRel = StrTrimLeft($targetPathRel, ".\\");
}

//LogDump($sourcePathRel);
//LogDump($targetPathRel);

$count = arrayCount($selectedPaths);
for( $i = 0; $i < $count; $i++ )
{
  @var $file = PathGetNamePart($selectedPaths[$i]);
  @var $sourceFile = $sourcePathRel ^ $file;
  @var $targetFile = $targetPathRel ^ $file;
  //LogDump($sourceFile);
  //LogDump($targetFile);
  MC.Utils.CreateLink ASADMIN LNKSRC="$sourceFile" LNKTRG="$targetFile" LNKTYPE=1
}

--- End code ---

Mathias (Author):
I have not testet your script..

But in the MultiScript code you are calling CustomCommand functions.
CustomCommand are another type of simple scripts that MultiScript can call. When CustomCommand does not know about MultiScript type of parameters and stuff.
So when sending MultiScript paramters to a CustomCommand, this parameters must be encapsulated with { } so they are evaluated before the CustomCommand is called.

Try change

--- Code: ---MC.Utils.CreateLink ASADMIN LNKSRC="$sourceFile" LNKTRG="$targetFile" LNKTYPE=1
--- End code ---

To

--- Code: ---MC.Utils.CreateLink ASADMIN LNKSRC="{$sourceFile}" LNKTRG="{$targetFile}" LNKTYPE=1
--- End code ---

Lukas:
Mathias, thank you for quick answer.

I simplified script, however doesn't work. I tested hard link and symbolic link only.


--- Code: ---@var $selectedPaths = GetSourceSelectedPaths();
@var $targetPath = GetTargetPath();
//LogDumpArray($selectedPaths);
//LogDump($targetPath);

@var $i;
@var $count = arrayCount($selectedPaths);
for( $i = 0; $i < $count; $i++ )
{
  @var $file = PathGetNamePart($selectedPaths[$i]);
  @var $sourceFile = $selectedPaths[$i];
  @var $targetFile = $targetPath ^ $file;
  LogDump($sourceFile);
  LogDump($targetFile);
  MC.Utils.CreateLink ASADMIN LNKSRC="{$sourceFile}" LNKTRG="{$targetFile}" LNKTYPE=1
}

--- End code ---

But never mind, workaround is to write filenames into txt and after run batch.

Happy new year!

Mathias (Author):
Ah You are mixing source and target

LNKSRC is the Link source (the link to create)
LNKTRG is the target of the Link (the existing file.. where the link should point to)

Also for Hardlinks the link and the target of the link must be on the same filesystem partition.

Lukas:
Thank you, It works.
 :)

Navigation

[0] Message Index

Go to full version