Move old files in folders

Hi

I’ve a lot of files in a folder and want to create a folder for each month and move files in that folder.
f. eks: Start from 201301

  • Creat a folder 201301 move all files with modified date 2013-01-01 ~2013-01-31 and so on till today.
  • When I'm done with that I want a script that move only remaing daily files
any suggestion would be appriciated.

Regards

Shahid

Hi Shahid64624,

 

What problems are you running into ?

 

Thanks for reply

I’dont know how to program that in PS. I got the script

Get-ChildItem C:\Application\Log*.log | Where-Object {$_.LastWriteTime -lt (Get-Date).AddDays(-90)} | Move-Item -Destination

But how to automate it with YYYYMM. Could you please modify the script.

Get-ChildItem C:\Application\Log*.log | Where-Object {$_.LastWriteTime -ep 20130101 - 20130131} | Move-Item -Destination C:\Application\Log{201301}

and loop till now 201902

This is a start to get the dates between begin and end of the months

[pre]

$beginmonth=1…12 | % { (New-Object DateTime(2019,01,01)).AddMonths($) }
$endmonth=1…12 | % { (New-Object DateTime(2019,01,31)).AddMonths($
) }

[/pre]

Great Thanks :slight_smile:

I can spend some time on it… I’m new in PS and it is a great fun :slight_smile:

 

We all luv us some PS, which is why we are here, but PS is not the most direct answer for everything.

In windows you have Robocopy, and you can do what you are after without writing any code, or if you choose, you can use robocopy with PS or PS alone as what was already provided.

You say, this…

I'dont know how to program that in PS. I got the script

Then it is vital that you get ramped up to avoid much of the unnecessary frustration, confusion, errors, etc., that you are going to encounter.
See this discussion list:

https://www.reddit.com/r/PowerShell/comments/afqmmw/i_want_to_help_my_husband_advance_his_powershell/ee3k6p6/?context=3

Havent tested the code,

 

But something like

 

[pre]

$range = 0..12
foreach($r in $range){
[datetime]$beginmonth=$r| % { (New-Object DateTime(2019,01,01)).AddMonths($_) }
[datetime]$endmonth=$r | % { (New-Object DateTime(2019,01,31)).AddMonths($_) } Write-Host $beginmonth.ToString("yyyyMMdd") $folder = $beginmonth.ToString("yyyyMMdd")
New-Item "c:\log\$folder" -ItemType Directory Get-ChildItem C:\Application\Log\*.log | Where-Object {$_.LastWriteTime -gt $beginmonth} and $_.LastWriteTime -LT $endmonth |Move-Item -Destination "c:\log\$folder" Write-Host $endmonth Write-Host '-----'
[/pre]
 

Something like this

 

[pre]

$range = 0..12
foreach($r in $range){
[datetime]$beginmonth=$r| % { (New-Object DateTime(2019,01,01)).AddMonths($_) } [datetime]$endmonth=$r | % { (New-Object DateTime(2019,01,31)).AddMonths($_) } Write-Host $beginmonth.ToString("yyyyMMdd") $folder = $beginmonth.ToString("yyyyMMdd")
New-Item "c:\log\$folder" -ItemType Directory Get-ChildItem C:\Application\Log\*.log | Where-Object {$_.LastWriteTime -gt $beginmonth} and $_.LastWriteTime -LT $endmonth |Move-Item -Destination "c:\log\$folder" Write-Host $endmonth Write-Host '-----'
}
[/pre]

hi postanote

Thanks a lot for your suggestion :slight_smile:

OMG Brat, You are my hero :slight_smile: :slight_smile: :slight_smile: :slight_smile:

Really thank you for your time.

Best Regards

Shahid