Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

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

Expert Comment

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

~bp
0
 
LVL 59

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
Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

 
LVL 59

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 59

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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

Many admins will agree: WSUS is is a nice invention but using it on the client side when updating a newly installed computer is still time consuming as you have to do several reboots and furthermore, the procedure of installing updates, rebooting an…
This is a little timesaver I have been using for setting up Microsoft Small Business Server (SBS) in the simplest possible way. It may not be appropriate for every customer. However, when you get a situation where the person who owns the server is i…
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). …
Windows 8 came with a dramatically different user interface known as Metro. Notably missing from that interface was a Start button and Start Menu. Microsoft responded to negative user feedback of the Metro interface, bringing back the Start button a…

971 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