Batch file copy / move from directory to directory

Posted on 2007-10-18
Last Modified: 2012-08-13
Creating a batch file (DOS / Win cmd)  to move files from one directory to another.   The batch file needs to be able to limit the number of files moved by a count.  For example, when I run the batch file I want only 50 files moved from directory a to directory b.  The nex time I run the batch file it will move another 50 or less depending how many files are left in directory a.
Question by:faron
    LVL 30

    Expert Comment

    @echo off


    set srcDir=c:\DirB
    set destDir=c:\DirB
    set maxFiles=50

    pushd "%srcDir%"

    set cnt=0

    for /f "tokens=*" %%a in ('dir /b /a-d 2^>NUL') do call :MOVEFILE "%%a"


    goto :EOF


    set /a cnt+=1

    if /i %cnt% GEQ goto :EOF

    if exist "%destDir%\%~1" del /F "%destDir%\%~1"

    move "%~1" "%destDir%"

    goto :EOF

    Author Comment

    When I run this I get an error message "the syntax of  the command is incorrect"  It is refering to the line containing (I suspect).

    if /i %cnt% GEQ goto :EOF

    If I remove the line all the files get copied from the directory as it does not monitor the count.  I then get an error "the system cannot find the batch label specific -EOFBack"  I get this message once for every file copied.
    LVL 9

    Accepted Solution

    a vbscript solution

    Set objFSO=CreateObject("Scripting.FileSystemObject")
    Set wshArgs = WScript.Arguments
    i = wshArgs(0)
    Set objFolder = objFSO.GetFolder(strFolder)
    iNum = objFolder.Files.Count
    For Each strFile In objFolder.Files
              objFSO.MoveFile strFile,"c:\temp1\"&strFile.Name            
              If Int(i) = j Then         
                      Exit For
              End If
    usage: c:\> cscript /nologo myscript.vbs 50

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Join & Write a Comment

    When you receive another warning that your shared drive is almost full and you have asked your users to clean out old files again and again, here is a single command that may help. This command will place all the files that have not been used rec…
    Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
    Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
    Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

    746 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    20 Experts available now in Live!

    Get 1:1 Help Now