Forum Discussion
.ps1 script: how to loop in a range of dates
- Dec 12, 2022
Just tested this script, this works:
foreach ($folder in Get-ChildItem -Path c:\mypath -Directory | Sort-Object Name) { try { Test-Path "$($folder.fullname)\myfolder1" -ErrorAction Stop | Out-Null Copy-Item "$($folder.fullname)\myfolder1" -Recurse -Destination C:\destpath\myfolder1 -Force:$true -Confirm:$false -ErrorAction Stop Write-Host ("Processing {0}" -f $folder.fullname) -ForegroundColor Green } catch { Write-Warning ("Folder myfolder1 not found in {0} or not enough permissions" -f $folder.FullName) } }Source directory:
Destination directory:It will check permissions and copy if it has permissions and will overwrite if the file is already present, it will show a warning if the folder1 folder was not found in a directory.
You can test it for yourself with a test directory structure 🙂
Just tested this script, this works:
foreach ($folder in Get-ChildItem -Path c:\mypath -Directory | Sort-Object Name) {
try {
Test-Path "$($folder.fullname)\myfolder1" -ErrorAction Stop | Out-Null
Copy-Item "$($folder.fullname)\myfolder1" -Recurse -Destination C:\destpath\myfolder1 -Force:$true -Confirm:$false -ErrorAction Stop
Write-Host ("Processing {0}" -f $folder.fullname) -ForegroundColor Green
}
catch {
Write-Warning ("Folder myfolder1 not found in {0} or not enough permissions" -f $folder.FullName)
}
}
Source directory:
Destination directory:
It will check permissions and copy if it has permissions and will overwrite if the file is already present, it will show a warning if the folder1 folder was not found in a directory.
You can test it for yourself with a test directory structure 🙂
many many many thanks!!!!
- Dec 12, 2022Let us know if it works out for you, please mark my answer as best response to mark it as solved if it did 🙂
- marco69Dec 12, 2022Copper ContributorI forgot one important point: I have to overwrite only if the file is newer...
how can I modify the script to do that?
thank you and excuse me for this missing- Dec 12, 2022
marco69 Changed it to also check for that:
$sourcepath = 'C:\MyPath' $destinationpath = 'C:\DestPath\myfolder1' foreach ($folder in Get-ChildItem -Path $sourcepath -Directory | Sort-Object Name) { try { Test-Path "$($folder.fullname)\myfolder1" -ErrorAction Stop | Out-Null foreach ($file in Get-ChildItem -Path "$($folder.fullname)\myfolder1" -File) { if (Test-Path -Path "$($destinationpath)\$($file.name)") { $destinationfile = Get-ChildItem -Path "$($destinationpath)\$($file.Name)" if ($file.LastWriteTime -gt $destinationfile.LastWriteTime) { Copy-Item "$($folder.fullname)\myfolder1\$($file.Name)" -Destination $destinationpath -Force:$true -Confirm:$false -ErrorAction Stop Write-Host ("Processing {0} in {1} and overwriting old version" -f $file.name, $folder.fullname) -ForegroundColor Green } else { Write-Host ("File {0} from {1} already present in {2}, no change needed" -f $file.Name, $folder.Name, $destinationpath) -ForegroundColor Green } } else { Copy-Item "$($folder.fullname)\myfolder1\$($file.name)" -Destination $destinationpath -ErrorAction Stop Write-Host ("Copying new file {0} from {1} to {2}" -f $file.name, $folder.fullname, $destinationpath) -ForegroundColor Green } } } catch { Write-Warning ("Folder myfolder1 not found in {0} or not enough permissions" -f $folder.FullName) } }But... Are there multiple directories in c:\mypath\xxxxx\myfolder1? This script copies files from one directory to another, is that enough?