Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: Batch file for moving files  (Read 5580 times)

0 Members and 1 Guest are viewing this topic.

roxorz

    Topic Starter


    Starter
    Batch file for moving files
    « on: August 12, 2010, 09:08:24 PM »
    Hello all,

    I am trying to set up a batch file that will move all files from a directory and it's sub-directories from one pc to a shared server drive (I:\). I was thinking a simple move command would be easy enough but the problem is that there are instances where a file being moved may already exist. If the source file and destination file have the same file name I want the batch program to skip it and move on the next one. I'm not familiar with the way to code this. Any help would be greatly appreciated.

    To be more specific I have files on the user's desktop: C:\user\desktop\temp. The temp folder will have sub-directories with image files. I want all files moved regardless of extension but need to have the directory structure preserved at the destination I:\.

    There is a program already in place that will sweep files out of the I:\ drive and archive them but the files being moved from the temp folder may have the same file name. I will have the batch file set up to run at log off so the user will have the images automatically sent for archiving but want to make sure that duplicates are left in the temp folder and can just be swept the next day when the file with the same name has been deleted and the batch runs again.

    I know this is fairly simple but I'm stumped as to make it ignore the file completely. Since I'm having it run at log off I can't have the batch file prompt the user if it's ok to overwrite and having to keep inputting no.

    Again any insight would be very helpful.
    What color does a smurf turn when you choke it?

    DaveLembke



      Sage
    • Thanked: 662
    • Certifications: List
    • Computer: Specs
    • Experience: Expert
    • OS: Windows 10
    Re: Batch file for moving files
    « Reply #1 on: August 21, 2010, 02:15:29 AM »
    Curious as to why you are using MOVE command and not just an XCOPY /s/d/y instruction?

    roxorz

      Topic Starter


      Starter
      Re: Batch file for moving files
      « Reply #2 on: August 21, 2010, 03:05:04 PM »
      I originally thought XCOPY would do the trick but I want the files moved instead of copied. XCOPY will leave the originals and I'd end up with too many files and would have to figure out a way to get rid of all the files that XCOPY successfully copied and leaving the ones that didn't. Unfortunately MOVE doesn't have a way to leave the original file alone if the file exists in the destination folder. I can have it overwrite automatically but I can't just have it say no without user input. Maybe I just don't know how XCOPY and MOVE work exactly and since I don't have alot of batch file experience I figured I'd look around for pointers.
      What color does a smurf turn when you choke it?

      roxorz

        Topic Starter


        Starter
        Re: Batch file for moving files
        « Reply #3 on: August 25, 2010, 07:29:52 PM »
        Ok I think I have half a solution so far. I did a lot of looking around and found that you could actually pipe the "n" to the move command so that it will not overwrite files in the destination folder if they already exist.

        Kinda looks like this:

        echo n|move /-y "D:\TEMP\SOURCE\*.*" "D:\TEMP\DESTINATION"

        I knew it was something easy I was just overlooking but I didn't expect it to be that simple. ::)

        My problem now is that Move only works on files and not folders so my sub-directories aren't being touched.

        If anyone knows a way of making that work I'd be grateful.
        If not I'll just have to spend some extra time adding move commands for each and every sub-directory (and there's quite a few).
        What color does a smurf turn when you choke it?

        MattPwns



          Rookie

          Thanked: 1
          Re: Batch file for moving files
          « Reply #4 on: August 25, 2010, 07:49:27 PM »
          This should work, haven't tested it though.

          Code: [Select]
          @ECHO OFF

          FOR /f "tokens=* delims=" %%a IN ('DIR /b') DO (
          COPY \-y %%a "P:\a\t\h" >
          DEL /q %%a
          )

          Let me know!