zoukankan      html  css  js  c++  java
  • 示范NTFS 卷上的硬链接

    ' Hardlinks.vbs
    ' 示范 NTFS 卷上的硬链接
    ' --------------------------------------------------------

    Option Explicit

    ' 一些常量
    Const L_NoHardLinkCreated = "Unable to create hard link"
    Const L_EnterTarget = "Enter the file name to hard-link to"
    Const L_HardLinks = "Creating hard link"
    Const L_EnterHardLink = "Name of the hard link you want to create"
    Const L_CannotCreate = "Make sure that both files are on the same volume and the volume is NTFS"
    Const L_NotExist = "Sorry, the file doesn't exist"
    Const L_SameName = "Target file and hard link cannot have the same name"

    ' 确定要(硬)链接的现有文件
    dim sTargetFile
    if WScript.Arguments.Count >0 then
       sTargetFile = WScript.Arguments(0)
    else
       sTargetFile = InputBox(L_EnterTarget, L_HardLinks, "")
       if sTargetFile = "" then WScript.Quit
    end if

    ' 该文件存在吗?
    dim fso
    set fso = CreateObject("Scripting.FileSystemObject")  
    if Not fso.FileExists(sTargetFile) then
       MsgBox L_NotExist
       WScript.Quit
    end if

    ' 主循环
    while true
       QueryForHardLink sTargetFile
    wend


    ' 关闭
    WScript.Quit






    ' /////////////////////////////////////////////////////////////
    ' // Helper 函数



    ' 创建硬链接
    '------------------------------------------------------------
    function QueryForHardLink(sTargetFile)
       ' 如果在命令行上指定了硬链接名,则提取它
       dim sHardLinkName
       if WScript.Arguments.Count >1 then
          sHardLinkName = WScript.Arguments(1)
       else
          dim buf
          buf = L_EnterHardLink & " for" & vbCrLf & sTargetFile
          sHardLinkName = InputBox(buf, L_HardLinks, sTargetFile)
          if sHardLinkName = "" then WScript.Quit  
          if sHardLinkName = sTargetFile then
             MsgBox L_SameName
             exit function
          end if
       end if

       ' 验证两个文件均在同一个卷上,且
       ' 该卷是 NTFS
       if Not CanCreateHardLinks(sTargetFile, sHardLinkName) then
          MsgBox L_CannotCreate
          exit function
       end if
      
       ' 创建硬链接
       dim oHL
       set oHL = CreateObject("HardLink.Object.1")
       oHL.CreateNewHardLink sHardLinkName, sTargetFile
    end function


    ' 验证两个文件均在同一个 NTFS 磁盘上
    '------------------------------------------------------------
    function CanCreateHardLinks(sTargetFile, sHardLinkName)
       CanCreateHardLinks = false
      
       dim fso
       set fso = CreateObject("Scripting.FileSystemObject")
      
       ' 同一个驱动器?
       dim d1, d2
       d1 = fso.GetDriveName(sTargetFile)
       d2 = fso.GetDriveName(sHardLinkName)
       if d1 <> d2 then exit function

       ' NTFS 驱动器?
       CanCreateHardLinks = IsNTFS(sTargetFile)
    end function


    ' IsNTFS() — 验证文件的卷是否为 NTFS
    ' --------------------------------------------------------
    function IsNTFS(sFileName)
       dim fso, drv
      
       IsNTFS = False
       set fso = CreateObject("Scripting.FileSystemObject")  
       set drv = fso.GetDrive(fso.GetDriveName(sFileName))
       set fso = Nothing
      
       if drv.FileSystem = "NTFS" then IsNTFS = True
    end function

  • 相关阅读:
    C++ Primer注意事项11_运算符重载_算术/关系运算符_下标运算符
    android最新的工具DateHelper
    ssh否password登陆server
    atitit.设计模式(2) -----查询方式/ command 总结
    采用Eclipse中间Maven构建Web项目错误(一)
    dm8148 jpeg编解码器测试
    C++ 结构体和类的区别
    C++ const
    C++中的inline函数
    C++ 模板类demo
  • 原文地址:https://www.cnblogs.com/MaxWoods/p/395284.html
Copyright © 2011-2022 走看看