プログラミング学習サイト

プログラミングの学習を開始される方を対象としたプログラミング入門サイトです。

Powershellでハードリンクを作成する

Powershell概要

minegishirei.hatenablog.com

ハードリンクとは

ハードリンクとは、同じファイルを二つ作る仕組みのこと。

通常のコピーとは違い、片方の編集がもう片方に影響を及ぼすということ。

ファイルのハードリンクは実態を二つ作ることでもある。

#つまり同じファイルを二箇所に設置したいときに役に立つ

通常のwindowsには標準搭載されていない

このハードリンクというシステムをインストールなしで搭載しているのはwindowsvistaのバージョンのみ

他はpowershellを使わなければ実装できません

ハードリンク作成用コード



## ファイル名: New-FileSystemHardLink.ps1

function(){
    param(
        ## 新しく作りたいファイル名
        [string] $filename,
        ## すでに存在するコピーしたいファイル名
        [string] $existingFilename
        )
    ## ファイルパスが絶対パスかどうか調べる
    $filename = $executionContext.SessionState.Path.`
        GetUnresolvedProviderPathFromPSPath($filename)
    $existingFilename = Resolve-Path $existingFilename

    ## 関数に渡された引数の型を調べる
    $parameterTypes = [string], [string], [IntPtr]
    $parameters = [string] $filename, [string] $existingFilename, [IntPtr]::Zero

    ## Kernel32 DLLのハードリンク作成メソッドを呼び出す
    $currentDirectory = Split-Path $myInvocation.MyCommand.Path
    $invokeWindowsApiCommand = Join-Path $currentDirectory Invoke-WindowsApi.ps1
    $result = & $invokeWindowsApiCommand "kernel32" `
        ([bool]) "CreateHardLink" $parameterTypes $parameters

    ## エラー管理
    if(-not $result)
    {
        $message = "Could not create hard link of $filename to " +
            "existing file $existingFilename"
        Write-Error $message
    }

}

元ネタのコード(powershellのハードリンク作成)


############################################################################## 
## 
##  New-FileSystemHardLink.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 >New-FilesystemHardLink.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 = Resolve-Path $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 = Split-Path $myInvocation.MyCommand.Path
$invokeWindowsApiCommand = Join-Path $currentDirectory Invoke-WindowsApi.ps1
$result = & $invokeWindowsApiCommand "kernel32" `
    ([bool]) "CreateHardLink" $parameterTypes $parameters

##  Provide an error message if the call fails
if(-not $result)
{
    $message = "Could not create hard link of $filename to " +
        "existing file $existingFilename"
    Write-Error $message
}

title:windowsでハードリンクを作る(powershell) description:ハードリンクとは、同じファイルを二つ作る仕組みのこと。通常のコピーとは違い、片方の編集がもう片方に影響を及ぼすということ。ファイルのハードリンクは実態を二つ作ることでもある。 img:https://upload.wikimedia.org/wikipedia/commons/thumb/3/32/Hard_Link_Illustration.svg/1200px-Hard_Link_Illustration.svg.png

category_script:page_name.startswith("16")

page:https://minegishirei.hatenablog.com/entry/2024/05/22/090605