OK I am using the following code in a form to move files. The codebank submission helped get that working at least. Thanks to jmcilhinney. However I think that I want a separate class to house the file moving process. Going from what I have so far how can I keep updating the label from this new class? Seems like it should not be hard to do but I am not connecting the dots for whatever reason.
Thanks.
Code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
namespace VisualScrapUtil
{
public partial class frmUtil : Form
{
public frmUtil()
{
InitializeComponent();
bgWorker.WorkerReportsProgress = true;
bgWorker.WorkerSupportsCancellation = true;
}
private void btnPath_Click(object sender, EventArgs e)
{
FolderBrowserDialog fbd = new FolderBrowserDialog();
fbd.Description = "Select Images Folder";
if (fbd.ShowDialog() == DialogResult.OK)
{
txtFolder.Text = fbd.SelectedPath;
txtDestination.Text = txtFolder.Text;
}
}
private void button2_Click(object sender, EventArgs e)
{
if (txtFolder.TextLength != 0 || txtDestination.TextLength != 0)
{
if (bgWorker.IsBusy != true)
{
label1.Text = DateTime.Now.ToString();
bgWorker.RunWorkerAsync();
}
}
else
{
label2.Text = "PICK FILES";
}
}
private void btnDest_Click(object sender, EventArgs e)
{
FolderBrowserDialog fbd = new FolderBrowserDialog();
fbd.Description = "Select Images Destination";
if (fbd.ShowDialog() == DialogResult.OK)
{
txtDestination.Text = fbd.SelectedPath;
}
}
private void btnCancelMove_Click(object sender, EventArgs e)
{
bgWorker.CancelAsync();
}
private void bgWorker_DoWork(object sender, DoWorkEventArgs e)
{
//BackgroundWorker worker = new BackgroundWorker();
//if (worker.CancellationPending == true)
if (bgWorker.CancellationPending == true)
{
e.Cancel = true;
}
else
{
ProcessImages(txtFolder.Text, txtDestination.Text);
}
}
private void bgWorker_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
label2.Text = (e.ProgressPercentage.ToString() + "%");
}
private void bgWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
if (e.Cancelled == true)
{
label2.Text = "CANCELLED";
}
else if (e.Error !=null)
{
label2.Text = "Error: " + e.Error.Message;
}
else
{
label4.Text = DateTime.Now.ToString();
label2.Text= "FINISHED";
}
}
public int ProcessImages(String SourceFolder, String DestinationFile)
{
//leave the fingerprint records alone for now
//only process the .jpg files
try
{
string[] fileImages = Directory.GetFiles(SourceFolder);
Double dblNumFiles = fileImages.Length;
Int32 intPercentDone = 0;
Double dblRecCnt = 0;
foreach (string fileImage in fileImages)
{
dblRecCnt = ++dblRecCnt;
intPercentDone = (int)(((float)dblRecCnt / (float)dblNumFiles) * 100);
bgWorker.ReportProgress(intPercentDone);
if (fileImage.Contains(".jpg"))
{
string[] paths = fileImage.Split('\\');
string newImage = paths[paths.Length - 1];
if (newImage.Substring(0, 2) != "FP")
{
string newFolder = newImage.Substring(2, 8);
string destFilename = DestinationFile + "\\" + newFolder;
if (!Directory.Exists(destFilename))
{
Directory.CreateDirectory(destFilename);
}
destFilename = destFilename + "\\" + newImage;
File.Move(fileImage, destFilename);
}
}
}
}
catch (Exception ex)
{
//do something??
string str = ex.ToString();
}
return 0;
}
}
}