我试图建立基于文件夹名称的文本文件变量的剧本,发现他们在一个目录下,并将其移动到不同的目录(或其他脚本删除)。
例如:
$foldernames = Get-Content -Path "C:\Users\Me\Desktop\Folders.txt"
$startpath = "C:\Users\Me\Desktop\Test"
$topath = "C:\Users\Me\Desktop\Moved"
然后在$startpath
文件夹名称找到每个行项目,如果他们发现他们移动到$topath
。
这将做的工作。 [微笑]它假货阅读到移动显示目录的文本文件[使用获取内容做就是真正],然后聚集在源位置显示目录列表。接下来,它测试,看看是否.Name
在显示目录列表中移动。最后,它运行Move-Item
移动目录。
注意,年底-WhatIf
...删除,当你准备做你的实际测试您的测试数据集。 [笑容]
# fake reading in a text file
# in real life, use Get-Content
$ToMoveDirNameList = @(
'test1'
'TuTu'
'GoGoGadget'
'WPF'
)
$SourceDir = $env:TEMP
$DestDir = 'd:\temp'
$SourceDirList = Get-ChildItem -LiteralPath $SourceDir -Directory
foreach ($SDL_Item in $SourceDirList)
{
if ($SDL_Item.Name -in $ToMoveDirNameList)
{
Move-Item -LiteralPath $SDL_Item.FullName -Destination $DestDir -WhatIf
}
}
输出...
What if: Performing the operation "Move Directory" on target "Item: C:\Temp\test1 Destination: D:\temp\test1".
What if: Performing the operation "Move Directory" on target "Item: C:\Temp\TuTu Destination: D:\temp\TuTu".
What if: Performing the operation "Move Directory" on target "Item: C:\Temp\WPF Destination: D:\temp\WPF".
注2 - 没有错误检查。这会给你很多红色的错误文本,如果目的地已经有相同名称的目录。
注3 - 不存在未找到迪尔斯的纪录,无论是。 [笑容]