473,383 Members | 1,748 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

Problem in Recursive copy folder

Hi

I have a windows service that backups a folder onto another location on the
same computer. The service is written in C#. The size of the original folder
is large i.e. over 8 GB. Also the folder structure is very deep, i.e lot of
recursion. What happens is that the backup process terminates after sometime
taking backup of around 2 GB. If I debug the same code I dont get any error,
instead the whole folder is copied successfully. Is there any chance that
the function terminates because of recursion being very high or may be
because of the time involved in it, the Directory and File objects refering
to files or folders start destroying because of automatic garbage
collection.

Below is the the code that I'm using for recursively copying folder.

Consider IsPauseSignalled, DoPause, IsFileInFilter, IsFolderInFilter and
FileDiffer functions tested for all errors.
----------------------------------------------------------------------------------------------------------------------------------

private bool CopyFolder(string szSrcFolder, string szTargetFolder, string
szFileFilter, string szFolderFilter)
{
bool bRet = false;

if(IsPauseSignalled())
DoPause();

DirectoryInfo dirInfo = new DirectoryInfo(szSrcFolder);
if(dirInfo != null && dirInfo.Exists)
{
FileInfo[] filesInfo = dirInfo.GetFiles();
foreach(FileInfo info in filesInfo)
{
if( !IsFileInFilter(info.Name, szFileFilter))
{
string szSrcPath = szSrcFolder + "\\" + info.Name;
string szTrgPath = szTargetFolder + "\\" + info.Name;
try
{
if(FilesDiffer(info, szTrgPath))
{
File.Copy(szSrcPath, szTrgPath, true);
}
}
catch(Exception ex)
{
String szError = ex.Message;
}
}
}

try
{
//Recursively copy directories
DirectoryInfo[] dirsInfo = dirInfo.GetDirectories();
foreach (DirectoryInfo dInfo in dirsInfo)
{
if (!IsFolderInFilter(dInfo.Name, szFolderFilter))
{
string szSrcPath = szSrcFolder + "\\" +
dInfo.Name;
string szTrgPath = szTargetFolder + "\\" +
dInfo.Name;
Directory.CreateDirectory(szTrgPath);
CopyFolder(szSrcPath, szTrgPath, szFileFilter,
szFolderFilter);
}
}
}
catch (Exception ex)
{

}
bRet = true;
}
return bRet;
}
Feb 7 '08 #1
3 1823
Not code, but have you tried "robocopy" with the /E switch? It is part
of the platform SDK (IIRC) and can be downloaded from Microsoft.

Frankly it is going to be more efficient and more reliable than
anything you or I can create, especially in C#.

Marc
Feb 7 '08 #2
usman wrote:
I have a windows service that backups a folder onto another location
on the same computer. The service is written in C#. The size of the
original folder is large i.e. over 8 GB. Also the folder structure is
very deep, i.e lot of recursion. What happens is that the backup
process terminates after sometime taking backup of around 2 GB. If I
debug the same code I dont get any error, instead the whole folder is
copied successfully. Is there any chance that the function terminates
because of recursion being very high or may be because of the time
involved in it, the Directory and File objects refering to files or
folders start destroying because of automatic garbage collection.
If robocopy isn't an option, then perhaps you could try a thread.sleep every
now and again to let the file system catch up.

Is anything written to the event logs pertaining to the failure reason?

Andrew
Feb 7 '08 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: gusmeister | last post by:
Is there a Perl mod that has a recursive file copy function (similar to `cp -r` in Unix or `xcopy` in Windows)? File::Path does have a recursive file deletion function (rmtree) but no recursive...
2
by: LoserInYourFaceEngineer | last post by:
Hello All: I'm having trouble with a recursive function. The function is supposed to identify nested folders in a hierarchical folder structure. The function "searchForFolders()" is...
2
by: Mark | last post by:
I got this code from the Internet. Demo on the site works fine but downloaded version is not. I am a very new to .NET, I cannot figure this out, please help. Here is the code: ASPX code: <%@...
1
by: Dan | last post by:
I have an application that I want to use for copying files. My goal is to copy a files, if a file is in use or not accessible because of security reasons I want to make note of that file then...
7
by: =?Utf-8?B?RGF2aWQgQ29sbGl2ZXI=?= | last post by:
Hi all, using C#, .NET 1.1, ASP.NET I have created a recursive??? function. It looks a little like... MyContext.Current.Folder.Parent.Parent.Parent.Parent.Name As you can see, Parent is...
12
by: NOO Recursion | last post by:
Hi everyone! I am trying to write a program that will search a 12x12 for a thing called a "blob". A blob in the grid is made up of asterisks. A blob contains at least one asterisk. If an...
7
by: pallav | last post by:
I'm having some trouble with my copy constructor. I've tried using gdb to find the bug, but it seg faults in the destructor. I'm not able to see what I'm doing wrong. Since I'm using pointers, I...
3
by: webandwe | last post by:
Hi, I got form that let you state a folder name, on sumbit thw script creates the folder with the name you stated, the problem is I want it to upload the php file "view.php" when the folder is...
0
by: mcc99 | last post by:
Amazing, I surfed around to find a simple utility that could list files recursively from a given top folder down through it, writing out the path and filename in simple \path\file_name form, without...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.