[英]How to copy a file to multiple folders in PowerShell
我正在运行一个脚本,其中有多个环境,可以从弹出的 window 中选择。 我遇到的唯一问题是当我想设置脚本以从我创建的源 function 复制并一次将其放入多个位置时。
我需要帮助的代码部分发布在下面。
$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
下面的部分是rest 的拷贝函数在脚本中是如何设置的,让你更好的理解主要部分的拷贝是什么。
$Source = Select-Boss
$destination = 'D:\parts'
"Calling Copy-Item with parameters source: '$source', destination: '$destination'."
Copy-Item -Path $source -Destination $destination
但是对于一个特定的部分,我需要将它复制到多个位置。 我需要这样做,因为我不必将登录的服务器和 go 更改为不同的服务器。 这一切都在一个地方完成,我希望让事情变得更容易,而不是将一大堆小代码写入 go 并复制并保存在文件夹中。
copy-item
的-destination
参数仅使用一个值,因此您需要某种类型的循环。
假设您要在多个文件夹中使用相同的文件名:
$destFolders | Foreach-Object { Copy-Item -Path $Source -dest (Join-Path $_ $destFileName) }
应该这样做。
我认为您想要的是这样的:
$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
}
这段代码创建了一个文件夹数组,然后遍历每个文件夹,然后将文件复制到其中。
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
这是我使用过的最好,最简单的解决方案。
就我而言,它是一个网络位置,但也可以用于本地系统。
"\\\\foo\\foo"
位置按用户名包含10个文件夹。 #
使用双斜杠(在StackOverflow上未显示)
dir "\\foo\foo\*" | foreach-object { copy-item -path d:\foo -destination $_ } -verbose
您必须对网络共享和目标文件夹具有写权限。
# 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 -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`
我在 .txt 文件中提到了带有文件名的文件路径,带有文件夹名称的文件夹路径。 如何获取代码以使用它们将提到的文件复制到提到的文件夹中?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.