Bath file question

March 25, 2010 at 05:15:19
Specs: Windows xp
Ok I have a siy question about a DOS Batch file I was hoping someone could help me with.

I want to make a batch file that looks at a folder and runs a command on say the first 20 files in that folder.

Then I want another batch file that will run a command on files 21-40 of that folder.

See More: Bath file question

Report •

March 25, 2010 at 06:50:27
Yeah, that's pretty simple ... I guess a simple "FOR /F" will contain the needed features for that.

You do need to specify how to sort these files ... the first 20 oldest files ? The first 20 files in alphabetical order ? ...

Report •

March 25, 2010 at 06:55:52
alphabetic order is perfect

Report •

March 25, 2010 at 20:11:33
this just does "dir", which is useless, so put whatever you need into %comd%:
(not tested)
@echo off & setlocal enabledelayedexpansion
set comd=dir
set /a nbr=20
set /a loops=2
set /a test=loops*nbr
set /a n1=0
set /a n2=n1+nbr
set /a cc=n1
for /f %%a in ('dir /o:n /b '*.*') do (
set /a cc+=1
if !cc! gtr !n2! goto :xit
if !cc! gtr !n1! (
!comd! %%a
set /a n1+=nbr
if !n1! lss test goto :aa

Report •

Related Solutions

March 26, 2010 at 00:28:50
I see this will do the first 20 files
what about the second batch file that will do files 21-40. How does the batch file know to skip the first 20?

Report •

March 26, 2010 at 01:19:30
yeah, i kinda dropped off without explain.
the idea is to change %comd% as needed.
%comd% can be either another batchfile or an executable.
f/e: pre-loop: set comd=bat1
lets say:
set comd20=bat2
:: set comd40=bat3...

either way, just change it at each loop-bottom:
set /a n1+=nbr
:: foll sets comd to: comd20, comd40...
set comd=%comd%%n1%
if !n1! lss test goto :aa
ie: n1 advances 20 for each big-loop, and you want a different
batchfile for each big-loop, make batches or batchname-array
as(set %comd% to): bat20, bat40, bat60 and call %comd%.
kinda hard to explicate an i'm real tired, but i will try to help when the other shoe drops...

Report •

March 26, 2010 at 02:31:12
Ok I tried that but all I got were a bunch of errors
Lets just say for this example
i have a folder on my C: drive called test with 40 files in it
I want the batch file to go to that folder, and copy the first 20 files in that folder to another folder on c: caled test1 and then the second bacth file that will open the original test folder and copy files 21-40 into another folder called test 2.

Report •

March 26, 2010 at 11:04:24
might try this then:
@echo off & setlocal
:: put whatever the source directory is here:
set dir="c:\work\temp"
set max=2
set block=20
set xx=%1
if "xx"=="" (
set /a xx=0
dir /b %dir% *.* > listfils.txt
set /a ff=xx+1
set targ="c:\test"%ff%
md %targ% 2>nul
:: i think this is right, if not, fall back to: set /a xx=xx*block
set /a xx*=block
set /a cc=0
set /a n2=xx+block
for /f %%a in (listfils.txt) do (
set /a cc+=1
if cc gtr n2 goto :xit
if cc gtr xx copy %%a %targ%
goto :eof
if %ff% lss %max% call %0 %ff%
::------ end ------

Report •

March 26, 2010 at 19:45:38
you can store those files into arrays. then go through them as desired. if you can download and use gawk for windows (see my sig)

 cmd="dir /a-d /b"
 while( cmd | getline d ){
   cmd="firstbat.bat "dirs[i]
   cmd="secondbat.bat "dirs[i]

save the above as mydir.awk and on command line

c:\test> gawk -f mydir.awk

GNU win32 packages | Gawk| Report •

Ask Question