将文件与文件夹目录匹配,然后将找到的每个文件插入其文件夹目录 [英] match file with folder directory and insert each file found to its folder directory
本文介绍了将文件与文件夹目录匹配,然后将找到的每个文件插入其文件夹目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
代码:
c:\Users\H107371a\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup
c:\Users\m763671\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup
c:\Users\W575288\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup
csv文件包含:
Full Name Name
c:\ H107371a
c:\ m763671
c:\ W575288
$source = Read-Host -Prompt 'Insert SOURCE computer'
$destination = Read-Host -Prompt 'Insert DESTINATION computer'
$InvoiceList = Import-CSV -Path C:\$source-PREP.csv
for each($list in $InvoiceList){
$Name=$($list.name)
Get-Child Item -Path 'C:\' -Filter *.cmd | where {$_.Name -like "*$name*" } |
Set-Content -Path ('C:\{0}-Config.cmd' -f $_) -Force}
Copy-Item -Destination | where {$_.Name -like "*$name*" } "\\$destination\c$\Users\{0}\App Data\Roaming\Microsoft\Windows\Start Menu\Programs\Startup\" -Force
推荐答案
虽然不太确定,但我想您要:
Although not quite sure, I think you want to:
- 读取一个csv,其中名称"列必须与C:\驱动器中的.cmd文件的名称相对应.
- 如果找到了这样的文件,请在远程计算机上以其他名称复制该文件
为此,您可以使用以下代码:
For that you can use the following code:
$source = Read-Host -Prompt 'Insert SOURCE computer'
$destination = Read-Host -Prompt 'Insert DESTINATION computer'
$InvoiceList = Import-CSV -Path "C:\$source-PREP.csv"
# loop through the Names column from the CSV
foreach($userName in $InvoiceList.Name) {
# try and get a .cmd file with a basename like the name from the CSV
$file = Get-ChildItem -Path 'C:\' -Filter '*.cmd' | Where-Object {$_.BaseName -like "*$userName*" } | Select-Object -First 1
if ($file) {
# create a new file name for the copy
$newName = '{0}-Config.cmd' -f $userName
$target = '\\{0}\c$\Users\{1}\App Data\Roaming\Microsoft\Windows\Start Menu\Programs\Startup\{2}' -f $destination, $userName, $newName
Write-Host "Copying file '$($file.FullName)' to '$target'"
$file | Copy-Item -Destination $target -WhatIf
}
}
如您所见,Copy-Item可以直接复制一个具有新名称的文件.
As you can see, Copy-Item can copy a file with a new name directly.
-WhatIf
开关是出于您自身的安全考虑. ,什么也不会是副本,控制台只会向您显示将要发生的情况.如果您对所有设置都满意,请删除 -WhatIf
开关,然后再次运行代码.
The -WhatIf
switch is for your own safety. With that, nothing is copies, the console just shows you what would happen. If you are satisfied all looks OK, remove the -WhatIf
switch and run the code again.
这篇关于将文件与文件夹目录匹配,然后将找到的每个文件插入其文件夹目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文