Johnson Lee
Johnson Lee

Reputation: 1

Powershell Search older files, rename and copy newer files to all found locations

Tried to copy several files (newer version) from one location to several different locations (older version) based on search result. The search and rename worked fine; but copy didn't work. Just need a way to point the current search directory as -destination folder.

  1. Search for older files (DB2JCC) in all locations C: & D: drivers
  2. Rename files from DB2JCC* to OLD_DB2JCC*.*
  3. Copy the new version DB2JCC* files to all step 2 (above step) locations

Script used -

search for file name older than '2018-05-02' and replace with newer version files

Get-ChildItem C:\, D:\ -include '*db2jcc*' -Recurse -ErrorAction SilentlyContinue | 
Where-Object { $_.LastWriteTime -lt '2018-05-02' } | 
Rename-Item -NewName { $_.name -replace 'DB2JCC','Old_DB2JCC' }
ForEach-Object { Copy-Item 'C:\Program Files\IBM\SQLLIB\java\DB2JCC*.*' -Destination $ENV:Temp }

Any help or suggestion will be appreciated.

Thanks, Johnson

Upvotes: 0

Views: 94

Answers (1)

Abraham Zinala
Abraham Zinala

Reputation: 4694

Based off your answer, you can try something a long the lines of this:

[System.Collections.ArrayList]$Paths = @()
Get-ChildItem C:\,D:\ -Filter "*db2jcc*" -Recurse -ErrorAction SilentlyContinue | 
    Where-Object { $_.LastWriteTime -lt '2018-05-02' } | ForEach-Object -Process {
        $null = $Paths.Add($_.FullName)
        Rename-Item -Path $_.FullName -NewName $_.Name.Replace('DB2JCC','Old_DB2JCC') -ErrorAction SilentlyContinue } 
        
Foreach($Path in $Paths){
    $Path = $Path -replace '[^\\]+$'
    Copy-Item "C:\Program Files\IBM\SQLLIB\java\" -Filter "DB2JCC*" -Recurse -Destination $Path }

Upvotes: 0

Related Questions