Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

get date from filename and create folder

Posted on 2009-04-13
7
Medium Priority
?
409 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
7 Comments
 
LVL 2

Accepted Solution

by:
gnovakhs2n earned 2000 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
New benefit for Premium Members - Upgrade now!

Ready to get started with anonymous questions today? It's easy! Learn more.

 

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

New benefit for Premium Members - Upgrade now!

Ready to get started with anonymous questions today? It's easy! Learn more.

Question has a verified solution.

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

If like me you are one who spends a lot of time working and scripting with cmd.exe, sometimes it is handy to be able to quickly view a calendar for a given month and year. This script will quickly do just that!  Save the code posted below to a .bat …
Introduction: Recently, I got a requirement to zip all files individually with batch file script in Windows OS. I don't know much about scripting, but I searched Google and found a lot of examples and websites to complete my task. Finally, I was ab…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…

670 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