Link to home
Start Free TrialLog in
Avatar of mightyestme
mightyestme

asked on

get date from filename and create folder

I am trying to create an archive script which moves files to a new folder everyday. All files end with a date portion. A sample filename could be
A.B.C.YYYYMMDD
I want to create a folder named YYYYMMDD before moving all files into this folder. How can I do achieve this?
ASKER CERTIFIED SOLUTION
Avatar of gnovakhs2n
gnovakhs2n
Flag of Austria image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of mightyestme
mightyestme

ASKER

just a few more changes in the requirement. i have a drive where i copy all these files to. The directory structure of the backup folder is
G:\DailyFiles\Backups --> target folder where I want to create folders by date
D:\DailyRuns\Processes --> source folder where all the files are

I want to create these date folders under the G:\DailyFiles\Backups folder. And ideally, I would like to parameterize both, the source folder as well as the target folder. How do I achieve that using the script above?
figured it out. thanks for your help on this gnovakhs2n. I'm granting all points to you.

Cheers.
Thanks for your help. This answer saved me a full day's worth of research and testing effort, thereby saving my client a lot of money too!
@echo off
setlocal enabledelayedexpansion

set Source=D:\DailyRuns\Processes
set Destination=G:\DailyFiles\Backups

for /f "tokens=*" %%a in ('dir /a-d /b "%Source%"') do (
   set Folder=%%~xa
   set Folder=!Folder:~1!
   if not "!Folder:~7,1!"=="" (
      if "!Folder:~8!"=="" (
            if not exist "%Destination%\!Folder!\" md "%Destination%\!Folder!"
            move "%Source%\%%a" "%Destination%\!Folder!"
         )
      )
   )
)
or if don't need to validate the file extension name then the following will do:

@echo off
setlocal enabledelayedexpansion

set Source=D:\DailyRuns\Processes
set Destination=G:\DailyFiles\Backups

for /f "tokens=*" %%a in ('dir /a-d /b "%Source%"') do (
   set Folder=%%~xa
   set Folder=!Folder:~1!
      if not exist "%Destination%\!Folder!\" md "%Destination%\!Folder!"
      move "%Source%\%%a" "%Destination%\!Folder!"
   )
)
Please try, before awarding points:


@echo off
setlocal enabledelayedexpansion

set Source=D:\DailyRuns\Processes
set Destination=G:\DailyFiles\Backups

for /f "tokens=*" %%a in ('dir /a-d /b "%Source%\"') do (
   set Folder=%%~xa
   set Folder=!Folder:~1!
   if not exist "%Destination%\!Folder!\" md "%Destination%\!Folder!"
      move "%Source%\%%a" "%Destination%\!Folder!"
   )
)