setlocal enabledelayedexpansion @echo off setlocal SET "p=C:\Uploads\InboxUploads" FORFILES /P "%p%" /C "CMD /C md 0x22%p%\@fdate0x22" FORFILES /P "%p%" /M *.* /C "CMD /C move @file 0x22%p%0x22\@fdate" SET d=202108 FORFILES /P "%p%" /M *%d%*.* /C "CMD /C move @file 0x22%p%0x22\@fdate" -------------------------------------------------- for /f "tokens=1,2,3 delims=/" %a in (' forfiles /P "%p%" /m *.* /c "cmd /c echo @fdate" ') do ( md "%p%\%c-%b-%a" ) robocopy empty empty /S /MOVE $n = 0 $dest = Join-Path 'F:\STC Systems\SchoolMobileAppPortal2020\Uploads\AssignmentUploads\StudentUploads' $firstDate = '2020/08/01' $lastDate = '2020/11/10' While ( ( Get-Date $firstDate ).AddDays( $n ) -le ( Get-Date $lastDate ) ) { $path = Join-Path $dest ( Get-Date $firstDate ).AddDays( $n ).ToString( 'yyyy-MM-dd' ) # REMOVE the "-WhatIf" in the line below to allow the directories to be created New-Item -Path $path -ItemType 'Directory' $n++ } C:\Users\user25> $src = "D:\test" $fileList = Get-ChildItem -Path $src -File foreach ($file in $fileList){ $r = $src+'\'+$file.Basename.substring(0,4)+'-'+$file.Basename.substring(4,2)+'-'+$file.Basename.substring(6,2) if (Test-Path -Path (Join-Path -Path $r -ChildPath $fileName)) { } else { mkdir -path $r } Move-Item -Path $($file.FullName) -Destination $r }