如何将Windows快捷方式文件转换为常规文件,但保留快捷方式名称?

如何将Windows快捷方式文件转换为常规文件,但保留快捷方式名称?,windows,powershell,Windows,Powershell,我的照片目录树中有一百万张照片。最近,我创建了另一个快捷方式目录来选择一组我想要使用的照片。我使用了特殊的快捷方式文件名设置,以我想要的顺序对照片进行排序 问题是,当我将照片快捷方式目录导入Adobe Lightroom时,Lightroom使用的是底层文件名,而不是快捷方式名称。这会丢失我想要排序的快捷方式名称 我发现这个Powershell程序可以将快捷方式转换为实际文件,但在写入目标时,它也使用基础文件名,而不是快捷方式名: $wshShell = New-Object -ComObjec

我的照片目录树中有一百万张照片。最近,我创建了另一个快捷方式目录来选择一组我想要使用的照片。我使用了特殊的快捷方式文件名设置,以我想要的顺序对照片进行排序

问题是,当我将照片快捷方式目录导入Adobe Lightroom时,Lightroom使用的是底层文件名,而不是快捷方式名称。这会丢失我想要排序的快捷方式名称

我发现这个Powershell程序可以将快捷方式转换为实际文件,但在写入目标时,它也使用基础文件名,而不是快捷方式名:

$wshShell = New-Object -ComObject wscript.shell
# Source directory to copy from
$LinksFolder = 'D:\ShortcutFolder'
# Destination USB drive
$ExtDrive = 'E:'
# "fullname" gets the underlying filename, not the shortcut name
gci $LinksFolder *.lnk -recurse | select -expand fullname | Foreach{
       $Source = ($wshShell.CreateShortcut($_)).TargetPath
       $Destination = (split-path $_ ).Replace($LinksFolder, $ExtDrive)
       # Add -whatif to this to test
       Copy-Item $Source $Destination 
}
如何将快捷方式文件转换为包含快捷方式目标内容的实际文件,但对写入的文件使用快捷方式文件名

提前谢谢
Ben

诀窍是在
$Destination
中使用快捷方式的
$\uName
,但保留
$Source
文件的扩展名:

$linksFolder = 'D:\ShortcutFolder'
$DestFolder = 'D:\ShortcutFolder' #Do you want all the photos to end in the links folder?
$wshShell = New-Object -ComObject wscript.shell

gci $LinksFolder '*.lnk' -recurse | Foreach{
       $Source = Get-item ($wshShell.CreateShortcut($_.FullName)).TargetPath

       #change the extension to match source. Hopefully there's no extra '.lnk' in the name
       $Destination = Join-Path $DestFolder $_.Name.Replace('.lnk',$Source.Extension) 
       Copy-Item $Source $Destination -WhatIf #Take off -Whatif when you like the results
}

为了澄清这一点,我不得不对Rich的优秀答案做一些细微的修改,如下所示:

$wshShell = New-Object -ComObject wscript.shell
$LinksFolder = 'D:\pictures'
# Dest is my USB thumbdrive
$DestFolder = 'L:'   
$wshShell = New-Object -ComObject wscript.shell
# my source files look like "2021_02_IMG_6299.jpg - Shortcut.lnk"
gci $LinksFolder '*.lnk' -recurse | Foreach{
       $Source = Get-item ($wshShell.CreateShortcut($_.FullName)).TargetPath
       #change the extension to match source. Hopefully there's no extra '.lnk' in the name
       $Destination = Join-Path $DestFolder $_.Name.Replace(' - Shortcut.lnk','') # changed Replace
       Copy-Item $Source $Destination -WhatIf     #Take off -Whatif when you like the results
}

不清楚您试图使用
md$Destination
命令执行什么操作。是否希望所有照片都保存在您的
$links文件夹中
,还是希望保留
$Source
图像文件夹的某些部分?忽略“md$Destination”。我甚至没用那个。我已经从示例中删除了它。这很有效!!(稍加修改)你是一位学者和绅士!我的文件名看起来像“2021_02_IMG_6299.jpg-Shortcut.lnk”。我必须更改以将$Destination行分配给以下内容:$Destination=Join Path$DestFolder$\.Name.Replace('-Shortcut.lnk','')