Solved

batch file for recursiv copy of files to target folder

Posted on 2008-10-06
3
474 Views
Last Modified: 2012-05-05
Hello Experts
i´m looking for a simple batch script to copy files from a source folder (include subfolders) into a target folder with ''dynamic'' subfolders.

the source file structure look like
\\myserver\myfolder

this folder contains several subfolders for example

/school/klasse1/teacher/....
/school/klasse2/teacher/....
/school/teacher/....

In these folders are many *.rtf files ( ~ 100000 files)

Now the heavy part. I have to copy all the rtf files to a target folder with several subfolders . For each folder the maximum count of files should not more than 1000 files.
For example:

\\myotherserver\target\klasse1\ not more than 1000 files
\\myotherserver\target\klasse2\ not more than 1000 files
...

is there any way to realize this.


0
Comment
Question by:bibabutze
3 Comments
 
LVL 21

Expert Comment

by:AmazingTech
ID: 22657284
So you don't care which folder it comes from and you don't care what order?

Any 1000 rtf files in klasse1 then another 1000 rtf files in klasse2

So with 100000 rtf files the last folder should be klasse100?
0
 

Author Comment

by:bibabutze
ID: 22657622
yes.
is there a solution for it
0
 
LVL 38

Accepted Solution

by:
Shift-3 earned 500 total points
ID: 22659755
Paste the script below into a text file with a .cmd extension.  Customize the value of the source variable with the root folder to copy from.  Customize the value of the dest variable with the directory to copy to (without the trailing number).  Running the script will copy the .rtf files to numbered folders.

Note that files with the same names will be overwritten if they get copied to the same destination folder.  If this is a problem please specify how you would like the script to handle it.


@echo off
setlocal enabledelayedexpansion
 
set source=\\myserver\myfolder\*.rtf
set dest=\\myotherserver\target\klasse
 
set filecount=0
set foldercount=1
set maxfiles=1000
 
for /F "tokens=*" %%G in ('dir "%source%" /A:-D /B /S') do (
 set /A filecount+=1
 xcopy "%%G" "%dest%!foldercount!\" /C /H /R /Y
 
 if [!filecount!]==[%maxfiles%] (
  set /A foldercount+=1
  set filecount=0
 )
)

Open in new window

0

Featured Post

Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

Question has a verified solution.

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

The following is a collection of cases for strange behaviour when using advanced techniques in DOS batch files. You should have some basic experience in batch "programming", as I'm assuming some knowledge and not further explain the basics. For some…
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…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

856 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