Compare file name variable with file path variable - c#

I have created some code which pulls a list of file names off the server and stores them, I also get the folder names from a directory on my computer and I need to compare them both to find out if there are any names which are on the local machine and not on the server.
serverlistarray contains the list of folders on the server (1 folder per entry), local listarray contains the directory listing on the local computer, this is also has 1 folder per entry. I read the local files using this:
String[] localfilelistarray = System.IO.Directory.GetDirectories(/*file location*/);
My problem lies within the get directory's, it includes the full path of the file which I cannot compare. The code below is what I have tried to do, I check the serverlistarray for the file in locallistarray and if they match i put them in the list.
for (int b = 0; b < localfilelistarray.Length; b++)
{
//problem with the compare
if (!serverlistarray.Contains(localfilelistarray[b].Replace(/*file path before filename*/, "")))
{
//add to list variable
}
}
I then use this code to do what I want for everything in the list (for now its a message box to show the filename).
for (int f = 0; f < delete.Count(); f++)
{
MessageBox.Show(/*list variable*/);
}
BUT I get every file even if it is in the serverlistarray and I cant work out whats wrong. I believe its to do with the comparison of the two arrays but when I put message boxes in that loop thay come out as expected but the comparison doesnt seem to work.
Can anyone see what ive done wrong?
ive tried using replace and direct comparisons to no avail. I though of adding the file path to the serverlistarray BUT I can not do this as I need the raw file name for server operations and other parts of this code.
EDIT:
serverlistarray example:
[0]folder1
[1]folder2
[2]folder3
localfilearray example:
[0]c:\\users\Noliver\folder1
[1]c:\\users\Noliver\folder2
[2]c:\\users\Noliver\folder3
[3]c:\\users\Noliver\folder4
[4]c:\\users\Noliver\folder5
expected output (message boxes in for loop):
c:\\users\Noliver\folder4
&
c:\\users\Noliver\folder5
EDIT 2:
Ive tried this code with no lick using the file path:
for (int b = 0; b < localfilelistarray.Length; b++)
{
String[] temp = System.IO.Path.GetFileName(localfilelistarray[b]).Split('.');
MessageBox.Show(localfilelistarray[b] + " - " + temp[0]);
for (int t = 0; t < serverlistarray.Length;t++)
{
MessageBox.Show("server " + serverlistarray[t] + " - " + localfilelistarray[b] + " - " + temp[0]);
}
if (!update.Contains(temp[0]))
{
delete.Add(localfilelistarray[b]);
}
}
the output from this showed that the serverlistarray contained the file name from temp[0] (the file name with the extension taken off) but all the files were in the list variable. could this come down to new lines or a space at the end of one of the strings? for some reason the if statement checking if the array contains the value doesnt work.

From the way the string is stored the deepest folder name were just written as if they are a file name. So I used Path.GetFileName to extract it.
localfilearray.Where(x => !serverlistarray.Contains(Path.GetFileName(x)))
This work as well on most of case, if you don't like the name of previous one :
localfilearray.Where(x => !serverlistarray.Contains(x.Split('\\').Last()))
To use the above code with what you have now, you can :
Expand your existing delete list to include what to delete :
delete.AddRange(localfilearray.Where(x => !serverlistarray.Contains(x.Split('\\').Last())));
Or, just declare it like this :
var delete = new List(localfilearray.Where(x => !serverlistarray.Contains(x.Split('\\').Last())));`

I'd advise against splitting on "\" etc., instead use DirectoryInfo to get the name of the directory:
var directoryInfo = new DirectoryInfo(#"c:\MyDir");
String directoryName = directoryInfo.Name; // returns "MyDir"

Related

Batch takes a part from the file name and create a folders with this part

I have files with names like this:
414_gtmlk_videos_Mas_147852_hty1147.xls
414_gtmlk_videos_Mas_P147852_hty1147.txt
I want to creat a job to check the filenames and take the part after Mas in the file name (147852-P147852)
and create a folders with this part name (the folder name should be: 147852-P147852).
And finally move each file to his folder.
Batch takes a part from the file name and create a folders with this part i have files with names like this:
414_gtmlk_videos_Mas_147852_hty1147.xls
414_gtmlk_videos_Mas_P147852_hty1147.txt (the folder name will be
here:147852-P147852)
Here's a way to do this with a Batch Script since you have this tagged as a batch-file in your question. Just set your source directory accordingly and the rest should just work based on the detail you provided and my understanding.
I used a simple batch FOR /F loop incorporating MD with IF conditions. I used the underbar characters as the delimiter and set the token to 5 to make this work.
#ECHO ON
SET Src=C:\Folder\Path
FOR /F "TOKENS=5 DELIMS=_" %%F IN ('DIR /B /A-D "%Src%\*.txt"') DO (
IF NOT EXIST "%Src%\%%~F-P%%~F" MD "%Src%\%%~F-P%%~F"
IF EXIST "%Src%\*%%~F*P%%~F*.txt" MOVE /Y "%Src%\*%%~F*P%%~F*.txt" "%Src%\%%~F-P%%~F"
)
GOTO EOF
Further Resources
FOR /F
IF
MD
FOR /?
delims=xxx - specifies a delimiter set. This replaces the
default delimiter set of space and tab.
tokens=x,y,m-n - specifies which tokens from each line are to
be passed to the for body for each iteration.
This will cause additional variable names to
be allocated. The m-n form is a range,
specifying the mth through the nth tokens. If
the last character in the tokens= string is an
asterisk, then an additional variable is
allocated and receives the remaining text on
the line after the last token parsed.
I have some C# code below. The first part does the following:
Gets paths
Get names of file
Modify full paths to get "147852" part, between __Mas_ and last _
string pathToGetFile = #"C:\\";
string[] filePaths = System.IO.Directory.GetFiles(pathToGetFile +#"\\", "*_Mas_*");
string[] fullName = new string[filePaths.Length];
for (int i = 0; i < filePaths.Length; i++)
{
fullName[i] = filePaths[i].Substring(filePaths[i].LastIndexOf("\\") + 1);
filePaths[i] = filePaths[i].Substring(filePaths[i].LastIndexOf("_Mas_") + 5);
int l = filePaths[i].IndexOf("_");
filePaths[i] = filePaths[i].Substring(0, l);
Now you can create folders with yours names
filePaths is now like that: 147852, P147852
if (!Directory.Exists(#"C:\" + filePaths[i]))
System.IO.Directory.CreateDirectory(#"C:\" + filePaths[i]);
}
Now just move files to new directories
for (int i = 0; i < filePaths.Length; i++)
{
string sourceFile = System.IO.Path.Combine(pathToGetFile, fullName[i]);
string destFile = System.IO.Path.Combine(#"C:\" + filePaths[i], #"C:\" + filePaths[i] + "\\" + fullName[i]);
File.Copy(sourceFile,destFile,true);
}
Now, what happens
Files:
C:\414_gtmlk_videos_Mas_147852_hty1147.xls
C:\414_gtmlk_videos_Mas_P147852_hty1147.txt
They will be copied according to the:
C:\147852\
C:\P147852\

C# Split File Name Beginner Exercise

I have a directory filled with multiple excel files that I would like to rename. The names all have leading integers and a '-'. For example: 0123456-Test_01. I would like to rename all of the files within this directory by removing this prefix. 0123456-Test_01 should just be Test_01. I can rename a hard coded instance of a string, but am having trouble getting the files and renaming all of them.
My code is below. Any help is appreciated, as I am clearly new to C#.
public static void Main()
{
//Successfully splits hardcoded string
var temp = "0005689-Test_01".Split('-');
Console.WriteLine(temp[1]);
Console.ReadLine();
//Unsuccessful renaming of all files within directory
List<string> files = System.IO.Directory.GetFiles(#"C:\Users\acars\Desktop\B", "*").ToList();
System.IO.File.Move(#"C:\Users\acars\Desktop\B\", #"C:\Users\acars\Desktop\B\".Split('-'));
foreach (string file in files)
{
var temp = files.Split('-');
return temp[1];
};
}
There are some errors to fix in your code.
The first one is the wrong usage of the variable files. This is the full list of files, not the single file that you want to split and move. As explained comments you should use the iterator result stored in the variable file
The most important problem is the fact that the File.Move method throws an exception if the destination file exists. After removing the first part of your filename string, you cannot be sure that the resulting name is unique in your directory.
So a check for the existance of the file before the Move is mandatory.
Finally, it is better use Directory.EnumerateFiles because this method allows you to start the execution of your moving code without loading first all filenames in memory in a list. (In a folder full of files this could make a noticeable difference in speed)
public static void Main()
{
string workPath = #"C:\Users\acars\Desktop\B";
foreach (string file in Directory.EnumerateFiles(workPath)
{
string[] temp = file.Split('-');
if(temp.Length > 1)
{
string newName = Path.Combine(workPath, temp[1]);
if(!File.Exists(newName))
File.Move(file, newName);
}
}
}
Pay also attention to the comment below from CodeNotFound. You are using an hard-coded path so the problem actually doesn't exist, but if the directory contains a single "-" in its name then you should use something like this to get the last element in the splitted array
string newName = Path.Combine(workPath, temp[temp.Length-1]);

copying files from one folder to another only if their file name is a in a specified text file c#

I need to copy files from one folder to another but only if their file name is also in a text file. The text file is set up like so
file1.jpg
file2.jpg
file3.jpg
etc
There are around one-million files to copy. I'm using C#.
What would the best way to go about this be? I'm not sure if I should first read all the file names from the text file and put them in to a list then possibly convert the list in to an array then maybe use the array somehow? Or maybe there's a better way to go about it?
I know how to read and write to files and how to copy from one source destination to another. I don't know how to filter out specific files when copying from one source destination to another though.
Any help is greatly appreciated.
Thank you.
The following code will help you the process you want
string source = #"C:\SourcePath\";
string destination = #"C:\DestinationPath\";
string[] strFiles = File.ReadAllText(#"C:\Filename.txt").Split(' ');
for (int i = 0; i < strFiles.Length; i++)
{
File.Copy(source + strFiles[i], destination + strFiles[i]);
}
If the text file is one line with million files name. Use this
string from = #"c:\from" , to =#"d:\to"; // source and destination
StreamReader file = new StreamReader(#"c:\list.txt"); // your files list
string total=file.ReadLine();
string[] tobecopied = total.Split(' ');
foreach(string fil in tobecopied)
{
if(File.Exists(from+#"\"+fil))
{
File.Copy(from+#"\"+fil,to+#"\"+fil);
}
else
{
MessageBox.Show(fil+"Not found ");
}
}
But if the text file have 1 line per 1 file , for example
FIle1.exe
File2.exe
use this
string from = #"c:\from" , to =#"d:\to"; // source and destination
StreamReader file = new StreamReader(#"c:\list.txt"); // your files list
string total="";
string temp="";
while((temp=file.ReadLine())!=null)
{
total+=temp+" ";
}
string[] tobecopied = total.Split(' ');
foreach(string fil in tobecopied)
{
if(File.Exists(from+#"\"+fil))
{
File.Copy(from+#"\"+fil,to+#"\"+fil);
}
else
{
MessageBox.Show(fil+"Not found ");
}
}
These ways also check for file existance.
Hope it works. If someone see error please edit it.

Retrieving a Single File and the Behavior of Directory.GetFiles C#

public int RunStageData(string rootDirectory, stringdataFolder)
{
string[] files = new string[] { };
files = Directory.GetFiles(rootDirectory + dataFolder);
string[] tableOrder = new string[] { };
tableOrder = Directory.GetFiles(#"C:\_projects\ExampleProject\src", "TableOrder.txt");
System.IO.StreamReader tableOrderReader = new System.IO.StreamReader(tableOrder[0]);
for (int count = 0; count < files.Length; count++)
{
string currentTableName =tableOrderReader.ReadLine();
//files[count] = Directory.GetFiles(#"C:\_projects\ExampleProject\src", currentTableName);
}
}
Hi everyone, sorry if my code is a bit sloppy. I'm having an issue primarily with the line I have commented out. So basically what I'm trying to do here is to populate a string array of file names based on the ordering of these names in a txt file. So I read the first line from the txt file, then retrieve the name of that file in the directory(assuming it exists) and put it in the first spot of the array, then move on.
For Example if the txt file had these words in the following order:
Dog
Sheep
Cat
I would want the array to have Dog first, then Sheep, then Cat. My issue is that the line that I have commented gives me an error that says "Error 41 Cannot implicitly convert type 'string[]' to 'string'"
I'm guessing the reason for this is that Directory.GetFiles has the possibility of returning multiple files. So, is there another method I could use to achieve the results I'm looking for? Thank you.
I am assuming you want the contents of the file (if you just want the file name and need to check for existance a different solution will be required).
files[count] = File.ReadAllText(Path.Combine(#"C:\_projects\ExampleProject\src", currentTableName));
And a couple other suggestions:
Don't initialize your variables with bogus data, = new string[] {} can be removed
Don't use count as an indexer, it is confusing (count is a property of the array after all)
Use Path.Combine when joining paths. It is much easier as it handles the \ for you.
From your question:
So basically what I'm trying to do here is to populate a string array
of file names based on the ordering of these names in a txt file. So I
read the first line from the txt file, then retrieve the name of that
file in the directory(assuming it exists) and put it in the first spot
of the array, then move on.
So, your TableOrder.txt already contains the files in the correct order, thus you can do:
string[] files = File.ReadAllLines(#"C:\_projects\ExampleProject\src\TableOrder.txt")
If your array files contains only paths, you can do it as:
path = #"C:\_projects\ExampleProject\src\" + currentTableName;
If(File.Exists(path))
{
files[count] = path;
}

How to match two paths pointing to the same file?

I have two lists containing paths to a directory of music files and I want to determine which of these files are stored on both lists and which are only stored on one. The problem lies in that the format of the paths differ between the two lists.
Format example:
List1: file://localhost//FILE/Musik/30%20Seconds%20To%20Mars.mp3
List2: \\FILE\Musik\30 Seconds To Mars.mp3
How do I go about comparing these two file paths and matching them to the same source?
The answer depends on your notion of "same file". If you merely want to check if the file is equal, but not the very same file, you could simply generate a hash over the file's content and compare that. If the hashes are equal (please use a strong hash, like SHA-256), you can be confident that the files are also. Likewise you could of course also compare the files byte by byte.
If you really want to figure that the two files are actually the same file, i.e. just addressed via different means (like file-URL or UNC path), you have a little more work to do.
First you need to find out the true file system path for each of the addresses. For example, you need to find the file system path behind the UNC path and/or file-URL (which typically is the URL itself). In the case of UNC paths, that are shares on a remote computer, you might even be able to do so.
Also, even if you have the local path figured out somehow, you also need to deal with different redirection mechanisms for local paths (on Windows junctions/reparse points/links; on UNIX symbolic or hard links). For example, you could have a share using file system link as source, while the file URL uses the true source path. So to the casual observer they still look like different files.
Having all that said, the "algorithm" would be something like this:
Figure out the source path for the URLs, UNC paths/shares, etc. you have
Figure out the local source path from those paths (considering links/junctions, subst.exe, etc.)
Normalize those paths, if necessary (i.e. a/b/../c is actually a/c)
Compare the resulting paths.
I think the best way to do it is by temporarily converting one of the paths to the other one's format. I would suggest you change the first to match the second.
string List1 = "file://localhost//FILE/Musik/30%20Seconds%20To%20Mars.mp3"
string List2 = "\\FILE\Musik\30 Seconds To Mars.mp3"
I would recommend you use Replace()-method.
Get rid of "file://localhost":
var tempStr = List1.Replace("file://localhost", "");
Change all '%20' into spaces:
tempStr = List1.Replace("%20", " ");
Change all '/' into '\':
tempStr = List1.Replace("/", "\");
VoilĂ ! To strings in matching format!
Use python: you can easily compare the two files like this
>>> import filecmp
>>> filecmp.cmp('file1.txt', 'file1.txt')
True
>>> filecmp.cmp('file1.txt', 'file2.txt')
False
to open the files with the file:// syntax use URLLIB
>>> import urllib
>>> file1 = urllib.urlopen('file://localhost/tmp/test')
for the normal files path use the standard file open.
>>> file2 = open('/pathtofile','r')
I agree completely with Christian, you should re-think structure of the lists, but the below should get you going.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication5
{
class Program
{
public static List<string> SanitiseList(List<string> list)
{
List<string> sanitisedList = new List<string>();
foreach (string filename in list)
{
String sanitisedFilename = String.Empty;
if (!String.IsNullOrEmpty(filename))
{
sanitisedFilename = filename;
// get rid of the encoding
sanitisedFilename = Uri.UnescapeDataString(sanitisedFilename);
// first of all change all back-slahses to forward slashes
sanitisedFilename = sanitisedFilename.Replace(#"\", #"/");
// if we have two back-slashes at the beginning assume its localhsot
if (sanitisedFilename.Substring(0, 2) == "//")
{
// remove these first double slashes and stick in localhost
sanitisedFilename = sanitisedFilename.TrimStart('/');
sanitisedFilename = sanitisedFilename = "//localhost" + "/" + sanitisedFilename;
}
// remove file
sanitisedFilename = sanitisedFilename.Replace(#"file://", "//");
// remove double back-slashes
sanitisedFilename = sanitisedFilename.Replace("\\", #"\");
// remove double forward-slashes (but not the first two)
sanitisedFilename = sanitisedFilename.Substring(0,2) + sanitisedFilename.Substring(2, sanitisedFilename.Length - 2).Replace("//", #"/");
}
if (!String.IsNullOrEmpty(sanitisedFilename))
{
sanitisedList.Add(sanitisedFilename);
}
}
return sanitisedList;
}
static void Main(string[] args)
{
List<string> listA = new List<string>();
List<string> listB = new List<string>();
listA.Add("file://localhost//FILE/Musik/BritneySpears.mp3");
listA.Add("file://localhost//FILE/Musik/30%20Seconds%20To%20Mars.mp3");
listB.Add("file://localhost//FILE/Musik/120%20Seconds%20To%20Mars.mp3");
listB.Add(#"\\FILE\Musik\30 Seconds To Mars.mp3");
listB.Add(#"\\FILE\Musik\5 Seconds To Mars.mp3");
listA = SanitiseList(listA);
listB = SanitiseList(listB);
List<string> missingFromA = listB.Except(listA).ToList();
List<string> missingFromB = listA.Except(listB).ToList();
}
}
}

Categories