Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2014-02-26
8
Medium Priority
?
764 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 57

Expert Comment

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

~bp
0
 
LVL 57

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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 57

Accepted Solution

by:
Bill Prew earned 80 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 57

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

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

The password reset disk is often mentioned as the best solution to deal with the lost Windows password problem. In Windows 2008, 7, Vista and XP, a password reset disk can be easily created. But besides Windows 7/Vista/XP, Windows Server 2008 and ot…
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…
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…
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). …

670 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