Need Windows script to create folders named with file dates and move files to the matching folder

0

I need a CMD or Powershell script or batch file to

  • step through a list of files
  • make a folder named with the date of a file if it doesn't already exist, then
  • move that file into its matching folder.

I have a list of about 400 files like this:

enter image description here

Using this as an example, the script should create a folder including the top two files called 2018-05-16, a folder with the 3 next files called 2018-05-17 etc. Preferably in the same folder as the files are located now.

I'm on Windows 8 if that makes a difference.

Gerlof Leuhof

Posted 2018-09-06T21:34:31.110

Reputation: 107

Answers

1

Sample directory before

> gci

    Verzeichnis: A:\

Mode                LastWriteTime         Length Name
----                -------------         ------ ----
-a----       2018-05-16     16:02             14 GOPR0150.MP4
-a----       2018-05-16     16:10             14 GOPR0151.MP4
-a----       2018-05-17     01:25             14 GOPR0152.MP4
-a----       2018-05-17     01:32             14 GOPR0153.MP4
-a----       2018-05-17     01:32             14 GOPR0154.MP4

running this small PowerShell script

## Q:\Test\2018\09\06\SU_1355955.ps
ForEach($File in (Get-ChildItem '.\GOPR*.mp4')){
    $DestFolder = Join-Path $File.DirectoryName $File.LastWriteTime.ToString('yyyy-MM-dd')
    if (!(Test-Path $DestFolder)){MD $DestFolder|Out-Null}
    $File | Move-Item -Destination $DestFolder
}

> gci -recurse -file

    Verzeichnis: A:\2018-05-16

Mode                LastWriteTime         Length Name
----                -------------         ------ ----
-a----       2018-05-16     16:02             14 GOPR0150.MP4
-a----       2018-05-16     16:10             14 GOPR0151.MP4

    Verzeichnis: A:\2018-05-17

Mode                LastWriteTime         Length Name
----                -------------         ------ ----
-a----       2018-05-17     01:25             14 GOPR0152.MP4
-a----       2018-05-17     01:32             14 GOPR0153.MP4
-a----       2018-05-17     01:32             14 GOPR0154.MP4

Edit: depending on your local date format you may have to
change to .ToString('yyyy\-MM\-dd')

LotPings

Posted 2018-09-06T21:34:31.110

Reputation: 6 150