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.
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
)
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With