Script to Create Folder, Sub-folders and copy to destination

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
zagix
Posts: 68
Joined: 16 Oct 2013 23:19

Script to Create Folder, Sub-folders and copy to destination

#1 Post by zagix » 10 Nov 2013 07:15

Hello,
This is very difficult and tiring to do manually which i am facing daily, i request the experts to help me in sorting this issue.

I have 50 folders at one workstation, starting from 000 to 050. The work station path:
D:\Depository\BBC\[(Todays date-format ddmmyyyy)]\data\000
D:\Depository\BBC\[(Todays date-format ddmmyyyy)]\data\001
D:\Depository\BBC\[(Todays date-format ddmmyyyy)]\data\002
.......\050

File types in the folder are .pdf, .txt, .tiff & .jpeg in each folder.

I need to create a backup from workstation to Network in which i required to first create folder and sub folders and then copy files.

Stage 1.

Network Path: \\acer\c:\Repository\000\

Create folder on todays date (dd-mm-yyyy) and create 3 sub folders named: PDF,TEXT,IMAGES

It will be something like this:

\\acer\c:\Repository\000\10-11-2013\data\PDF
\\acer\c:\Repository\000\10-11-2013\data\TEXT
\\acer\c:\Repository\000\10-11-2013\data\IMAGES

\\acer\c:\Repository\001\10-11-2013\data\PDF
\\acer\c:\Repository\001\10-11-2013\data\TEXT
\\acer\c:\Repository\001\10-11-2013\data\IMAGES


Stage 2.
Copy the files from folder 000 of workstation to
\\acer\c:\Repository\000\10-11-2013\data\PDF (Copy all .pdf from folder \000\*.pdf)
\\acer\c:\Repository\000\10-11-2013\data\TEXT (Copy all .txt from folder \000\*.txt)
\\acer\c:\Repository\000\10-11-2013\data\IMAGES (which will be all .tiff & .jpeg from folder \000\*.tiff & *.jpeg)

\\acer\c:\Repository\001\10-11-2013\data\PDF (Copy all .pdf from folder \001\*.pdf)
\\acer\c:\Repository\001\10-11-2013\data\TEXT (Copy all .txt from folder \001\*.txt)
\\acer\c:\Repository\001\10-11-2013\data\IMAGES (which will be all .tiff & .jpeg from folder \001\*.tiff & *.jpeg)

Do this for all folders starting from 000 to 050.
Experts please help.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Script to Create Folder, Sub-folders and copy to destina

#2 Post by foxidrive » 10 Nov 2013 07:46

I think this should work - it is not tested.

It assumes that the date in source and target directories is in dd-mm-yyyy format.

If the source date format is really ddmmyyyy then replace this portion in the for command
"D:\Depository\BBC\%datestamp%\data\*"
with this
"D:\Depository\BBC\%dd%%mm%%yyyy%\data\*"

Code: Select all

@echo off
for /f "tokens=2 delims==" %%a in ('wmic OS Get localdatetime /value') do set "dt=%%a"
set "YY=%dt:~2,2%" & set "YYYY=%dt:~0,4%" & set "MM=%dt:~4,2%" & set "DD=%dt:~6,2%"
set "HH=%dt:~8,2%" & set "Min=%dt:~10,2%" & set "Sec=%dt:~12,2%"

set "datestamp=%DD%-%MM%-%YYYY%"

for /d %%a in ("D:\Depository\BBC\%datestamp%\data\*") do (
      xcopy "%%a\*.pdf"   "\\acer\c\Repository\%datestamp%\%%~nxa\PDF\"
      xcopy "%%a\*.txt"   "\\acer\c\Repository\%datestamp%\%%~nxa\TEXT\"
      xcopy "%%a\*.tiff"  "\\acer\c\Repository\%datestamp%\%%~nxa\IMAGES\"
      xcopy "%%a\*.jpg"   "\\acer\c\Repository\%datestamp%\%%~nxa\IMAGES\"
)
pause


Note that I changed c: to c in the \\acer\ path.

zagix
Posts: 68
Joined: 16 Oct 2013 23:19

Re: Script to Create Folder, Sub-folders and copy to destina

#3 Post by zagix » 10 Nov 2013 13:01

Hi,

Foxidrive its Brilliant.

One thing which is skipped in target directory to make directory in dd-mm-yyyy format.
That target directory does not exists we will create and then copy source files.

One more addition which is not in my original post. please excuse me.
In source directory i have one sub-folder named: Index which is to be copied in all folders of target.
i.e. D:\Depository\BBC\11112013\data\Index this Index folder and its contents to be copied in all folders of target directories.

Thanks in advance.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Script to Create Folder, Sub-folders and copy to destina

#4 Post by foxidrive » 10 Nov 2013 16:00

There should be a date on the folders in the target directory - or is there an error message on the console when it starts? It may need admin permission.

Replace this line

set "datestamp=%DD%-%MM%-%YYYY%"

with these and see if there is an error message.

set "datestamp=%DD%-%MM%-%YYYY%"
echo "%datestamp%"
pause


If that doesn't show an error message, what folders do you see created?
I did noticed a %MM% on the end of the date stamp, which I removed, so it would have been creating dd-mm-yyyymm folders. I edited the code above.

Regarding "D:\Depository\BBC\11112013\data\Index" where does that folder tree have to be copied? Into 000 and 001 and 002 etc?

zagix
Posts: 68
Joined: 16 Oct 2013 23:19

Re: Script to Create Folder, Sub-folders and copy to destina

#5 Post by zagix » 10 Nov 2013 18:15

Hi,

Thanks for response,
I did noticed a %MM% on the end of the date stamp, which I removed, so it would have been creating dd-mm-yyyymm folders. I edited the code above.


That has now solved the issue.

Regarding "D:\Depository\BBC\11112013\data\Index" where does that folder tree have to be copied? Into 000 and 001 and 002 etc?


Folder Index to be copied to all the folders in the target directories, from 000 to the last folder 000,001,002,003,004 .....

Thankyou very much for your help.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Script to Create Folder, Sub-folders and copy to destina

#6 Post by foxidrive » 11 Nov 2013 00:56

This modification should also copy the index tree into each folder.

Code: Select all

@echo off
for /f "tokens=2 delims==" %%a in ('wmic OS Get localdatetime /value') do set "dt=%%a"
set "YY=%dt:~2,2%" & set "YYYY=%dt:~0,4%" & set "MM=%dt:~4,2%" & set "DD=%dt:~6,2%"
set "HH=%dt:~8,2%" & set "Min=%dt:~10,2%" & set "Sec=%dt:~12,2%"

set "datestamp=%DD%-%MM%-%YYYY%"

for /d %%a in ("D:\Depository\BBC\%datestamp%\data\*") do (
   if /i not "%%~nxa"=="index" (
      xcopy "%%a\*.pdf"   "\\acer\c\Repository\%datestamp%\%%~nxa\PDF\"
      xcopy "%%a\*.txt"   "\\acer\c\Repository\%datestamp%\%%~nxa\TEXT\"
      xcopy "%%a\*.tiff"  "\\acer\c\Repository\%datestamp%\%%~nxa\IMAGES\"
      xcopy "%%a\*.jpg"   "\\acer\c\Repository\%datestamp%\%%~nxa\IMAGES\"
      xcopy "%%~dpa\index\*.*" "\\acer\c\Repository\%datestamp%\%%~nxa\index\" /s/h/e/k/f/c
   )
)
pause

zagix
Posts: 68
Joined: 16 Oct 2013 23:19

Re: Script to Create Folder, Sub-folders and copy to destina

#7 Post by zagix » 11 Nov 2013 03:32

Hi,

Foxidrive its perfect.

Thanks a lot.

Post Reply