【问题标题】:Moving files create shortcut in original location pointing to new location移动文件在原始位置创建指向新位置的快捷方式
【发布时间】:2016-10-18 10:53:52
【问题描述】:

我有一个 PowerShell 脚本,可以将所有文件从一个位置移动到另一个位置,这些文件的日期修改时间超过 3 年。我有它,所以文件在移动到新位置时也保持原来的文件结构。

我正在尝试这样做,一旦文件被移动到新位置,它会在原始目录中创建一个指向文件新位置的快捷方式。

以下是我目前的脚本,它完成了上述所有操作减去快捷方式。

$sourceDir = "C:\Users\bgough\Documents\powershell\docs"
$archiveTarget = "C:\Users\bgough\Documents\archive"
$dateToday = Get-Date
$date = $dateToday.AddYears(-3)

$items = Get-ChildItem $sourceDir -Recurse |
         Where-Object {!$_.PSIsContainer -and $_.LastWriteTime -le $date}

foreach ($item in $items)
{
  $withoutRoot = $item.FullName.Substring([System.IO.Path]::GetPathRoot($item.FullName).Length);
  $destination = Join-Path -Path $archiveTarget -ChildPath $withoutRoot

  $dir = Split-Path $destination
  if (!(Test-Path $dir))
  {
    mkdir $dir
  }

  Move-Item -Path $item.FullName -Destination $destination

  $WshShell = New-Object -ComObject WScript.Shell
  $Shortcut = $WshShell.CreateShortcut("$sourceDir")
  $Shortcut.TargetPath = $destination
  $Shortcut.Save()
}

在我的脚本中,我尝试了创建此快捷方式,但它没有帮助。我也通读了以下内容,但不太了解..

How to create a shortcut using Powershell

Powershell Hard and Soft Links

编辑:

我已经成功获得了在原始文件夹中创建的快捷方式。但是,我似乎无法弄清楚如何传递一个变量以用作快捷方式名称。目前,字符串是硬编码的,这就是快捷方式的名称。请参阅下面的代码:我想将名称设置为项目全名(与被移动的文档同名)。

$sourceDir = "C:\Users\bgough\Documents\powershell\docs"
$archiveTarget = "C:\Users\bgough\Documents\archive"
$dateToday = Get-Date
$date = $dateToday.AddYears(-3)

$items = Get-ChildItem $sourceDir -recurse | Where-Object {!$_.PsIsContainer -and $_.LastWriteTime -le $date}

foreach ($item in $items)
{
  $withoutRoot = $item.FullName.Substring([System.IO.Path]::GetPathRoot($item.FullName).Length);
  $destination = Join-Path -Path $archiveTarget -ChildPath $withoutRoot

  $dir = Split-Path $destination
  if (!(Test-Path $dir))
  {
    mkdir $dir
  }

  Move-Item -Path $item.FullName -Destination $destination

  $wshshell = New-Object -ComObject WScript.Shell
  $desktop = [System.Environment]::GetFolderPath('Desktop')
  $lnk = $wshshell.CreateShortcut($sourceDir + "\ShortcutName.lnk")
  $lnk.TargetPath = "$destination"
  $lnk.Save()
}

【问题讨论】:

  • 您要创建快捷方式还是(符号)链接?它们是完全不同的东西。另外,您的尝试没有按预期工作怎么办?你收到错误了吗?
  • 我正在寻找捷径。我也没有收到错误,但是原始位置没有快捷方式。
  • @AnsgarWiechers 非常感谢您的语法检查。 (叹气..)

标签: powershell


【解决方案1】:

.lnk 文件在您使用资源管理器时很好,但它们在 Powershell 或命令提示符下播放效果不佳。

您需要为文件创建一个符号链接。您不能在 Powershell 中执行此操作,但有一个名为 mklink 的命令行实用程序可以执行此操作。我已经将它包装在一个函数中,以便您可以调用它:

function CreateLink
{
    param
    (
        [string] $LinkName,
        [string] $TargetFile
    )

    &"cmd.exe" /c mklink "$LinkName" "$TargetFile" | Out-Null
}

在您的示例中,您可以这样称呼它:

CreateLink -LinkName $item.FullName -TargetFile $destination

当您在 Powershell 中查看目录时,该文件将显示为 0 字节大小。不用担心。

【讨论】:

  • 嗨,肖恩,非常感谢您的回答,今天您救了您一命。上面的问题是这个脚本将被安排每隔几周左右自行运行一次。
  • @AndroidMagic 为什么上面的调度有问题?
  • 我的错我刚刚明白你的意思:)
  • 没问题。祝脚本好运!
  • 非常感谢! :D
【解决方案2】:

感谢您的脚本 Android Magic。

我已经修改为:

  1. 将一组文件从源复制到目标
  2. 即使文件夹为空,它也会在目标上创建相同的文件夹结构
  3. 然后它会创建一个指向存档文件的符号链接。在 Powershell v5.1 中添加了 SymbolicLink 支持。您必须以管理员身份运行脚本才能创建符号链接。

如果出现任何问题,我想在电子邮件中添加一个功能和状态摘要,但那是另一天的事了。

$sourceDir = "\\Fileserver1\IT\Vendor"
$archiveTarget = "\\FS-ARCHIVE\Archive\Fileserver1\IT\Vendor"
$rootArchivePath = "\\FS-ARCHIVE\Archive"

$dateToday = Get-Date
$date = $dateToday.AddYears(-3)

# Copy folder structure to Archive
Get-ChildItem -Path $sourceDir -Recurse | 
?{ $_.PSIsContainer } | 
Copy-Item -Destination {Join-Path $archiveTarget $_.Parent.FullName.Substring($sourceDir.length)} -Force

$items = Get-ChildItem $sourceDir -Recurse -Attributes !Directory |
         Where-Object {$_.LastAccessTime -le $date}
foreach ($item in $items)
{
  $withoutRoot = Split-Path -Path $item.FullName
  $destination = $rootArchivePath + $withoutRoot.Remove(0,1)
  $destFile = $destination + "\" + $item

  Move-Item -Force -Path $item.FullName -Destination $destination -Verbose

  New-Item -ItemType SymbolicLink -Path $withoutRoot -Name $item -Value $destFile -Force -Verbose

}

【讨论】:

    猜你喜欢
    • 2011-06-29
    • 2021-05-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-13
    • 1970-01-01
    • 1970-01-01
    • 2015-12-24
    相关资源
    最近更新 更多