[英]How to copy a file to multiple folders in PowerShell
I am running a script that has multiple environments in it that can be selected from a window that pops up.我正在运行一个脚本,其中有多个环境,可以从弹出的 window 中选择。 the only problem I have run into is when I want to set up the script to copy from a source function that I have created and put it into multiple locations at once.我遇到的唯一问题是当我想设置脚本以从我创建的源 function 复制并一次将其放入多个位置时。
The part of the code I need help with is posted below.我需要帮助的代码部分发布在下面。
$Source = Select-Boss
$destination = 'D:\parts','D:\labor','D:\time','D:\money'
"Calling Copy-Item with parameters source: '$source', destination: '$destination'."
Copy-Item -Path $source -Destination $destination
The section below is how the rest of the copy functions are set up in the script, so you have a better understanding of what the main section copies are.下面的部分是rest 的拷贝函数在脚本中是如何设置的,让你更好的理解主要部分的拷贝是什么。
$Source = Select-Boss
$destination = 'D:\parts'
"Calling Copy-Item with parameters source: '$source', destination: '$destination'."
Copy-Item -Path $source -Destination $destination
But for one specific part I need to have it copied to multiple locations.但是对于一个特定的部分,我需要将它复制到多个位置。 I need this to happen since I don't have to change the server that I logged into and go to a different one.我需要这样做,因为我不必将登录的服务器和 go 更改为不同的服务器。 It is all done in one location and I was hoping to make things easier and not write a whole bunch of little coding to go and copy and save in the folders.这一切都在一个地方完成,我希望让事情变得更容易,而不是将一大堆小代码写入 go 并复制并保存在文件夹中。
copy-item
only takes a single value for its -destination
parameter, so you need a loop of some type. copy-item
的-destination
参数仅使用一个值,因此您需要某种类型的循环。
Assuming you want the same file name in multiple folders: 假设您要在多个文件夹中使用相同的文件名:
$destFolders | Foreach-Object { Copy-Item -Path $Source -dest (Join-Path $_ $destFileName) }
should do it. 应该这样做。
What I think you want is something like this: 我认为您想要的是这样的:
$Source = Select-Boss
$destination = @("D:\parts","D:\labor","D:\time","D:\money")
# Calling Copy-Item with parameters source: '$source', destination: '$destination'."
foreach ($dir in $destination)
{
Copy-Item -Path $source -Destination $dir
}
This code is making an array of folders, then iterates through each one, copying your file to it. 这段代码创建了一个文件夹数组,然后遍历每个文件夹,然后将文件复制到其中。
https://www.petri.com/use-powershell-to-copy-files-to-multiple-locations
dir c:\work\*.txt | copy-item -Destination $destA -PassThru | copy -dest $destB -PassThru
This is the best and easy solution which I have used. 这是我使用过的最好,最简单的解决方案。
In my case, it is a network location but it can be used for local system too. 就我而言,它是一个网络位置,但也可以用于本地系统。
"\\\\foo\\foo"
location contains 10 folders by user name. "\\\\foo\\foo"
位置按用户名包含10个文件夹。 #
use double slash (it's not showing here on StackOverflow) #
使用双斜杠(在StackOverflow上未显示)
dir "\\foo\foo\*" | foreach-object { copy-item -path d:\foo -destination $_ } -verbose
You must have write permission on the network share and destination folders. 您必须对网络共享和目标文件夹具有写权限。
# Copy one or more files to another directory and subdirectories
$destinationFrom = "W:\_server_folder_files\basic"
$typeOfFiles = "php.ini", "index.html"
$filesToCopy = get-childitem -Path $destinationFrom -Name -include $typeOfFiles -Recurse
$PathTo = "W:\test\"
$destinationPath = Get-ChildItem -path $PathTo -Name -Exclude "*.*" -recurse -force
foreach ($file_item in $filesToCopy)
{
#Remove-Item -Path (Join-Path -Path $PathTo -ChildPath $file_item)
Copy-Item -Path (Join-Path -Path $destinationFrom -ChildPath $file_item) -Destination $PathTo
# Write-Verbose (Join-Path -Path $destinationFrom -ChildPath $file_item) -verbose
ForEach($folder in $destinationPath)
{
#Remove-Item -Path (Join-Path -Path (Join-Path -Path $PathTo -ChildPath $folder) -ChildPath $file_item)
#Copy-Item -Path (Join-Path -Path $destinationFrom -ChildPath $file_item) -Destination (Join-Path -Path $PathTo -ChildPath $folder)
# Write-Verbose (Join-Path -Path $PathTo -ChildPath $folder) -verbose
}
}
`$filesToCopy = get-Content C:\Users\lukhm\Desktop\Files.txt $destinationPath = Get-Content C:\Users\lukhm\Desktop\Folders.txt -force ForEach($folder in $destinationPath) { #Remove-Item -Path (Join-Path -Path (Join-Path -Path $PathTo -ChildPath $folder) -ChildPath $file_item) Copy-Item -Path $file_item -Destination $folder # Write-Verbose (Join-Path -Path $PathTo -ChildPath $folder) -verbose } } `$filesToCopy = get-Content C:\Users\lukhm\Desktop\Files.txt $destinationPath = Get-Content C:\Users\lukhm\Desktop\Folders.txt -foreach($destinationPath 中的$folder){ #Remove -Item -Path (Join-Path -Path (Join-Path -Path $PathTo -ChildPath $folder) -ChildPath $file_item) 复制项目 -Path $file_item -Destination $folder # Write-Verbose (Join-Path -Path $ PathTo -ChildPath $folder) -verbose } }
Copy-Item : The filename, directory name, or volume label syntax is incorrect.
At line:18 char:9
+ Copy-Item -Path $file_item -Destination $folder
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [Copy-Item], IOException
+ FullyQualifiedErrorId : System.IO.IOException,Microsoft.PowerShell.Comman
ds.CopyItemCommand`
I have mentioned file path with file name, folder path with folder name in.txt files.我在 .txt 文件中提到了带有文件名的文件路径,带有文件夹名称的文件夹路径。 How do I get the code to use them to copy mentioned files to mentioned folders?如何获取代码以使用它们将提到的文件复制到提到的文件夹中?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.