gpt4 book ai didi

powershell - 递归添加文件到文件夹,如果不存在则创建文件夹

转载 作者:行者123 更新时间:2023-12-05 04:05:28 43 4
gpt4 key购买 nike

我正在尝试将配置文件递归添加到以下路径:C:\Users*\AppData\Roaming\

到目前为止,这是我的代码:

#Sets config file as source.
$Source = "$dirFiles\NewAgentAP\AgentAP.cfg"

#Recursive copying of a file to specific folder destination.
$Destination = 'C:\Users\*\AppData\Roaming\Trio\Trio Enterprise'
Get-ChildItem $Destination | ForEach-Object {Copy-Item -Path $Source -Destination $_ -Force}

我希望 powershell 脚本为每个用户添加路径(如果他们还没有)然后添加 .cfg 文件。我曾尝试搜索问题但没有运气。

最佳答案

我认为这里一个好的解决方案是迭代所有用户文件夹,确保目标路径存在,然后执行您的复制。

#Sets config file as source.
$Source = "$dirFiles\NewAgentAP\AgentAP.cfg"

#Recursive copying of a file to specific folder destination.
$Destination = 'AppData\Roaming\Trio\Trio Enterprise'
Get-ChildItem C:\Users\* -Directory | ForEach-Object {New-Item -Path (Join-Path $_.FullName $Destination) -ItemType "directory" -Force | Copy-Item -Path $Source -Destination $_ -Force}

使用New-Item创建文件夹,-Force开关会导致文件夹不存在时创建,并传递文件夹对象,或者如果它确实存在,它只是传递文件夹对象而不做任何其他事情。

关于powershell - 递归添加文件到文件夹,如果不存在则创建文件夹,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51091975/

43 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com