SOLVED

.ps1 script: how to loop in a range of dates

Copper Contributor

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 to increment day by day my loop, extract day.month.year from the date and then compose the new string for my path......

 

can somebody help me?

 

thanks!!!

23 Replies
:D Let me know, give me some likes on my comments and mark it as a solution if it's solved will be enough :p

Hi@Harm_Veenstra , we got it!!!

 

I took the very first version and a little bit arranged, this is the very final version, it work fine!!!

 

 

$sourcepath = 'C:\MyPath'
$myfolder1 = 'a\b\c\myfolder1'
$destinationpath = 'C:\DestPath\myfolder1'

Write-Warning ($sourcepath)
Write-Warning ($myfolder1)
Write-Warning ($destinationpath)

$folders = Get-ChildItem -Path $sourcepath -Directory

foreach ($folder in $folders | Sort-Object Name) {
	if (Test-Path "$($folder.fullname)\$($myfolder1)") {
		Write-Warning ("Processing file: $($folder.fullname)\$($myfolder1)" -f $folder.FullName)
		Write-Warning ("To be copied to: $($destinationpath)\$($myfolder1)" -f $folder.FullName)
		robocopy2.exe "$($folder.fullname)\$($myfolder1)" "$($destinationpath)\$($myfolder1)" /S /XO /LOG+:"$($destinationpath)\robocopy.log" /R:1 /W:1
	}
    else {
        Write-Warning ("Folder {0} not found in {1} or not enough permissions" -f $myfolder1, $folder.FullName)
    }
}

 

 

thank you so much my friend!

for your patience, kindness and a huge huge proficiency!!!

cheers!
Yes :) Good to hear!