Copy Files from Multiple Sources to Multiple Destination at the same time - c#

I have written a code to copy all files in a directory using c# to a particluar location.I am calling that exe using a .bat file for this process.
The problem is there are n number of sources and n number of destinations and this batch job at a time only copy files from one location to another one.
Is there any way to copy files through a .bat file or using script or even using .net technology from multiple sources to multiple destination?
Copying file from one location to another is easy but i want to copy files from multiple locations at the same time.
Please help me for making this possible.

Create a class called CopyThread which inherits from the Thread class. Add source and destination member to them along with setters. When you create a CopyThread object make sure you set its source and destination values. When you start the thread, it should copy all files from source to destination.

You can have one solution for that,use "command line parameters" to take source and destination path as input to your program and from a batch file call your program multiple times with diffrent parameters. If your sources and destinations are dynamic you have to write small batch program to take inputs for source and destination and call your program with the given inputs as parameters.

Related

copying files to another location from text file

We're working on moving files from one server to another. The last time we did this, we used vb Script. We move around a million files (took several days).
We're looking to speed up the process, as this looks like it's going to be a recurring process with possibly more files to be moved going forward.
Parameters:
Files to move dictated by two text files with multiple lines
Multiple files will be associated with each line in .txt files
Will need to search for files with parameters from text files
Copy the found files over to the new drive
Here's the idea:
Store all parameters into an array
Loop through the array and find the met parameters using something like this:
System.IO.Directory.GetFiles("PATH").Where(s => s.Contains("three"));
If c# containerizes that search, then copy those files using RoboCopy so we can multithread
Question:
Can this be done?
If not is there a way to multithread the code in c# to move through this faster?
My experience with c# is what I've learned in the last few days. If you need some of the code I've created already, let me know.
I would suggest reversing the order to minimize the GetFiles:
Store all parameters into an array
Store all the files on the path:
var files = Directory.GetFiles("PATH")
Loop through the array and find the met parameters:
var ans = parameters.SelectMany(parameter => files.Where(s => s.Contains(parameter)))
.Distinct()
.ToList();
Then copy those files

Add Multiple Folders / Directories to a ZipFile

I am writing a backup program that requires predefined multiple folder(s) & single file(s) to be added to a single zip archive. I have had no issues adding a single folder using -ZipFile.CreateFromDirectory(string, string, c..level, bool(false))
However i am having a hard time adding multiple folders as there does not seem to be a way to update an archive or target two folders using the CreateFromDirectory method.
- Would be nice if there was an UpdateFromDirectory mehod!
I have been trying to stay away from third party libraries for no reason really, however as far as i have found none deal with multiple unrecursive folders.
I have tried just about everything other than writing my own code to recurse & add individually which i don't really want to do.
The program has several inputs that defines the folders / files to be zipped and depending on whether they are not null should add them to a single zip file regardless of whether they are a folder or file.
I guess my question is whether this is possible at all using the boxed libraries without custom recursing or even with a third party library without heavy mods... Not sure if i have made my question clear, sure you will all let me know if i have not.
From what I can tell using the ZipFile class you can only create and read. if you want to update you would need to create the whole zip again. [Source: ZipFile methods]
to target more than one folder you could arrange all the files and folders into one folder then zip the entire source without including the source folder. In most cases moving this files/folders isn't possible so I'd recommend looking into Symlinks within windows. I'd redirect to you [Issue with creating symbolic link to directory
You can create a "myFolder" folder and put in it all the folders you want found in the zipped folder. Then do ZipFile.CreateFromDirectory("myFolder", "name of zip file to create", CompressionLevel.Fastest, false, Encoding.UTF8). Overriding this IncludeBaseDirectory method to false allows this to be done.

Put file connection to variable on SSIS

i want to do extract zip files on SSIS 2008, using execute process task. I've got confused because Arguments just can do 1 file process and 1 working directory. But i want to an Arguments and a working directory changes dynamic according to SQL table which is give the value for path on Arguments and Working directory. What should i do?
If you wants to pass those values dynamically, then pass the values through varaibles
The operations provided by the File System task target individual file system objects. To
use it against multiple objects, you should use the Foreach Loop Container.
Just google for examples, you should find several

Combining .log files in C# by timestamp

My question is actually related to this post.
VB script + read files (only files with "log" name) and copy content files into one file.txt
I want to do exactly what he has done (combine *.log files that I have a user browse for) however I need them to be inserted into the new log file time wise. For example:
1.log (12:15:66)
2.log (10:09:33)
3.log (15:11:10)
I need the out put to be in the final.log file but in the order (2.log, 1.log, 3.log) because thats the order timewise they were created. I also will have different numbers of log files so it needs to either combine all in a directory or ask for each file until I don't specify anymore. I am going to be using C# also not VB like in the example.
Help is much appreciated!
Once the user has selected all of the logs that he wants to include, you can get the FileInfo for each file. Store those in a list and sort by timestamp. Then use a simple loop to copy each one to the output file.

Make sure file got copied successfully using c#

I am creating an application to back up files from a source directory into a destination directory. I store the files information from the source and destination folders in separate lists then I compare the lists based on their size, date modified etc to see which files need to be copied.
Anyways the point is that I end up with a list of the files that need to be copied and I will like to know how much time is every file taking therefore I have tried the following techniques:
Technique 1
Technique 2
Thechnique 3 : the regular File.Copy("source....","Destination")
The first two techniques are great because I can see the progress. The problem is that when I copy some files with those techniques, the new file sometimes has different dates. I will like both files to have the same modified date and also the same creation date. Moreover if for whatever reason my program crashes the file that is being copied will be corrupted because I have tried copying a large file ( a file that takes about a minute to get copied in windows) if I exit my program meanwhile the file is being copied the file that is being copied sometimes has the same attributes and the same size so I want to make sure I don't have corrupted files in case my program crashes.
Maybe I should use aether techniques 1 or 2 and then at the end copy the attributes from the source file and assign those to the destination file. I don't know how to do that though.
FileInfo has members CreationTime and LastWriteTime that are settable - so you could settle for your preferring techniques and set the dates afterwards if that helps.
Have you considered just writing a shell script that calls robocopy? Any time I've had to run backup tasks like this, I just write a script -- robocopy already does the heavy lifting for me, so there's often no need to create a bespoke application.
A solution that I have but its long:
I know I can copy the file from the source and then name the file in the destination something else like "fileHasNotBeenCopiedYet" with attributes of hidden then when my program finishes copying the file change the name to the source name and copy the attributes and then latter I know that if a file with that name ("fileHasNotBeenCopiedYet") exists that means that the file is corrupted.

Categories