Solved

get date from filename and create folder

Posted on 2009-04-13
7
406 Views
Last Modified: 2012-05-06
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?
0
Comment
Question by:mightyestme
  • 3
  • 3
7 Comments
 
LVL 2

Accepted Solution

by:
gnovakhs2n earned 500 total points
ID: 24136239
hi, try this batch file
@echo off
setlocal enabledelayedexpansion
 
 
for /F "tokens=* usebackq" %%G in (`dir /b`) do (
for /F "tokens=1 delims=." %%X in ("%%~xG") do (
if not exist %%X md %%X 
if not %ERRORLEVEL% == 0 goto xa
copy *.%%X %%X
del *.%%X
:xa
echo %%X
)
)

Open in new window

0
 

Author Comment

by:mightyestme
ID: 24136395
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?
0
 

Author Comment

by:mightyestme
ID: 24136449
figured it out. thanks for your help on this gnovakhs2n. I'm granting all points to you.

Cheers.
0
MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

 

Author Closing Comment

by:mightyestme
ID: 31569828
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!
0
 
LVL 16

Expert Comment

by:t0t0
ID: 24136561
@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!"
         )
      )
   )
)
0
 
LVL 16

Expert Comment

by:t0t0
ID: 24136567
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!"
   )
)
0
 
LVL 16

Expert Comment

by:t0t0
ID: 24136572
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!"
   )
)
0

Featured Post

Guide to Performance: Optimization & Monitoring

Nowadays, monitoring is a mixture of tools, systems, and codes—making it a very complex process. And with this complexity, comes variables for failure. Get DZone’s new Guide to Performance to learn how to proactively find these variables and solve them before a disruption occurs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

You may have already been in the need to update a whole folder stucture using a script. Robocopy does it well and even provides a list of non-updated files in a log (if asked to). Generally those files that were locked by a user or a process by the …
This article was inspired by a question here at Experts Exchange (http://www.experts-exchange.com/Software/Photos_Graphics/Images_and_Photos/Q_28629170.html). The requirements stated in that question are (1) reduce the file size of a large number of…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

680 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question