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 🙂
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?
- marco69Dec 13, 2022Copper Contributor
Hi Harm_Veenstra,
I have to copy myfolder1 to destination, so this is ok.
I have a weird behavior when I run the script, it gets me an error not founding the files under myfolder1, but some of those files exist...
When I? wun theprevious version of the script it works very fine(simply it didnt consider the age of the files but it worked very well).
This is the error:
Get-ChildItem : Cannot find path 'C:\MyPath\20210618\a\b\c\myfolder1' because it does not exist. At C:\scripts\myscript.ps1:7 char:27 + ... h ($file in Get-ChildItem -Path "$($folder.fullname)\$myfolder1" -Fil ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : ObjectNotFound: (C:\MyPath\20210618\a\b\c\myfolder1:String) [Get-ChildItem], ItemNotFoundException + FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetChildItemCommandthis is the line where the error refers:
foreach ($file in Get-ChildItem -Path "$($folder.fullname)\$myfolder1" -File) {- Dec 13, 2022
marco69 I fixed it 🙂 It wasn't doing a Recurse in subdirectories if any and did not pass that along correctly, the script is now like this:
$sourcepath = 'C:\MyPath' $destinationpath = 'C:\DestPath\myfolder1' foreach ($folder in Get-ChildItem -Path $sourcepath -Directory -Recurse | Sort-Object Name) { try { Test-Path "$($folder.fullname)\myfolder1" -ErrorAction Stop | Out-Null foreach ($file in Get-ChildItem -Path "$($folder.fullname)\myfolder1" -Recurse -File) { if (Test-Path -Path "$($destinationpath)\$($file.name)") { $destinationfile = Get-ChildItem -Path "$($destinationpath)\$($file.Name)" if ($file.LastWriteTime -gt $destinationfile.LastWriteTime) { Copy-Item $file.FullName -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 $file.FullName -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) } }