Results 1 to 4 of 4

Thread: [RESOLVED] For Loop file filter in a .bat or .cmd file

  1. #1

    Thread Starter
    Fanatic Member aconybeare's Avatar
    Join Date
    Oct 2001
    Location
    UK
    Posts
    772

    Resolved [RESOLVED] For Loop file filter in a .bat or .cmd file

    Hi,

    I'm not sure if this is the best place to post this question so please forgive me if it's not.

    I've got a for loop in a batch file that loops ".jpg" files and I'd like to change it to find ".jpg" and ".pdf" files.

    Is this possible?

    Code:
    SET counter=0
    FOR /r "e:\test\myfiles\" %%X IN (*.jpg) DO (set /a counter+=1)
    
    ECHO Copied %counter% of 5 file(s)
    Any help or ideas will be greatly appreciated.

    Regards Al

  2. #2
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: For Loop file filter in a .bat or .cmd file

    Presuming what you have works, why not just add another FOR with a another counter variable
    Code:
    SET counter=0
    SET pdfCounter = 0
    FOR /r "e:\test\myfiles\" %%X IN (*.jpg) DO (set /a counter+=1)
    FOR /r "e:\test\myfiles\" %%X IN (*.pdf) DO (set /a pdfcounter+=1)
    
    ECHO Copied %counter% of 5 file(s)
    ECHO Copied %pdfcounter% of 5 file(s)

  3. #3

    Thread Starter
    Fanatic Member aconybeare's Avatar
    Join Date
    Oct 2001
    Location
    UK
    Posts
    772

    Re: For Loop file filter in a .bat or .cmd file

    As a workaround I've applied the following which works but I was hoping for a cleaner solution.

    Code:
    SET counter=0
    FOR /r "e:\test\myfiles\" %%X IN (*.jpg) DO (set /a counter+=1)
    FOR /r "e:\test\myfiles\" %%X IN (*.pdf) DO (set /a counter+=1)
    
    ECHO Copied %counter% file(s)

  4. #4

    Thread Starter
    Fanatic Member aconybeare's Avatar
    Join Date
    Oct 2001
    Location
    UK
    Posts
    772

    Re: For Loop file filter in a .bat or .cmd file

    Hack,

    Snap you suggested pretty much what I've managed to figure out. I must have started typing out my reply just before you posted yours.

    Many thanks

    Cheers Al

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width