Creating paths with variables (move a folder and contents to new folder)










0















looked all over and found plenty of stuff regarding this but none are using variables to form paths. What I need to do is move a folder, sub-folders and files to a new path on button click. So far none of what I found worked. At the moment, i'm getting no files or folders moved what so ever. The current solution I tried is from MSDN and tried to adapt it to my code. If you could correct the code and show me an example it would be great. I don't know what i'm doing wrong here. Here is the code:



private void CopyPartsToProject()

string sourcePath = (pathToQuotes + "/" + client_name.Text + "/" + quote_id.Text);
string targetPath = (pathToClient + "/" + client_name.Text + "/" + project_number.Text);

string sourceFile = sourcePath + "/" + "*.*";
string destinationFile = targetPath + "/" + "*.*";

System.IO.File.Move(sourceFile, destinationFile);

System.IO.Directory.Move(sourcePath, targetPath);




pathToQuotes and pathToClient are retreived from a MySQL database (from user input) in another method. The info is getting retreived without any problems and the paths are correct. If you could give me a hand it would be appreciated. Thanks.










share|improve this question


























    0















    looked all over and found plenty of stuff regarding this but none are using variables to form paths. What I need to do is move a folder, sub-folders and files to a new path on button click. So far none of what I found worked. At the moment, i'm getting no files or folders moved what so ever. The current solution I tried is from MSDN and tried to adapt it to my code. If you could correct the code and show me an example it would be great. I don't know what i'm doing wrong here. Here is the code:



    private void CopyPartsToProject()

    string sourcePath = (pathToQuotes + "/" + client_name.Text + "/" + quote_id.Text);
    string targetPath = (pathToClient + "/" + client_name.Text + "/" + project_number.Text);

    string sourceFile = sourcePath + "/" + "*.*";
    string destinationFile = targetPath + "/" + "*.*";

    System.IO.File.Move(sourceFile, destinationFile);

    System.IO.Directory.Move(sourcePath, targetPath);




    pathToQuotes and pathToClient are retreived from a MySQL database (from user input) in another method. The info is getting retreived without any problems and the paths are correct. If you could give me a hand it would be appreciated. Thanks.










    share|improve this question
























      0












      0








      0








      looked all over and found plenty of stuff regarding this but none are using variables to form paths. What I need to do is move a folder, sub-folders and files to a new path on button click. So far none of what I found worked. At the moment, i'm getting no files or folders moved what so ever. The current solution I tried is from MSDN and tried to adapt it to my code. If you could correct the code and show me an example it would be great. I don't know what i'm doing wrong here. Here is the code:



      private void CopyPartsToProject()

      string sourcePath = (pathToQuotes + "/" + client_name.Text + "/" + quote_id.Text);
      string targetPath = (pathToClient + "/" + client_name.Text + "/" + project_number.Text);

      string sourceFile = sourcePath + "/" + "*.*";
      string destinationFile = targetPath + "/" + "*.*";

      System.IO.File.Move(sourceFile, destinationFile);

      System.IO.Directory.Move(sourcePath, targetPath);




      pathToQuotes and pathToClient are retreived from a MySQL database (from user input) in another method. The info is getting retreived without any problems and the paths are correct. If you could give me a hand it would be appreciated. Thanks.










      share|improve this question














      looked all over and found plenty of stuff regarding this but none are using variables to form paths. What I need to do is move a folder, sub-folders and files to a new path on button click. So far none of what I found worked. At the moment, i'm getting no files or folders moved what so ever. The current solution I tried is from MSDN and tried to adapt it to my code. If you could correct the code and show me an example it would be great. I don't know what i'm doing wrong here. Here is the code:



      private void CopyPartsToProject()

      string sourcePath = (pathToQuotes + "/" + client_name.Text + "/" + quote_id.Text);
      string targetPath = (pathToClient + "/" + client_name.Text + "/" + project_number.Text);

      string sourceFile = sourcePath + "/" + "*.*";
      string destinationFile = targetPath + "/" + "*.*";

      System.IO.File.Move(sourceFile, destinationFile);

      System.IO.Directory.Move(sourcePath, targetPath);




      pathToQuotes and pathToClient are retreived from a MySQL database (from user input) in another method. The info is getting retreived without any problems and the paths are correct. If you could give me a hand it would be appreciated. Thanks.







      c# winforms






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 13 '18 at 19:52









      Christian VernerChristian Verner

      538




      538






















          1 Answer
          1






          active

          oldest

          votes


















          0














          You need a recursive method to achieve moving directories including all files and sub-directories:



          private void moveDirectory(string sourcePath ,string targetPath)

          if (!System.IO.Directory.Exists(targetPath))

          System.IO.Directory.CreateDirectory(targetPath);

          String files = Directory.GetFiles(sourcePath);
          String directories = Directory.GetDirectories(sourcePath);
          foreach (string f in files)

          System.IO.File.Copy(f, Path.Combine(targetPath,Path.GetFileName(f)), true);

          foreach(string d in directories)

          // recursive call
          moveDirectory(Path.Combine(sourcePath, Path.GetFileName(d)), Path.Combine(targetPath, Path.GetFileName(d)));




          then the usage is like this:



          private void CopyPartsToProject()

          string sourcePath = (pathToQuotes + "/" + client_name.Text + "/" + quote_id.Text);
          string targetPath = (pathToClient + "/" + client_name.Text + "/" + project_number.Text);

          moveDirectory(sourcePath, targetPath);






          share|improve this answer






















            Your Answer






            StackExchange.ifUsing("editor", function ()
            StackExchange.using("externalEditor", function ()
            StackExchange.using("snippets", function ()
            StackExchange.snippets.init();
            );
            );
            , "code-snippets");

            StackExchange.ready(function()
            var channelOptions =
            tags: "".split(" "),
            id: "1"
            ;
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function()
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled)
            StackExchange.using("snippets", function()
            createEditor();
            );

            else
            createEditor();

            );

            function createEditor()
            StackExchange.prepareEditor(
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader:
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            ,
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            );



            );













            draft saved

            draft discarded


















            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53288520%2fcreating-paths-with-variables-move-a-folder-and-contents-to-new-folder%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            0














            You need a recursive method to achieve moving directories including all files and sub-directories:



            private void moveDirectory(string sourcePath ,string targetPath)

            if (!System.IO.Directory.Exists(targetPath))

            System.IO.Directory.CreateDirectory(targetPath);

            String files = Directory.GetFiles(sourcePath);
            String directories = Directory.GetDirectories(sourcePath);
            foreach (string f in files)

            System.IO.File.Copy(f, Path.Combine(targetPath,Path.GetFileName(f)), true);

            foreach(string d in directories)

            // recursive call
            moveDirectory(Path.Combine(sourcePath, Path.GetFileName(d)), Path.Combine(targetPath, Path.GetFileName(d)));




            then the usage is like this:



            private void CopyPartsToProject()

            string sourcePath = (pathToQuotes + "/" + client_name.Text + "/" + quote_id.Text);
            string targetPath = (pathToClient + "/" + client_name.Text + "/" + project_number.Text);

            moveDirectory(sourcePath, targetPath);






            share|improve this answer



























              0














              You need a recursive method to achieve moving directories including all files and sub-directories:



              private void moveDirectory(string sourcePath ,string targetPath)

              if (!System.IO.Directory.Exists(targetPath))

              System.IO.Directory.CreateDirectory(targetPath);

              String files = Directory.GetFiles(sourcePath);
              String directories = Directory.GetDirectories(sourcePath);
              foreach (string f in files)

              System.IO.File.Copy(f, Path.Combine(targetPath,Path.GetFileName(f)), true);

              foreach(string d in directories)

              // recursive call
              moveDirectory(Path.Combine(sourcePath, Path.GetFileName(d)), Path.Combine(targetPath, Path.GetFileName(d)));




              then the usage is like this:



              private void CopyPartsToProject()

              string sourcePath = (pathToQuotes + "/" + client_name.Text + "/" + quote_id.Text);
              string targetPath = (pathToClient + "/" + client_name.Text + "/" + project_number.Text);

              moveDirectory(sourcePath, targetPath);






              share|improve this answer

























                0












                0








                0







                You need a recursive method to achieve moving directories including all files and sub-directories:



                private void moveDirectory(string sourcePath ,string targetPath)

                if (!System.IO.Directory.Exists(targetPath))

                System.IO.Directory.CreateDirectory(targetPath);

                String files = Directory.GetFiles(sourcePath);
                String directories = Directory.GetDirectories(sourcePath);
                foreach (string f in files)

                System.IO.File.Copy(f, Path.Combine(targetPath,Path.GetFileName(f)), true);

                foreach(string d in directories)

                // recursive call
                moveDirectory(Path.Combine(sourcePath, Path.GetFileName(d)), Path.Combine(targetPath, Path.GetFileName(d)));




                then the usage is like this:



                private void CopyPartsToProject()

                string sourcePath = (pathToQuotes + "/" + client_name.Text + "/" + quote_id.Text);
                string targetPath = (pathToClient + "/" + client_name.Text + "/" + project_number.Text);

                moveDirectory(sourcePath, targetPath);






                share|improve this answer













                You need a recursive method to achieve moving directories including all files and sub-directories:



                private void moveDirectory(string sourcePath ,string targetPath)

                if (!System.IO.Directory.Exists(targetPath))

                System.IO.Directory.CreateDirectory(targetPath);

                String files = Directory.GetFiles(sourcePath);
                String directories = Directory.GetDirectories(sourcePath);
                foreach (string f in files)

                System.IO.File.Copy(f, Path.Combine(targetPath,Path.GetFileName(f)), true);

                foreach(string d in directories)

                // recursive call
                moveDirectory(Path.Combine(sourcePath, Path.GetFileName(d)), Path.Combine(targetPath, Path.GetFileName(d)));




                then the usage is like this:



                private void CopyPartsToProject()

                string sourcePath = (pathToQuotes + "/" + client_name.Text + "/" + quote_id.Text);
                string targetPath = (pathToClient + "/" + client_name.Text + "/" + project_number.Text);

                moveDirectory(sourcePath, targetPath);







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 13 '18 at 20:21









                roozbeh Sroozbeh S

                8481411




                8481411





























                    draft saved

                    draft discarded
















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid


                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.

                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53288520%2fcreating-paths-with-variables-move-a-folder-and-contents-to-new-folder%23new-answer', 'question_page');

                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    How to how show current date and time by default on contact form 7 in WordPress without taking input from user in datetimepicker

                    Syphilis

                    Darth Vader #20