• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 885
  • Last Modified:

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

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
wind-surfer
Asked:
wind-surfer
  • 4
  • 2
1 Solution
 
Bill PrewCommented:
Does it matter at all which hundred?

~bp
0
 
Bill PrewCommented:
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
 
wind-surferAuthor Commented:
Bill,

It doesn't matter which hundred are moved.

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

Thank you.
0
Industry Leaders: 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!

 
Bill PrewCommented:
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
 
wind-surferAuthor Commented:
Bill,

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

Thanks.
0
 
Bill PrewCommented:
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's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now