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

Author Topic: Batch file to copy files but not copy the batch file?  (Read 3956 times)

0 Members and 1 Guest are viewing this topic.

jez251

    Topic Starter


    Starter

    • Experience: Familiar
    • OS: Windows 7
    Batch file to copy files but not copy the batch file?
    « on: September 16, 2014, 12:42:20 PM »
    Hi,

    I have a very simple batch file script to copy files in the current folder to another folder, but it also copies the actual .bat file. How can I exclude the .bat file from also being copied?

    here is what I have so far:

    copy /-Y *.xml F:\\Test
    Exit

    I tried this:

    except *.bat copy /-Y *.xml F:\\Test
    Exit

    with and without brackets but it does not work.

    Any help would be appreciated.
    Jaime



    Geek-9pm


      Mastermind
    • Geek After Dark
    • Thanked: 1026
      • Gekk9pm bnlog
    • Certifications: List
    • Computer: Specs
    • Experience: Expert
    • OS: Windows 10
    Re: Batch file to copy files but not copy the batch file?
    « Reply #1 on: September 16, 2014, 01:04:07 PM »
    The simple way is to have the batch file itself in another directory.
    Let's say your stuff in in D:STUFF
    and the bat file name MYBAT.BAT is in D:\BAT
    then  go into D:STUFF directory  and give the command
    d:\bat\mybat
    Which will only operate on files in the current directory and sub directories.



    jez251

      Topic Starter


      Starter

      • Experience: Familiar
      • OS: Windows 7
      Re: Batch file to copy files but not copy the batch file?
      « Reply #2 on: September 16, 2014, 01:14:07 PM »
      Sounds like a good workaround, but I would like for it to send files from the current dir. rather than pull files from the current dir.

      The target folder is a watched folder where all files in it are pulled into a translations program and automatically added to a translation workflow project. So I do want to avoid adding any files to the watched folder that do not belong there.

      Jaime

      Salmon Trout

      • Guest
      Re: Batch file to copy files but not copy the batch file?
      « Reply #3 on: September 16, 2014, 01:52:25 PM »
      If you use the copy command with the *.xml filemask then only files with extension .xml will be copied, and files with the extension .bat (or any other extension except .xml) will be ignored. Check your code.



      jez251

        Topic Starter


        Starter

        • Experience: Familiar
        • OS: Windows 7
        Re: Batch file to copy files but not copy the batch file?
        « Reply #4 on: September 16, 2014, 02:58:31 PM »
        Yes, sorry, I meant to write *.* so as to copy all files in the directory.

        Thanks,
        Jaime

        Salmon Trout

        • Guest
        Re: Batch file to copy files but not copy the batch file?
        « Reply #5 on: September 16, 2014, 03:52:22 PM »
        You can use a FOR loop and DIR to list the files to copy and skip the batch file's own name (which is %0) (that is a ZERO)

        Code: [Select]
        @echo off
        for /f "delims=" %%A in ('dir /b /a-d') do if not "%%A"=="%0" copy /-Y "%%A" F:\\Test