1

I'm trying to get a latest file from a path and copying it, then paste it into a generated folder.

This is what I tried so far:

    // This Method is called if the function/method CopyContent is invoked by the user or a bound event.
// Return true, if this component has to be revalidated!
public bool OnCopyContent(int arg)
{
    // Get latet file from the specificed Folder
    var directory = new DirectoryInfo(@""+sourceFolderPath);
    var myFile = (from f in directory.GetFiles()
            orderby f.LastWriteTime descending
            select f).First();


    // Newly Created Folder Name
    string generatedFolderName = destinationFolderName;


    // Newly Creted Folder Path (i.e C://Users/Desktop) Cretge it on desktop with name "Paste me here " 
    string generatedPathString = System.IO.Path.Combine(destinationFolderPath, generatedFolderName);


    if (!File.Exists(generatedPathString))
        System.IO.Directory.CreateDirectory(generatedPathString);



    // Copy the Latet file to the newly Created Folder on the Desktop
    string destFile = Path.Combine(@""+destinationFolderPath, myFile.Name);



    File.Copy(myFile.FullName, destFile, true);

    return false;
}

What im trying to do is

1 : I have Specifed Folder Path , i want to copy it's latest file in it depending on timee

2: Create new Folder on Desktop with name " NewlyAdded"

3: Paste the Copied File from the Specifed Folder to the Newly Created Folder

David
  • 361
  • 2
  • 3
  • 16
  • Possible duplicate of [How to copy a file to another path?](https://stackoverflow.com/questions/1979920/how-to-copy-a-file-to-another-path) – Heretic Monkey Jun 09 '18 at 18:57
  • 1
    What exactly is your question? Your code looks fine at a glance - what error are you having or what is going wrong? – Chris Jun 09 '18 at 19:17

3 Answers3

2

now my issue is how to copy it

simply : take the file name ans pass it with the destination folder to a string, then pass the file.FullName to Copy() method and it'll be copied. This code tested and worked.

EDIT : Added a line to generate folder if not exist, and copy the file to it.

 string newFolder = "NewlyAdded";
            string path = System.IO.Path.Combine(
               Environment.GetFolderPath(Environment.SpecialFolder.Desktop),
               newFolder
            );

            if (!System.IO.Directory.Exists(path))
            {
                System.IO.Directory.CreateDirectory(path);
             }
                    var directory = new DirectoryInfo(@"Sourcd folder");
            var myFile = (from f in directory.GetFiles()
                          orderby f.LastWriteTime descending
                          select f).First();

            string destFile = Path.Combine(path, myFile.Name);
            System.IO.File.Copy(myFile.FullName, destFile, true);

the true as a last parameter is to overwrite if exist.

Kaj
  • 806
  • 6
  • 16
  • string destFile = Path.Combine(@"Distination Folder", myFile.Name); the Distination Folder is a Generated Folder – David Jun 09 '18 at 18:34
  • Ok pass it's name to the method, I really didn't get the problem here – Kaj Jun 09 '18 at 18:35
  • mmm I'm not sure this is working for me with the genrated Folder . i have updated my code pls check it – David Jun 09 '18 at 18:44
  • how do you pass the name for the generated folder ? I really can't understand ur code ! ok i'll ask different, where would you generate that folder ? is it random ? just give me the method you want to generate the folder with so I can help you more – Kaj Jun 09 '18 at 18:54
  • I'm sorry pls check my edited question, i have cleared stuff – David Jun 09 '18 at 18:59
  • Never mind, I've updated my answer to suit ur needs, check it please. – Kaj Jun 09 '18 at 19:06
  • I dont not have a folder called NewlyAdded it just be Crated Via code then i acces it and paste the copied file in it – David Jun 09 '18 at 19:12
  • It'll check if the folder doesn't exist, it'll be created and copy the file to it, else, it'll just copy the file to the folder. isn't what you want ? – Kaj Jun 09 '18 at 19:14
  • Yes exacatly :D – David Jun 09 '18 at 19:15
  • yes it does, I tested it before I wrote it here ! tested and works – Kaj Jun 09 '18 at 19:19
  • Sorted Thank you – David Jun 09 '18 at 19:22
  • the first line of the code, for the folder name, the second one, for the folder location. so you can change them as you want, nothing wrong will happened. Try to read the code and understand the steps and you could modify it as you want. Happy coding :) – Kaj Jun 09 '18 at 19:22
  • Yes i understood :D it works now thank you a lot sir! – David Jun 09 '18 at 19:23
0

Looking at the documentation, the first parameter is the path of the original file...

https://msdn.microsoft.com/en-us/library/9706cfs5(v=vs.110).aspx

I have trying to copy it however im not sure now how to pass it in

You use the file path of the original, not the FileInfo itself!

Like this:

        var directory = new DirectoryInfo(@"C:\");
        FileInfo myFile = (from f in directory.GetFiles()
                      orderby f.LastWriteTime descending
                      select f).First();
        string filePath = myFile.FullName;

You'd use this filePath variable, not the FileInfo instance you're currently using.

Dan Rayson
  • 1,315
  • 1
  • 14
  • 37
  • How does that work? I dont want it to copy the entire folder, i want it to only copy the last file i got – David Jun 09 '18 at 17:47
  • @David Editted - make sense now? – Dan Rayson Jun 09 '18 at 17:50
  • Let me test please. thank you. i will check it and come bacl to you – David Jun 09 '18 at 17:51
  • When i try it , im getting an exception that says "the target is a directory and not a file" – David Jun 09 '18 at 17:55
  • @David It's quite clearly a random filename.... ` string fileName = System.IO.Path.GetRandomFileName();` from your original post. Lol :) – Dan Rayson Jun 09 '18 at 17:57
  • @David And I would assume trying to create a directory from a file path would throw the very exception you're seeing. – Dan Rayson Jun 09 '18 at 17:58
  • My bad.. I meant to show this really... 09:58 PM 09/06/2018 Error : Script (C#.net Script): Check method OnCopyContent in Script Node Script.: Exception has been thrown by the target of an invocation. / Exception has been thrown by the target of an invocation. / Cannot create "C:\Users\User\Desktop\copyFrom\80. Working with skyboxes & directional lights in Unity.mp4" because a file or directory with the same name already exists. – David Jun 09 '18 at 17:59
  • @David There's two possibilities, either the CreateDirectory is throwing that exception, or the File.Copy is. I believe it's the former of the two. You should perhaps merge the directory and filename together only when needed, just use a pure directory string in CreateDirectory. – Dan Rayson Jun 09 '18 at 18:05
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/172821/discussion-between-david-and-dan-rayson). – David Jun 09 '18 at 18:08
0
  • if you coudn't copy string file then open your current file which you want to copy in binary read (rb) and store that in variable.
  • after write that file in particular directory by using binary write

    may be it should help you

manan5439
  • 898
  • 9
  • 24