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

Author Topic: Help me in my Batch Script  (Read 3781 times)

0 Members and 1 Guest are viewing this topic.

akki15623

    Topic Starter


    Rookie

    Help me in my Batch Script
    « on: March 12, 2010, 03:25:22 PM »
    I have a batch script which copies the targeted files from drives, for example if I want all .doc to be copied from C Drive it copies all, the thing is I need it to copy 20, or 10 files not full .doc from C drive.

    Can anyone help me in this please.

    Here is the code for the script.
    Code: [Select]
    @echo off
    SETLOCAL EnableDelayedExpansion
    set destfolder=D:\copiedjpegs
    set searchdrive="C:\"
    for /f "tokens=*" %%P in ('dir %searchdrive%*.doc /s /b') do copy "%%P" %Desktop%

    greg



      Intermediate

      Thanked: 7
      Re: Help me in my Batch Script
      « Reply #1 on: March 12, 2010, 05:24:21 PM »
      . . . for example if I want all .doc to be copied from C Drive it copies all, the thing is I need it to copy 20, or 10 files not full .doc from C drive.


      C:\batch>type akki.bat
      Code: [Select]
      @echo off
      setLocal EnableDelayedExpansion
      set /a c=0
      set destfolder=E:\copydoc\

      for /f "delims==" %%i in ('dir c:\*.doc /s /b') do (
      echo %%~nxi
      set /a c+=1

      echo c=!c!
      if !c! GTR 9  goto  end

      copy "%%i" %destfolder%
      )
      :end

      Output:

      C:\batch>akki.bat
      longshot.doc
      c=1
              1 file(s) copied.
      longshot.doc
      c=2
              1 file(s) copied.
      ls.doc
      c=3
              1 file(s) copied.
      winword.doc
      c=4
              1 file(s) copied.
      winword2.doc
      c=5
              1 file(s) copied.
      fractions.doc
      c=6
              1 file(s) copied.
      third.doc
      c=7
              1 file(s) copied.
      twothird.doc
      c=8
              1 file(s) copied.
      What is My IP Address.doc
      c=9
              1 file(s) copied.
      aarp081009.doc
      c=10
      C:\batch>


      E:\copydoc>dir
       
       Directory of E:\copydoc

      03/12/2010  06:18 PM    <DIR>          .
      03/12/2010  06:18 PM    <DIR>          ..
      10/23/2009  09:25 PM            20,480 fractions.doc
      03/06/2005  09:59 PM             4,195 longshot.doc
      03/06/2005  09:59 PM             4,195 ls.doc
      10/23/2009  08:44 PM            24,576 third.doc
      10/23/2009  08:45 PM            24,576 twothird.doc
      10/20/2009  11:27 AM            24,064 What is My IP Address.doc
      04/14/2008  06:00 AM             4,608 winword.doc
      04/14/2008  06:00 AM             1,769 winword2.doc
                     8 File(s)        108,463 bytes
                     2 Dir(s)  139,440,300,032 bytes free

      E:\copydoc>
      Have a Nice Day

      akki15623

        Topic Starter


        Rookie

        Re: Help me in my Batch Script
        « Reply #2 on: March 12, 2010, 05:33:24 PM »

        C:\batch>type akki.bat
        Code: [Select]
        @echo off
        setLocal EnableDelayedExpansion
        set /a c=0
        set destfolder=E:\copydoc\

        for /f "delims==" %%i in ('dir c:\*.doc /s /b') do (
        echo %%~nxi
        set /a c+=1

        echo c=!c!
        if !c! GTR 9  goto  end

        copy "%%i" %destfolder%
        )
        :end


        Thanks Greg, you saved my day. ;)