C
C#•2y ago
Meistro

Hei all, I am new to this Server and I realy need help on specific Problem [Answered]

I am trying to move a .csv File from a Folder to another and it is actualy working but when I open the transfered File and compare it with the original one, the new File seem to be completly different. It is broken. There are new Characters in the new file which are not existing in the Origin one. Thanks in advance
61 Replies
ero
ero•2y ago
pelase share your code with us
Meistro
Meistro•2y ago
else //Move file from a folder to another
{
sr.Close();

{ //Move File to destination
string moveFrom = fileToMove; //source path
string moveTo = destinationPath_Failed + Filename; //destination

// Ensure that the target does not exist.
if (File.Exists(moveTo))
File.Delete(moveTo);

//moving file
File.Move(moveFrom, moveTo); //something went wrong here. I dont know why. The programm is not copying the file originaly
}
else //Move file from a folder to another
{
sr.Close();

{ //Move File to destination
string moveFrom = fileToMove; //source path
string moveTo = destinationPath_Failed + Filename; //destination

// Ensure that the target does not exist.
if (File.Exists(moveTo))
File.Delete(moveTo);

//moving file
File.Move(moveFrom, moveTo); //something went wrong here. I dont know why. The programm is not copying the file originaly
}
ero
ero•2y ago
looks fine
Meistro
Meistro•2y ago
the csv looks like this: "some text one the first line" after tranfer it looks like this: "some text one the first line ; ; ; ; ;; ; ; ;; ; " and it is writing a number in this length: 23452356345634563 to nomething like this: 23452E+16
ero
ero•2y ago
hm, that doesn't seem right moving a file just copies the raw bytes i'm pretty sure
Meistro
Meistro•2y ago
hmm I am realy comfusing and I realy need to finish this but dont know how to solve it :/ sorry for disturbing you
ero
ero•2y ago
i don't think the issue is with moving the file
Meistro
Meistro•2y ago
do you think its the File ? but it should still copy it correctly and originaly but it doesnt
ero
ero•2y ago
i'm not really sure, you'll have to show us more code
Meistro
Meistro•2y ago
Meistro
Meistro•2y ago
string strResult = sr.ReadToEnd(); //save file innerbody to a string
string[] result = strResult.Split(new string[] { Environment.NewLine }, StringSplitOptions.None); //split String in lines und save every Line in an Array

int i = 0;
List<string> converted = new(result);
if (!converted[0].Contains("H;")) //remove all blanks a initial H
{
for (int j = 0; j < result.Length; j++)
{
if (result[j].Contains("H;"))
{
while (!converted[i].Contains("H;"))
{
converted.RemoveAt(i);
}
break;
}
}

result = converted.ToArray();
}

i = result.Length - 1;
if (!result[i].Contains("T;")) //remove all blanks at initial T
{
for (int j = 0; j < result.Length; j++)
{
if (result[j].Contains("T;"))
{
while (!result[i].Contains("T;"))
{
result = result.SkipLast(1).ToArray();
i = result.Length - 1;
}
break;
}
}
}

int data = Get_datensaetze_anzahl(result); //count Datas... all line that have the inital D

string strResult = sr.ReadToEnd(); //save file innerbody to a string
string[] result = strResult.Split(new string[] { Environment.NewLine }, StringSplitOptions.None); //split String in lines und save every Line in an Array

int i = 0;
List<string> converted = new(result);
if (!converted[0].Contains("H;")) //remove all blanks a initial H
{
for (int j = 0; j < result.Length; j++)
{
if (result[j].Contains("H;"))
{
while (!converted[i].Contains("H;"))
{
converted.RemoveAt(i);
}
break;
}
}

result = converted.ToArray();
}

i = result.Length - 1;
if (!result[i].Contains("T;")) //remove all blanks at initial T
{
for (int j = 0; j < result.Length; j++)
{
if (result[j].Contains("T;"))
{
while (!result[i].Contains("T;"))
{
result = result.SkipLast(1).ToArray();
i = result.Length - 1;
}
break;
}
}
}

int data = Get_datensaetze_anzahl(result); //count Datas... all line that have the inital D

this part is actualy only to count Datas in the csv but I dont change the CSV
Kouhai
Kouhai•2y ago
Comment out all the code, and just do the move operation File.Move shouldn't corrupt the data
ero
ero•2y ago
and especially not turn a number from its full representation to the exponent one
Kouhai
Kouhai•2y ago
Oh, I didn't notice they said that 😅 Yeah, it's 100% not File.Move
Meistro
Meistro•2y ago
ok ok I will comment those other things out and try the move acion only thank you so much for your time guys ! I´ll give a feedback in a moment Hei Hei, me again. I´ve found something weird. I comented out this line
var files = from file in Directory.EnumerateFiles(rootFolderPath, "*.csv") select file;
var files = from file in Directory.EnumerateFiles(rootFolderPath, "*.csv") select file;
and replaced it with this one
string[] files = System.IO.Directory.GetFiles(rootFolderPath, "*.csv");
string[] files = System.IO.Directory.GetFiles(rootFolderPath, "*.csv");
and for some reason the files arent brocken. might it be the problem ?
Kouhai
Kouhai•2y ago
Directory.EnumerateFiles can't change your files content
ero
ero•2y ago
Also just a completely pointless linq query
Meistro
Meistro•2y ago
ok ok .... still dont get the File distroyer 😄 ok thank you anyways. Could you not close this Help section ? I would liek to give you a Feedback as soon as I found something. I have tested something else. When I open the original file and Copy everything to my clipboard and paste it in a text file. It produces the same broken file as my Code but I dont know where in the code it does such problem
Accord
Accord•2y ago
Ask the thread owner or member with permission to close this!
ero
ero•2y ago
that sounds weird, don't know about that
Meistro
Meistro•2y ago
its a .csv file btw .csv have many custom settings and grids in it ... ok anyways. Thank you guys have a nice day 🙂
Kouhai
Kouhai•2y ago
How are you viewing your CSV content? In a text editor or as a table?
Meistro
Meistro•2y ago
after the File.move I opend it with both. Text editor(notepad++) and table(excel)
Kouhai
Kouhai•2y ago
And how are you copying the original file's data to your clipboard? From excel or notepad++
Meistro
Meistro•2y ago
this is the corrupted file content example: H;1;270345392022;4632;;;;;;;;;;;;;;;;;;;;;;; and this is the original H;1;70345392022;4632 from notepad and put it in csv
Kouhai
Kouhai•2y ago
That same corrupted data happen when copy to the clipboard?
Meistro
Meistro•2y ago
yes.... but its just a test... I wanted to reproduce the move by hand but I dont know if its the right ways
Kouhai
Kouhai•2y ago
That's very weird, would you be able to share the original csv?
Meistro
Meistro•2y ago
emmm oh sorry ... I cant 😦 becauz its an intern data I could make an example for you
ero
ero•2y ago
i can only imagine this happening if you copy data from like one OS to another like between the OS and a VM
Kouhai
Kouhai•2y ago
Hmmm, if you can make a reproducible example that might help 😅 But anyhow, I would honestly open the original file in a hex editor and examine the contents
Meistro
Meistro•2y ago
hex editor ? sorry for late response I tryed to reproduce a file with no errors but seems that as soon as I edtited the file it also corrupted it sooo weird haha
Meistro
Meistro•2y ago
here is an example When I open thi sin excel it shows everything normal but when I edit it it give those weird semicolon and same when I run the File.move in code I will respond you in 1 Hour... I have to travel with train now 😄 no Internet on my way
Kouhai
Kouhai•2y ago
Your best bet is to open the original file in a hex editor and examine it
Meistro
Meistro•2y ago
ok thank you I will try it. Sorry I was busy when I came home. Back to office. i will try it out now I tryed the Hex thing but cant figure it out how it is possible that it adds more characters than it has after moving the file
Kouhai
Kouhai•2y ago
How many bytes does the file have before and after moving?
Meistro
Meistro•2y ago
oh thats a good question but good hint ! I will check out ass soon as I have the posibility ... I have found out a reproduce way. open this in excel, then save it and close it. next open it with notepad and you will see that there are semicolons on top line and last line
Kouhai
Kouhai•2y ago
First off, these commas are added to pad header Second H;1;2, why are commas and semicolons both used to separate cells? Third, saving in excel is different from copying the file.
Cisien
Cisien•2y ago
moving a file just updates the filesystem metadata, it doesn't copy bytes. that's why moves can be significantly faster than copies
ero
ero•2y ago
it's a floating point number? 2,70346E+14
Kouhai
Kouhai•2y ago
You're probably right, but that won't work well with CSV
Meistro
Meistro•2y ago
how do I even make Copys in C# ?
mtreit
mtreit•2y ago
What do you mean? This is true if the move is to a location on the same drive 😉
Meistro
Meistro•2y ago
Here Moving only updates… but how do i perform a copy
mtreit
mtreit•2y ago
Of a file?
Meistro
Meistro•2y ago
Yes ^^
mtreit
mtreit•2y ago
File.Copy
Meistro
Meistro•2y ago
Ahh ok… and i can save the copy in an object ?
mtreit
mtreit•2y ago
The copy is like any other file, you can do whatever you like with it.
Meistro
Meistro•2y ago
Thank you ! Maybe this would solve the problem ok thank you. I will test it again tomorrow …. Is there a way to make a file only openable with a specific app ?
mtreit
mtreit•2y ago
No
Meistro
Meistro•2y ago
Kk
mtreit
mtreit•2y ago
Well Maybe If you run that app as a different user and set the DACL on the file to only allow that user access it's theoretically possible.
Meistro
Meistro•2y ago
Becauz opening the csv with excel then save it and open finaly opening the file again with e.g Notepad++ breaks the file
mtreit
mtreit•2y ago
Breaks the file how? It's possible you have notepad++ configured to change the encoding or line endings or something.
Meistro
Meistro•2y ago
Could you maybe help with my other question ?
mtreit
mtreit•2y ago
Normally opening a file in a text editor shouldn't change it unless you explicitly do something to change the file.
Meistro
Meistro•2y ago
The excel does something when opening in excel This one as an example
Accord
Accord•2y ago
✅ This post has been marked as answered!