Move an exact quantity of files

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
jhennig
Posts: 7
Joined: 31 Aug 2009 17:48

Move an exact quantity of files

#1 Post by jhennig » 31 Aug 2009 18:12

I've researched this for days and cannot resolve this issue and thought I'd ask for some expert advice.

I need to move a quantity of PDF files from one directory to another. They are unrelated as to file name and I must move EXACTLY 8 at a time. What files or the order in which they are moved does not matter as long as exactly 8 are moved in each "batch". The source directory is in constant flux so I figure I'll have to have it loop.

I also need to rename the files as they're being moved to a common name and add a sequence number to each one. Again it doesn't matter what the order is as long as it's sequential 1-8 (e.g. file_1.pdf, file_2.pdf, file_3.pdf, etc.). The name can be the same for each subsequent batch too.

I was thinking about something to move the 8 oldest than "now", but it's beyond my skills. It's selecting the number (quantity) of files that's killing me.

Any advice is greatly appreciated.

DccD
Posts: 23
Joined: 26 Aug 2009 19:34

#2 Post by DccD » 31 Aug 2009 18:53

I'm not sure if this complies with all your needs but it should get close to it :)

Code: Select all

@echo off

set "sourcedir=C:\source"
set "destdir=C:\dest"

cd /d "%sourcedir%"
set counter=0
set numb=
for %%a in (*.pdf) do call :loop "%%~a"
goto :eof

:loop
set /A counter+=1
if %counter% == 9 exit
if exist "%destdir%\file_%numb%%counter%.pdf" set /A numb+=1
move %1 "%destdir%\file_%numb%%counter%.pdf"
goto :eof

jhennig
Posts: 7
Joined: 31 Aug 2009 17:48

#3 Post by jhennig » 31 Aug 2009 19:35

This is great. Thanks DCCD!

I'm trying to refine it a bit because it will take any value below 8 files. My test directory had 11 files. It moved 8 and renamed them perfectly. But when i ran it a second time, it move the remaining three. I need it to exit if there aren't exactly 8.

I tried using an IF NOT, but it breaks the script, perhaps because of the loop?

I changed:

if %counter% == 9 exit

to:

if not %counter% == 8 exit

Can you offer a suggestion as to why it's not working there?

Thanks so much for your help.

DccD
Posts: 23
Joined: 26 Aug 2009 19:34

#4 Post by DccD » 31 Aug 2009 21:31

Ok this should work now:

Code: Select all

@echo off

set "sourcedir=C:\source"
set "destdir=C:\dest"
cd /d "%sourcedir%"

:: Count the number of PDF files
set numbfile=0
FOR /F "tokens=*" %%A IN ('dir /b *.pdf 2^>nul') do (set /A numbfile+=1)
if %numbfile% LSS 8 exit

:: Start the process
set counter=0
set numb=
for %%a in (*.pdf) do call :loop "%%~a"
goto :eof

:loop
set /A counter+=1
if %counter% == 9 exit
if exist "%destdir%\file_%numb%%counter%.pdf" set /A numb+=1
move %1 "%destdir%\file_%numb%%counter%.pdf"
goto :eof

jhennig
Posts: 7
Joined: 31 Aug 2009 17:48

#5 Post by jhennig » 01 Sep 2009 03:59

:o !

Perfect! Thanks so much for your help with this.

Best Regards.

Post Reply