powershellget-childitemcopy-itemps1

Powershell ps1 Get Children Copy command basic


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)


Solution

  • 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
    }