Here's the scenario: we have a production web server with several thousand files and folders to which "_DATE" is attached. We want to move them to a temporary folder (make sure they are not used), and delete the files later.
I can use:
Get-ChildItem -Path W:\IIS -Recurse | Where-Object{$_.Name -match "_\d{8,10}$"}
Get a list of all files / folders and their locations. But manually deleting them all seems to be a lot of work, especially if necessary in the future. I found some examples that almost do what I want:
cls $source = "W:\IIS" $destination = "C:\Temp\DevTest" foreach ($i in Get-ChildItem -Path $source -Recurse) { if ($i.Name -match "_\d{8,10}$") { Copy-Item -Path $i.FullName -Destination $item.FullName.ToString().Replace($source,$destination).Trim($item.Name) } }
and
cls $source = "W:\IIS" $destination = "C:\Temp\DevTest" $bin = Get-ChildItem -Path $source -Recurse | Where-Object{$_.Name -match "_\d{8,10}$"} foreach ($item in $bin) { Copy-Item -Path $item.FullName -Container -Destination $item.FullName.ToString().Replace($source,$destination).Trim($item.Name) -Recurse }
The problem with these two is that when I test them only with the instance instance, I get a flat directory and I need to keep the directory structure so that if the movement goes wrong, I can return (preferably by dragging and dropping all the folders back to the IIS folder), or I get a copy of a large number of additional folders / files that do not appear when I run the first command.
Modification of my first command:
Get-ChildItem -Path W:\IIS -Recurse | Where-Object{$_.Name -match "_\d{8,10}$"} | Copy-Item -Container -Destination C:\Temp\DevTest -Recurse
It copies everything I need, but with a flat directory tree, instead of preserving the tree structure (but replacing the source directory with the destination).
Any suggestions / comments?