?
Solved

Batch file copy / move from directory to directory

Posted on 2007-10-18
3
Medium Priority
?
3,284 Views
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.
0
Comment
Question by:faron
3 Comments
 
LVL 30

Expert Comment

by:SteveGTR
ID: 20106276
@echo off

setlocal

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"

popd

goto :EOF

:MOVEFILE

set /a cnt+=1

if /i %cnt% GEQ goto :EOF

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

move "%~1" "%destDir%"

goto :EOF
0
 

Author Comment

by:faron
ID: 20106500
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.
0
 
LVL 9

Accepted Solution

by:
ghostdog74 earned 500 total points
ID: 20106530
a vbscript solution

Set objFSO=CreateObject("Scripting.FileSystemObject")
Set wshArgs = WScript.Arguments
i = wshArgs(0)
j=1
strFolder="C:\temp"
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
          j=j+1
Next
usage: c:\> cscript /nologo myscript.vbs 50
0

Featured Post

NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
YESTERDAY YESTERDAY.BAT is inspired by a previous article I wrote entitled: TOMORROW.BAT (http://www.experts-exchange.com/OS/Microsoft_Operating_Systems/MS_DOS/A_4196-Advanced-Batch-File-Programming-TOMORROW-BAT.html). The crux of this batch f…
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…

807 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