Solved Create a folder and copy files using windows batch

August 6, 2012 at 19:20:14
Specs: Windows 7
Hi Gurus,

I've been searching on the Internet, but could not really find what I'm looking for.

Would need some help in performing the following tasks using windows batch script.

For the current month and year, example: August 2012, need to perform the followings:
1) Check and create a directory called: Aug2012 if it does not exist.
2) Check all files with extension *.AUD on D:\AuditData (source) and copy them to target directory. Target directory name is based on the respective month and year, for example: Aug2012. Overwrite the files if exist.

Some samples:
20120701.AUD
20120702.AUD
20120703.AUD
20120704.AUD
20120705.AUD

20120801.AUD
20120802.AUD
20120803.AUD
20120814.AUD
20120815.AUD

For all files with timestamp July 2012, copy them to Jul2012 directory.

For all files with timestamp August 2012, copy them to Aug2012 directory.

Please help to check and advise.

Thank you for your help.

- Jack


See More: Create a folder and copy files using windows batch

Report •


#1
August 7, 2012 at 00:04:07
✔ Best Answer
This copies them into dirs named YYYYMM.
Your homework assignment is to morph it to do mmmYYYY.

:: ===== script starts here ===============
::
:: jack.bat 2012-08-07 13:13:22.18
@echo off & setLocal enableDELAYedeXpansioN

for /f "tokens=* delims= " %%a in ('dir/b/s D:\AuditData\*.AUD') do (
set F=%%~Na
set F=!f:~0,6!
if not exist !F! md !F!
copy %%a !F! > nul
)
::====== script ends here =================


====================================
Life is too important to be taken seriously.

M2


Report •

#2
August 7, 2012 at 02:29:27
Hi Mechanix2Go,

Thanks a lot for your help.

It works like magic. Great :)

Could you advise on how to create the dir (YYYYMM) into other drive, for example: E drive?

Thanks


- Jack


Report •

#3
August 7, 2012 at 02:34:49
Hi Mechanix2Go,

Please ignore my previous posting as I've managed to fix it.

Thanks again for you help.


- Jack


Report •

Related Solutions

#4
August 7, 2012 at 03:49:09
Hi Mechanix2Go,

Just wondering how to handle if either my source path or target path has some spaces.

For example:

Source
D:\Audit Data\Computer ABC\

Target
E:\Backup\Audit Data (2012)\Computer ABC\

Thanks


- Jack


Report •

#5
August 7, 2012 at 04:04:52
In general, "bla bla"

dir/b/s "D:\Audit Data\Computer ABC\*.AUD"


====================================
Life is too important to be taken seriously.

M2


Report •

#6
August 7, 2012 at 17:39:56
Hi Mechanix2Go,

Thanks for your advise.

The source path working OK, however, I'm having some problem with the target path which is having some spaces too.

Had tried using this method, but it's just crating a blank folder, not copying.

set F="E:\Backup\Audit Data (2012)\Computer ABC\"!f:~0,6!

Could you help a little bit?

Thanks.


- Jack


Report •


Ask Question