Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Move files in folders dependent on the file date

I'm trying to sort some thousand files on a Windows server into multiple folders. File names are Extract_YYYYMMDDHHMISS.dat where YYYY is the year, MM is the month and DD is the date. I want to move these files to a folder hierarchy that I have defined as follows:

Archive\2013\01\01
Archive\2013\01\02
Archive\2013\01\03
...
Archive\2013\02\01

and so on.

@echo off
setlocal enabledelayedexpansion
for /f %%f in ('dir Extract_* /b') do (
echo %%f
echo %%~15,8f
)
endlocal

I'm trying to use a for loop and string formatting to get the YYYYMMDD part into a variable, and then further split it, but I'm stuck for now.

Any help appreciated.

like image 630
Saad Waseem Avatar asked Nov 16 '25 10:11

Saad Waseem


2 Answers

try this and remove the echo if the output is OK:

@echo off &setlocal
for %%i in (Extract_*.dat) do (
    set "fname=%%~i"
    setlocal enabledelayedexpansion
    set "name=!fname:*_=!"
    set "year=!name:~0,4!"
    set "month=!name:~4,2!"
    set "day=!name:~6,2!"
    echo move "!fname!" "Archive\!year!\!month!\!day!"
    endlocal
)
like image 69
Endoro Avatar answered Nov 19 '25 00:11

Endoro


You can use the "YYYY"=="20NN" pattern to extract the core file name:

@echo off &setlocal
set name=Extract_Full_Data_Over_Time_20130101121314.dat
set core=%name:*_2=2%
echo %core%
20130101121314.dat

Obviously you shouldn't do this with file dates before 2000.

like image 35
captcha Avatar answered Nov 18 '25 23:11

captcha



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!