Solved

get date from filename and create folder

Posted on 2009-04-13
7
402 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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

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…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

762 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

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now