Solved

get date from filename and create folder

Posted on 2009-04-13
7
405 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
The New “Normal” in Modern Enterprise Operations

DevOps for the modern enterprise offers many benefits — increased agility, productivity, and more, but digital transformation isn’t easy, especially if you’re not addressing the right issues. Register for the webinar to dive into the “new normal” for enterprise modern ops.

 

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

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 …
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…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

809 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