# Move old files in folders

**URL:** <https://forums.powershell.org/t/move-old-files-in-folders/12002>\
**Category:** PowerShell Help\
**Created:** [February 5, 2019, 7:54am UTC](https://forums.powershell.org/t/move-old-files-in-folders/12002 "2019-02-05T07:54:25Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![shahid646](https://avatars.discourse-cdn.com/v4/letter/s/6de8d8/32.png) [@shahid646](https://forums.powershell.org/u/shahid646)\
**Post date:** [February 5, 2019, 7:54am UTC](https://forums.powershell.org/t/move-old-files-in-folders/12002/1 "2019-02-05T07:54:25Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![bdevogt](https://avatars.discourse-cdn.com/v4/letter/b/eada6e/32.png) [@bdevogt](https://forums.powershell.org/u/bdevogt)\
**Post date:** [February 5, 2019, 8:06am UTC](https://forums.powershell.org/t/move-old-files-in-folders/12002/2 "2019-02-05T08:06:26Z")

</div>

Hi [Shahid64624](https://powershell.org/profile/shahid646/),

&nbsp;

What problems are you running into ?

&nbsp;

---

<div class="post-metadata">

**Author:** ![shahid646](https://avatars.discourse-cdn.com/v4/letter/s/6de8d8/32.png) [@shahid646](https://forums.powershell.org/u/shahid646)\
**Post date:** [February 5, 2019, 8:16am UTC](https://forums.powershell.org/t/move-old-files-in-folders/12002/3 "2019-02-05T08:16:51Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![bdevogt](https://avatars.discourse-cdn.com/v4/letter/b/eada6e/32.png) [@bdevogt](https://forums.powershell.org/u/bdevogt)\
**Post date:** [February 5, 2019, 8:38am UTC](https://forums.powershell.org/t/move-old-files-in-folders/12002/4 "2019-02-05T08:38:33Z")

</div>

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]

---

<div class="post-metadata">

**Author:** ![shahid646](https://avatars.discourse-cdn.com/v4/letter/s/6de8d8/32.png) [@shahid646](https://forums.powershell.org/u/shahid646)\
**Post date:** [February 5, 2019, 9:11am UTC](https://forums.powershell.org/t/move-old-files-in-folders/12002/5 "2019-02-05T09:11:28Z")

</div>

Great Thanks 🙂

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

&nbsp;

---

<div class="post-metadata">

**Author:** ![postanote](https://avatars.discourse-cdn.com/v4/letter/p/ebca7d/32.png) [@postanote](https://forums.powershell.org/u/postanote)\
**Post date:** [February 5, 2019, 4:59pm UTC](https://forums.powershell.org/t/move-old-files-in-folders/12002/6 "2019-02-05T16:59:45Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![bdevogt](https://avatars.discourse-cdn.com/v4/letter/b/eada6e/32.png) [@bdevogt](https://forums.powershell.org/u/bdevogt)\
**Post date:** [February 6, 2019, 2:31am UTC](https://forums.powershell.org/t/move-old-files-in-folders/12002/7 "2019-02-06T02:31:03Z")

</div>

Havent tested the code,

&nbsp;

But something like

&nbsp;

[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]

&nbsp;

---

<div class="post-metadata">

**Author:** ![bdevogt](https://avatars.discourse-cdn.com/v4/letter/b/eada6e/32.png) [@bdevogt](https://forums.powershell.org/u/bdevogt)\
**Post date:** [February 6, 2019, 2:33am UTC](https://forums.powershell.org/t/move-old-files-in-folders/12002/8 "2019-02-06T02:33:46Z")

</div>

Something like this

&nbsp;

[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]

---

<div class="post-metadata">

**Author:** ![shahid646](https://avatars.discourse-cdn.com/v4/letter/s/6de8d8/32.png) [@shahid646](https://forums.powershell.org/u/shahid646)\
**Post date:** [February 6, 2019, 7:31am UTC](https://forums.powershell.org/t/move-old-files-in-folders/12002/9 "2019-02-06T07:31:49Z")

</div>

hi [postanote](https://powershell.org/profile/postanote/)

Thanks a lot for your suggestion 🙂

---

<div class="post-metadata">

**Author:** ![shahid646](https://avatars.discourse-cdn.com/v4/letter/s/6de8d8/32.png) [@shahid646](https://forums.powershell.org/u/shahid646)\
**Post date:** [February 6, 2019, 7:35am UTC](https://forums.powershell.org/t/move-old-files-in-folders/12002/10 "2019-02-06T07:35:41Z")

</div>

OMG Brat, You are my hero 🙂 🙂 🙂 🙂

Really thank you for your time.

Best Regards

Shahid

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:33pm UTC](https://forums.powershell.org/t/move-old-files-in-folders/12002/11 "2024-05-16T20:33:21Z")

</div>


