Solved

Windows command line command to move a fixed number of files from all subdirectories

Posted on 2014-02-26
8
691 Views
Last Modified: 2014-06-11
I would like a Windows batch file for DOS (not Powershell)  to move JUST 100 files from folder S:\SOURCE to folder D:\Destination INCLUDING files WITHIN SUBDIRECTORIES (duplicate filenames not moved).  

I want the maximum number of files moved to be 100.
0
Comment
Question by:wind-surfer
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
8 Comments
 
LVL 56

Expert Comment

by:Bill Prew
ID: 39890712
Does it matter at all which hundred?

~bp
0
 
LVL 56

Expert Comment

by:Bill Prew
ID: 39890748
Give this a try:

@echo off
setlocal EnableDelayedExpansion

set BaseDir=S:\Source
set DestDir=D:\Destination
set MaxFiles=100

set Count=0
for /f "tokens=*" %%A in ('dir /a-d /b /s "%BaseDir%\*.*") do (
  if not exist "%DestDir%\%%~nxA" (
    set /a Count+=1
    if !Count! GTR %MaxFiles% goto :Done
    move "%%~A" "%DestDir%"
  )
)

:Done

Open in new window

~bp
0
 

Author Comment

by:wind-surfer
ID: 39890894
Bill,

It doesn't matter which hundred are moved.

Also, it seems that there is a syntax error in your code.

Thank you.
0
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 
LVL 56

Accepted Solution

by:
Bill Prew earned 20 total points
ID: 39891826
Sorry, looks like I forgot a trailing single quote on the FOR line.  Try this:

@echo off
setlocal EnableDelayedExpansion

set BaseDir=S:\Source
set DestDir=D:\Destination
set MaxFiles=100

set Count=0
for /f "tokens=*" %%A in ('dir /a-d /b /s "%BaseDir%\*.*"') do (
  if not exist "%DestDir%\%%~nxA" (
    set /a Count+=1
    if !Count! GTR %MaxFiles% goto :Done
    move "%%~A" "%DestDir%"
  )
)

:Done

Open in new window

~bp
0
 

Author Comment

by:wind-surfer
ID: 39893693
Bill,

The code just hangs. No files are moved. Is there another error in it?

Thanks.
0
 
LVL 56

Expert Comment

by:Bill Prew
ID: 39893705
I tested it here and it did work fine.  How many files are under the base folder, it can take a while to locate all the files before it starts moving the first 100.

~bp
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

My purpose is to describe the basic concepts of virtual memory as implemented in a modern Windows-based operating system. I will also describe the problems inherent in older systems and how virtual memory solves them. The dark ages - before virtu…
Many times while working on a computer regardless of any Operating System, lag and crashes seem to creep in, hindering your working speed. Sometimes, it can also cause your work to be lost unexpectedly and as a result, you are unable to meet your de…
Windows 8 comes with a dramatically different user interface known as Metro. Notably missing from the new interface is a Start button and Start Menu. Many users do not like it, much preferring the interface of earlier versions — Windows 7, Windows X…
With the advent of Windows 10, Microsoft is pushing a Get Windows 10 icon into the notification area (system tray) of qualifying computers. There are many reasons for wanting to remove this icon. This two-part Experts Exchange video Micro Tutorial s…

632 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