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.

0

2 Answers 2

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')

0

The script does not recognise the DestFolder, not sure what am I doing wrong. I have also changed the date format. running in command prompt I get the following

E:\Travel Videos\GOPRO\20221015>ForEach($File in (Get-ChildItem '.\GOPR*.mp4')){ 'ForEach' is not recognized as an internal or external command, operable program or batch file.

E:\Travel Videos\GOPRO\20221015> $DestFolder = Join-Path $File.DirectoryName $File.LastWriteTime.ToString('dd\MM\yyyy') '$DestFolder' is not recognized as an internal or external command, operable program or batch file.

E:\Travel Videos\GOPRO\20221015> if (!(Test-Path $DestFolder)){MD $DestFolder|Out-Null} $DestFolder)){MD was unexpected at this time.

E:\Travel Videos\GOPRO\20221015> $File | Move-Item -Destination $DestFolder '$File' is not recognized as an internal or external command, operable program or batch file.

1
  • 1
    As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.
    – Community Bot
    Commented Feb 24, 2023 at 4:38

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .