Forum Discussion
marco69
Dec 07, 2022Copper Contributor
.ps1 script: how to loop in a range of dates
Hi all, I have to copy the content of the folders: C:\mypath\YYYYDDMM\myfolder1 into one folder destination. And: YYYYMMDD is from 20140101 to 20211231 I'm struggling to find a way t...
- 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 🙂
marco69
Dec 12, 2022Copper Contributor
Hi Harm_Veenstra!
I have this situation:
C:\mypath\20220421\myfolder1
C:\mypath\20220422\myfolder1
C:\mypath\20220425\myfolder1
C:\mypath\20220511\myfolder1
....
C:\mypath\20220718\myfolder1
C:\mypath\20220723\myfolder1
...
C:\mypath\20221212\myfolder1
starting from the very first folder (the oldest, in the example above is the folder at 21Apr2022) I have to copy the content of myfolder1 to a destination directory, for example:
c:\destpath\myfolder1
Then:
copy C:\mypath\20220421\myfolder1\* -> c:\destpath\myfolder1\*
copy C:\mypath\20220422\myfolder1\* -> c:\destpath\myfolder1\*
copy C:\mypath\20220425\myfolder1\* -> c:\destpath\myfolder1\*
...
copy C:\mypath\20221212\myfolder1\* -> c:\destpath\myfolder1\*
For info, just to better clarify, when copying I have to overwrite a file if it already exists and normally copy if it's new.
mf
I have this situation:
C:\mypath\20220421\myfolder1
C:\mypath\20220422\myfolder1
C:\mypath\20220425\myfolder1
C:\mypath\20220511\myfolder1
....
C:\mypath\20220718\myfolder1
C:\mypath\20220723\myfolder1
...
C:\mypath\20221212\myfolder1
starting from the very first folder (the oldest, in the example above is the folder at 21Apr2022) I have to copy the content of myfolder1 to a destination directory, for example:
c:\destpath\myfolder1
Then:
copy C:\mypath\20220421\myfolder1\* -> c:\destpath\myfolder1\*
copy C:\mypath\20220422\myfolder1\* -> c:\destpath\myfolder1\*
copy C:\mypath\20220425\myfolder1\* -> c:\destpath\myfolder1\*
...
copy C:\mypath\20221212\myfolder1\* -> c:\destpath\myfolder1\*
For info, just to better clarify, when copying I have to overwrite a file if it already exists and normally copy if it's new.
mf
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 🙂
- marco69Dec 12, 2022Copper ContributorYou are my Hero!!!!
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