Comparing files in folder and moving them if size smaller - c#

im new here, have been trying to compere one file to another and if previous file in folder is bigger then move the file. I was trying to find solution for vba, batch, c# but had no luck yet. there are many files in folder sorted by date and we need to compare one file to next and so on. If i could get ANY help on that i would greatly appreciate that!

For c#:
Use the static methods on the Directory class to get a list of the files in a folder.
Use the FileInfo class to get information about the files (IE: file size)
Use File.Move() to relocate the files if they match your criteria.
Have a look over the IOException and UnauthorizedAccessException documentation to see all the bad things that might happen when your program runs.

Related

Tracking the location of a file in C# windows?

I need to track the location of some files that i know the initial full paths. Situtation as follows :
I have a file in path C:\Temp\first.txt
in some time this first.txt file changes location to for example C:\Temptwo folder
i need to learn this second location automatically when the location change happens is there way to do it in C# thanks.
It depends on where the files are moving from and to. If you are moving from C:\\MyProgram\TempA\Temp.txt to C:\\MyProgram\TempB\Temp.txt, then you can just use the directory tools. Directory.GetFiles will scan a directory for you and return an array of strings containing the names of those files. If you know the name of the file you are looking for, or you are using a unique file extension, then you can search through the files you find until you identify the file you are looking for. You can also call Directory.GetDirectories recursively to look through sub-directories if you have a lot of folders this file could be hiding in.
While this might technically work on the C drive, I would suggest carefully considering your use case before trying to re-invent Windows Search.

How to merge 2 zip files together into 1 zip

I am trying to make a custom launcher for Minecraft in C# but I have come across a bump.
I want to add something into it, Minecraft Forge, but the only way I could think of is to change the extension of minecraft.jar to minecraft.zip, extract the contents of the Minecraft Forge.zip and the minecraft.zip into the same folder and then zip that entire folder up into minecraft.jar.
However minecraft.jar has a file named aux.class so whenever my extract script (Made in java) tries to extract it, it simply says:
Unable to find file G:\Programming\C#\Console\Forge Installer\Forge Installer\bin\Debug\Merge\aux.class.
The only other way I can think of is to merge minecraft_forge.zip into minecraft.zip, I have spent around 2 hours looking on Google (watch as someone sees it within a couple of minutes) but it always shows me results for "How to zip multiple files", "How to make a zip file in C#" etc.
So I have come here looking for my answer, sorry if this is a lot to read but I always see comments on here saying "You didn't give enough information for us to help you with".
EDIT: The question in case it wasn't clear is: How am I able to put the contents of minecraft_forge.zip into minecraft.zip?
In your case, if you cannot unzip the files due to OS limitations, you need to "skip" unzipping temporary files to zip them. Instead, only handle input & output streams, as suggested in the answers found here: How can I add entries to an existing zip file in Java?
As you pointed out, "aux" is a protected keyword within windows and it does not matter what the file suffix may be; windows won't let you use it. Here are a couple of threads that discusses this in general.
Ref 1: Windows reserved words.
Ref 2: Windows reserved words.
If you are typing in commands to perform the copy or unzip, there is a chance you can get this to work by using a path prefix of the following \\.\ or \\?\. When I tested this, it worked with either a single or double back-slash following the period or question mark. Such that the following work:
\\.\c:\paths\etc
\\.\\c:\paths\etc
\\?\c:\path\etc
\\?\\c:\path\etc
I used the following command to test this. When trying to rename through windows explorer it gave a "The specified device name is invalid." error message. From the command line it worked just fine. I should point out, that once you create these files, you will have to manually delete them using the same technique. Windows Explorer reports that these text files which have a size of 0 bytes "is too large for the destination file system", ie... the recycle bin.
rename "\.\c:\temp\New Text Document.txt" aux.txt
del "\.\c:\temp\aux.txt"
As far as copying directly from zip or jar files, I tried this myself and it appeared to work. I used 7-zip and opened the jars directly using the "open archive..." windows explorer context menu. I then dragged-and-dropped the contents from forge.jar to the minecraft jar file. Since it is the minecraft jar file with the offending file name the chance of needing to create a temporary file on the filesystem is reduced. I did see someone mention that 7-zip may extract to a temporary file when copying between jars and zips.
7-zip reference on copying between archives
I should point out that my copy of minecraft jar (minecraft_server.1.8.7.jar) did not contain a file named aux.class. I also did not try to use the jar after the copy/merge. Nor did I spend too much time trying to figure out how well it merged the two contents since it appears like there may be a conflict with com\google\common\base\ since there are similar class name but with different $ variable suffixes on them.
I hope these two possible suggestions could give you some room to work with to find a solution for your needs... if you're still looking.

Using zip archives

I created quite a neat application for backups. It just copies the directories you specify to wherever you want. Unfortunately I encountered a problem. The maximum length of the file path is about 250 so what happens is that the program can't copy files if the new location path is longer - and the program collapses.
I learned that using zip archives would resolve the problem. But... The program uses some filters and it's logic isn't that straightforward. I really wouldn't like to rewrite it all.
My program visits all subdirectories in a specified directory and checks if there are any new or modified files there. If it finds some it copies it to ..../Backups/......../dir_A/dir_B/file. Every file and dir is processed separately (it's checked if it was modified, if it fits the filters, etc)
What I need is something that would help me substitute Backups directory with Backups.zip Especially offering something like this:
myArchive.copyFile("dir_a/dir_b/...../file", original_file_path);
myArchive.fileModificationDate("dir_a/dir_b/...../file");
myArchive.getDirectotiesIn("dir_a/dir_b/");
Is there such a feature in .Net? Or maybe there is a free library for this?
There are mainly two free alternatives for handling zip files in c# :
DotNetZip
SharpZipLib
I prefer DotNetZip.
SharpZipLib

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