简体   繁体   English

Powershell 引用文本文档并移动不在该文档中的所有内容的脚本

[英]Powershell Script that references a text doc and moves everything NOT in that doc

I created a powershell script that copies the name of every file in a given folder (it creates filename.txt, not C:\path\filename.txt) and places it all in a text doc.我创建了一个 powershell 脚本,它复制给定文件夹中每个文件的名称(它创建 filename.txt,而不是 C:\path\filename.txt)并将其全部放在文本文档中。

I'm now trying to write a powershell script that ONLY copies items to a different directory that are not contained in that text doc.我现在正在尝试编写一个 powershell 脚本,该脚本仅将项目复制到该文本文档中未包含的不同目录。

Any ideas?有任何想法吗? Thank you all.谢谢你们。

The following code will do exactly what you're after in a more PowerShell way.以下代码将以更 PowerShell 的方式完成您所追求的。 Better to use an array to hold the names to be ignored.最好使用数组来保存要忽略的名称。 If you need to use the.txt then just use Get-Content to load the.txt into the $ignoreFiles variable.如果您需要使用.txt,那么只需使用Get-Content将.txt 加载到$ignoreFiles变量中。

#-file gets files only (not folders)
$ignoreFiles = (Get-ChildItem -Path "C:\Users\YOURS" -file).name
$checkFolder = Get-ChildItem -Path "C:\Users\YOURS" -file
$targetFolder = "C:\Users\YOURS"
 
foreach ($t in $checkFolder){
    if($t.name -notin $ignoreFiles){
        Copy-Item $t.fullname $targetFolder
    }
}
#-file gets files only (not folders)
$listPath = "\\server\C$\exclusionpath\list.txt"
$downloadPath = "\\adifferentserver\download\"
$targetFolder = "\\adifferentserver\Processing\"
$ignoreFiles = Get-Content -Path $listPath
$checkFolder = Get-ChildItem -Path $downloadPath -File -Exclude $ignoreFiles - 
 Name
foreach ($t in $checkFolder){
#Copy-Item $t.fullname $targetFolder
Copy-Item (Get-ChildItem -Path $downloadPath -File | Where-Object {$_.Name -eq 
$t}).FullName $targetFolder
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM