How to copy Folder and files with Sub folders to Other Folder in Ax 2012 X++

static void Johnkrish_CopyFolderAndFiles(Args _args)
{
str source = @”E:\JohnkrishFolderSource”;
str dest = @”E:\JohnkrishFolderDestination”;
str tempFolderPath;
System.String[] directories = System.IO.Directory::GetDirectories(source, “*.*”, System.IO.SearchOption::AllDirectories);
int folderCount = directories.get_Length();
int currentFolderCount;
void copyAllFiles(str sourceFolder, str destFolder)
{
str tempDestFileName;
System.String[] filePaths = System.IO.Directory::GetFiles(sourceFolder, “*.*”, System.IO.SearchOption::AllDirectories); //get listing of all files within the folder
System.IO.FileInfo sourceFile;
int fileCount = filepaths.get_Length();
int currentFileCount;

for(currentFileCount = 0; currentFileCount < fileCount ; ++currentFileCount)
{
sourceFile = new System.IO.FileInfo(filepaths.GetValue(currentFileCount));
tempDestFileName = strReplace(filepaths.GetValue(currentFileCount), sourceFolder, destFolder);
//Copy file
sourceFile.CopyTo(tempDestFileName, true);
}
}

//Create sub directories
for(currentFolderCount = 0; currentFolderCount < folderCount ; ++currentFolderCount)
{
tempFolderPath = strReplace(directories.GetValue(currentFolderCount), source, dest);
if(!System.IO.Directory::Exists(tempFolderPath))
{
System.IO.Directory::CreateDirectory(tempFolderPath);
}
}

//Copy all files include sub directory files
copyAllFiles(source, dest);
info(‘Success’);
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s