Powershell PS1获取儿童复制命令基本

发布于 2025-01-18 13:52:22 字数 2231 浏览 0 评论 0原文

我不是编码员。我有一个 Powershell 脚本 .ps1,它进入“db”的所有子文件夹并删除所有我想删除的文件。它适用于我需要的东西。代码如下:

Get-ChildItem "C:\Program Files (x86)\Steam\steamapps\common\Game 2022\data\database\db\*\dbc\permanent" -Include filmename00.dbc -Recurse | Remove-Item -Verbose

Get-ChildItem "C:\Program Files (x86)\Steam\steamapps\common\Game 2022\data\database\db\*\dbc\permanent" -Include filename11.dbc -Recurse | Remove-Item -Verbose

Get-ChildItem "C:\Program Files (x86)\Steam\steamapps\common\Game 2022\data\database\db\*" -Include "filename11.dbc" -Recurse | Remove-Item -Verbose

Get-ChildItem "C:\Program Files (x86)\Steam\steamapps\common\Game 2022\data\database\db\*\dbc\permanent" -Include filmename22.dbc -Recurse | Remove-Item -Verbose

Get-ChildItem "C:\Program Files (x86)\Steam\steamapps\common\Game 2022\data\database\db\*\lnc\all" -Include filmename33.lnc -Recurse | Remove-Item -Verbose

Get-ChildItem "C:\Program Files (x86)\Steam\steamapps\common\Game 2022\data\database\db\*\edt\permanent" -Include filmename44.edt -Recurse | Remove-Item -Verbose

现在我想以通用的方式以相同的方式复制2个文件来模拟上面的代码,但我不知道该怎么做。

我正在使用此代码来复制文件:

Copy-Item -Path "File to copy 1.lnc" -Destination "C:\Program Files (x86)\Steam\steamapps\common\Game 2022\data\database\db\2220\lnc\all\" -Recurse

Copy-Item -Path "File to copy 2.lnc" -Destination "C:\Program Files (x86)\Steam\steamapps\common\Game 2022\data\database\db\2220\lnc\all\" -Recurse

Copy-Item -Path "File to copy 1.lnc" -Destination "C:\Program Files (x86)\Steam\steamapps\common\Game 2022\data\database\db\2230\lnc\all\" -Recurse

Copy-Item -Path "File to copy 2.lnc" -Destination "C:\Program Files (x86)\Steam\steamapps\common\Game 2022\data\database\db\2230\lnc\all\" -Recurse

Copy-Item -Path "File to copy 1.lnc" -Destination "C:\Program Files (x86)\Steam\steamapps\common\Game 2022\data\database\db\2240\lnc\all\" -Recurse

Copy-Item -Path "File to copy 2.lnc" -Destination "C:\Program Files (x86)\Steam\steamapps\common\Game 2022\data\database\db\2240\lnc\all\" -Recurse

有人可以帮我创建 2 个通用代码(我认为每个副本一行),将“lnc\all”文件夹中的 2 个文件复制到“db”中现有的任何子文件夹中文件夹?

也就是说,用 2 个通用代码替换 6 个代码,以防“db”中还有其他文件夹,它也可以工作。谢谢。

我不是程序员,如果可能的话,请以现成的格式向我发送代码(作为示例,谢谢)

I'm not a coder. I have a Powershell script .ps1 that goes into all subfolders of "db" and deletes the files I want to delete in all of them. It works for what I need. The following code:

Get-ChildItem "C:\Program Files (x86)\Steam\steamapps\common\Game 2022\data\database\db\*\dbc\permanent" -Include filmename00.dbc -Recurse | Remove-Item -Verbose

Get-ChildItem "C:\Program Files (x86)\Steam\steamapps\common\Game 2022\data\database\db\*\dbc\permanent" -Include filename11.dbc -Recurse | Remove-Item -Verbose

Get-ChildItem "C:\Program Files (x86)\Steam\steamapps\common\Game 2022\data\database\db\*" -Include "filename11.dbc" -Recurse | Remove-Item -Verbose

Get-ChildItem "C:\Program Files (x86)\Steam\steamapps\common\Game 2022\data\database\db\*\dbc\permanent" -Include filmename22.dbc -Recurse | Remove-Item -Verbose

Get-ChildItem "C:\Program Files (x86)\Steam\steamapps\common\Game 2022\data\database\db\*\lnc\all" -Include filmename33.lnc -Recurse | Remove-Item -Verbose

Get-ChildItem "C:\Program Files (x86)\Steam\steamapps\common\Game 2022\data\database\db\*\edt\permanent" -Include filmename44.edt -Recurse | Remove-Item -Verbose

Now I want to copy 2 files in the same way in a generic way to simulate this code above but I don't know how to do it.

I'm using this code to copy the files:

Copy-Item -Path "File to copy 1.lnc" -Destination "C:\Program Files (x86)\Steam\steamapps\common\Game 2022\data\database\db\2220\lnc\all\" -Recurse

Copy-Item -Path "File to copy 2.lnc" -Destination "C:\Program Files (x86)\Steam\steamapps\common\Game 2022\data\database\db\2220\lnc\all\" -Recurse

Copy-Item -Path "File to copy 1.lnc" -Destination "C:\Program Files (x86)\Steam\steamapps\common\Game 2022\data\database\db\2230\lnc\all\" -Recurse

Copy-Item -Path "File to copy 2.lnc" -Destination "C:\Program Files (x86)\Steam\steamapps\common\Game 2022\data\database\db\2230\lnc\all\" -Recurse

Copy-Item -Path "File to copy 1.lnc" -Destination "C:\Program Files (x86)\Steam\steamapps\common\Game 2022\data\database\db\2240\lnc\all\" -Recurse

Copy-Item -Path "File to copy 2.lnc" -Destination "C:\Program Files (x86)\Steam\steamapps\common\Game 2022\data\database\db\2240\lnc\all\" -Recurse

Can someone help me create 2 generic codes (I think one line for each copy) to copy the 2 files inside the "lnc\all" folders to any subfolder existing in the "db" folder?

That is, replace the 6 codes with 2 generic ones, in case there are other folders in the "db" it works too. Thanks.

I'm not a programmer, if possible send me the code in ready format (as an example, thanks)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

谈情不如逗狗 2025-01-25 13:52:22

如注释中所述,对于代码的第一部分,除了具有参考之外,没有自动化的方法( hash表在这种情况下),以定位目录及其特定文件。哈希表的是目标路径,而 values 是用include参数来定位的文件:

$initialDir = 'C:\Program Files (x86)\Steam\steamapps\common\Game 2022\data\database\db'
$targets = @{
    '\*' = 'filename11.dbc'
    '\*\dbc\permanent' = 'filmename00.dbc', 'filename11.dbc', 'filmename22.dbc'
    '\*\lnc\all' = 'filmename33.lnc'
    '\*\edt\permanent' = 'filmename44.edt'
}

foreach($key in $targets.PSBase.Keys) {
    $path = Join-Path $initialDir -ChildPath $key
    Get-ChildItem $path -Include $targets[$key] -Recurse |
        Remove-Item -Verbose
}

对于第二个代码,它可以通过这种方式自动化,请注意,-path来自copy-item的参数可以采取一系列路径来复制,因为我们正在复制相同的2个文件,您可以将两者的路径存储在变量中。然后,对于目标路径,似乎应该这样做:

'C:\Program Files (x86)\Steam\steamapps\common\Game 2022\data\database\db\*\lnc\all\'

首先将所有目录在该路径中获取,每个目录将文件复制到它们:

$filesToCopy = @(
    # Use absolute paths here to be safe
    'File to copy 1.lnc'
    'File to copy 2.lnc'
)
$destination = 'C:\Program Files (x86)\Steam\steamapps\common\Game 2022\data\database\db\*\lnc\all\'

Get-ChildItem $destination -Directory | ForEach-Object {
    Copy-Item $filesToCopy -Destination $_.FullName
}

For the first part of your code, as explained in comments, there is no automated way to do it aside from having a reference (hash table in this case) to target the directories and it's specific files. The keys of the hash table are the target paths and the values are the files to target with the -Include parameter:

$initialDir = 'C:\Program Files (x86)\Steam\steamapps\common\Game 2022\data\database\db'
$targets = @{
    '\*' = 'filename11.dbc'
    '\*\dbc\permanent' = 'filmename00.dbc', 'filename11.dbc', 'filmename22.dbc'
    '\*\lnc\all' = 'filmename33.lnc'
    '\*\edt\permanent' = 'filmename44.edt'
}

foreach($key in $targets.PSBase.Keys) {
    $path = Join-Path $initialDir -ChildPath $key
    Get-ChildItem $path -Include $targets[$key] -Recurse |
        Remove-Item -Verbose
}

For the second code, it could be automated this way, note that the -Path parameter from Copy-Item can take an array of paths to copy, in that sense, since we're copying the same 2 files, you can store the paths of both in a variable. Then for the destination path, seems like this should do it:

'C:\Program Files (x86)\Steam\steamapps\common\Game 2022\data\database\db\*\lnc\all\'

First get all the directories in that path, and the for each directory copy the files to them:

$filesToCopy = @(
    # Use absolute paths here to be safe
    'File to copy 1.lnc'
    'File to copy 2.lnc'
)
$destination = 'C:\Program Files (x86)\Steam\steamapps\common\Game 2022\data\database\db\*\lnc\all\'

Get-ChildItem $destination -Directory | ForEach-Object {
    Copy-Item $filesToCopy -Destination $_.FullName
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文