24/7/365 Support

Program: Create a Filesystem Hard Link

Discussion

It is sometimes useful to refer to the same file by two different names or locations. You can’t solve this problem by copying the item, because modifications to one file do not automatically affect the other.

The solution to this is called a hard link, an item of a new name that points to the data of another file. The Windows operating system supports hard links, but only Windows Vista includes a utility that lets you create them.

Example 176 lets you create hard links without needing to install additional tools. It uses (and requires) the InvokeWindowsApi.ps1 script.

Example 176. NewFilesystemHardLink.ps1

############################################################################## ## ## NewFileSystemHardLink.ps1 ## ## Create a new hard link, which allows you to create a new name by which you ## can access an existing file. Windows only deletes the actual file once ## you delete all hard links that point to it. ## ## ie: ## ## PS >"Hello" > test.txt ## PS >dir test* | select name ## ## Name ## ## test.txt ## ## PS >NewFilesystemHardLink.ps1 test2.txt test.txt ## PS >type test2.txt ## Hello ## PS >dir test* | select name ## ## Name ## ## test.txt ## test2.txt ## ##############################################################################

param( ## The new filename you want to create [string] $filename,

## The existing file that you want the new name to point to [string] $existingFilename )

## Ensure that the provided names are absolute paths

$filename = $executionContext.SessionState.Path.` GetUnresolvedProviderPathFromPSPath($filename)

$existingFilename = ResolvePath $existingFilename

## Prepare the parameter types and parameters for the CreateHardLink function $parameterTypes = [string], [string], [IntPtr] $parameters = [string] $filename, [string] $existingFilename, [IntPtr]::Zero

## Call the CreateHardLink method in the Kernel32 DLL

$currentDirectory = SplitPath $myInvocation.MyCommand.Path

$invokeWindowsApiCommand = JoinPath $currentDirectory InvokeWindowsApi.ps1

$result = & $invokeWindowsApiCommand "kernel32" ` ([bool]) "CreateHardLink" $parameterTypes $parameters

Example 176. NewFilesystemHardLink.ps1 (continued)

## Provide an error message if the call fails if(not $result) {

$message = "Could not create hard link of $filename to " + "existing file $existingFilename" WriteError $message }

Help Category:

Get Windows Dedicated Server

Only reading will not help you, you have to practice it! So get it now.

Processor RAM Storage Server Detail
Intel Atom C2350 1.7 GHz 2c/2t 4 GB DDR3 1× 1 TB (HDD SATA) Configure Server
Intel Atom C2350 1.7 GHz 2c/2t 4 GB DDR3 1× 128 GB (SSD SATA) Configure Server
Intel Atom C2750 2.4 GHz 8c/8t 8 GB DDR3 1× 1 TB (HDD SATA) Configure Server
Intel Xeon E3-1230 v2 3.3 GHz 4c/8t 16 GB DDR3 1× 256 GB (SSD SATA) Configure Server
Intel Atom C2350 1.7 GHz 2c/2t 4 GB DDR3 1× 250 GB (SSD SATA) Configure Server

What Our Clients Say