MS Dos command

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • akshaycjoshi
    New Member
    • Jan 2007
    • 153

    MS Dos command

    I had developed one application in which the user enters information about the registration of vehicles in c# the backend being MS Access 2003.

    Is there any MS Dos command that copies the file from one location to other and it does not copy the file if it is already present ???

    maybe there is some switch to perform this action.It should not ask the user for confirmation.

    Please explain with an example.
  • DonBytes
    New Member
    • Aug 2008
    • 25

    #2
    You could copy it with C# and set overwrite to false? or is that some scheduled task that runs beside your program?

    Comment

    • akshaycjoshi
      New Member
      • Jan 2007
      • 153

      #3
      using cmd for that.

      Not using c# claases for that.

      Comment

      • DonBytes
        New Member
        • Aug 2008
        • 25

        #4
        A bit overkill but as I am pretty much certain copy and xcopy can't do what you want here goes:

        Code:
        robocopy.exe . "C:\Destination Directory" Filename /XC
        In my example I assumed robocopy was in the path and executed in the folder where the file was. If you execute from another folder you'd have to:
        Code:
        robocopy.exe "C:Source Directory" "C:\Destination Directory" Filename /XC
        Robocopy is available from the Windows Server 2003 Resource Kit Tools downloadable here

        Edit for clarification: The switch /XC eXcludes Changed files thus only files not existing will be copied.
        Last edited by DonBytes; Aug 31 '08, 08:33 PM. Reason: Clarification

        Comment

        • akshaycjoshi
          New Member
          • Jan 2007
          • 153

          #5
          I dont want robocopy.
          Any other way to do it ?

          Or is there any way to ask for confirmation by using /-Y and then write something that will cause only "no" to be typed automatically ??


          Or is there any way to check for presence of a particular file in a directory?If yes then if i came to know that the file already exists i wont execute the copy command.

          Comment

          • Curtis Rutland
            Recognized Expert Specialist
            • Apr 2008
            • 3264

            #6
            Well, if you absolutely have to do this with shell commands, this might not be the proper forum, as this is for .NET programming questions.

            However, if you are only doing this through cmd.exe for convenience, there is a .NET namespace that provides a simple and elegant solution to your problem, System.IO.

            For example, if you have the following directory structure:
            c:\dev\a
            c:\dev\b
            And assume you have test.txt in the a directory, and don't know whether or not it already exists in the b directory, you can do something like this:

            Code:
            //at the top with the using statements:
            using System.IO;
            
            //later, where you want to make the move:
            string pathA = @"c:\dev\a\test.txt";
            string pathB = @"c:\dev\b\test.txt";
            FileInfo fi = new FileInfo(pathA);
            fi.CopyTo(pathB);
            .CopyTo("path") will copy but not overwrite an existing file. In other words, it will only copy to, not replace. If you wanted it to automatically replace the existing file, you can use this:
            Code:
            fi.CopyTo(pathB,true);
            Also, if you wish to test for the presence of the second file, you could use this code:
            Code:
            FileInfo fi_b = new FileInfo(pathB);
            bool exists = fi_b.Exists;
            So, if you can use a .NET solution for this, it makes the problem much easier. If you can't, I can move this thread to the Windows forum, where you will be more likely to get help on this topic.

            Let us know.

            Comment

            • akshaycjoshi
              New Member
              • Jan 2007
              • 153

              #7
              insertAlias,
              Ur approach is just fine but when the file is already present it is throwing an exception.
              What should i do ?

              Comment

              • Curtis Rutland
                Recognized Expert Specialist
                • Apr 2008
                • 3264

                #8
                Use the third code sample I gave you as part of an if statement to test if the file exists. If it does, move on. Otherwise, copy the file.

                Comment

                • akshaycjoshi
                  New Member
                  • Jan 2007
                  • 153

                  #9
                  Thanks insertAlias !

                  My problem is solved.May God bless you.

                  Comment

                  Working...