Solved

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

Posted on 2014-02-26
8
588 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
  • 4
  • 2
8 Comments
 
LVL 54

Expert Comment

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

~bp
0
 
LVL 54

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
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
LVL 54

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 54

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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

The way I use Experts Exchange to assist me in analyzing and diagnosing a problem is I first enter a Verbose Question at Experts Exchange like: Office 2007 will hang when opening and saving files I then launch WordPad (any text editor will do) an…
When you upgrade from Windows 8 to 8.1 or to Windows 10 or if you are like me you are on the Insider Program you may find yourself with many 450MB recovery partitions.  With a traditional disk that may not be a problem but with relatively smaller SS…
In this video, we discuss why the need for additional vertical screen space has become more important in recent years, namely, due to the transition in the marketplace of 4x3 computer screens to 16x9 and 16x10 screens (so-called widescreen format). …
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…

820 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