How to recursively list all files of desired file type in a specified directory?

efirvida

I want list all .java files in the desired directory using .bat script in Windows. When I run this:

set JMETALHOME=%cd%
dir %JMETALHOME%\jmetal\problems /S /B 

it lists all the files in the problems directory. But when I want to filter the files only to the java files:

set JMETALHOME=%cd%
dir %JMETALHOME%\jmetal\problems /S /B *.java > sources.txt

it lists all the .java files in the %JMETALHOME% folder.

Mofi

There are two common methods to get names of all files with a specific file extension found in a directory tree recursively with full path. One common method is using command DIR and the other common method is using command FOR.

Example code demonstrating both methods for *.java and *.html files:

@echo off
set "JMETALHOME=%CD%"

dir "%JMETALHOME%\jmetal\problems\*.java" "%JMETALHOME%\jmetal\problems\*.html" /B /S >SourcesDir.txt

if exist SourcesFor1.txt del SourcesFor1.txt
for /R "%JMETALHOME%\jmetal\problems" %%I in (*.java *.html) do echo %%I>>SourcesFor1.txt

(for /R "%JMETALHOME%\jmetal\problems" %%I in (*.java *.html) do echo %%I)>SourcesFor2.txt

DIR requires the specification of 1 or even more wildcard patterns together with path (absolute or relative).

FOR offers the possibility to specify the path separately from the wildcard patterns.

FOR outputs the found file names line by line which requires either usage of >> redirection operator with an extra deletion of a perhaps already existing output file before running FOR as demonstrated above with SourcesFor1.txt example, or embedding FOR itself in a block and use > to redirect everything output by the block into a file overwriting a perhaps already existing file as demonstrated with SourcesFor2.txt example.

On second FOR example make sure not having a space between echo %%I and closing parenthesis ) of the block because otherwise this space would be appended on each file name written into output file SourcesFor2.txt.

For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.

  • del /?
  • dir /?
  • echo /?
  • for /?
  • if /?
  • set /?

Read also the Microsoft article about Using command redirection operators.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to list all files in named directory, file type included?

From Dev

Recursively list all files in directory (Unix)

From Dev

Recursively list all files in directory (Unix)

From Dev

How to make a specified directory to an archive file into a desired location?

From Dev

How to recursively remove all but list of files?

From Dev

How to recursively list all hidden files and directories?

From Dev

Apply Linux command recursively to all files in directory of single (.sh) type

From Dev

How can I recursively list Md5sum of all the files in a directory and its subdirectories?

From Dev

How to copy a directory with only specified type of files?

From Dev

How to copy all files by specified extension to another location recursively

From Dev

How to find files recursively by file type and copy them to a directory while in ssh?

From Dev

How to copy all files in directory recursively and unzip compressed files on fly

From Dev

Copying saved files from a List<File> into a directory recursively in java

From Dev

how to search for a line match from all files recursively found in a directory

From Dev

How to search and remove ^M in all files in a directory recursively?

From Dev

How do I copy all files recursively to a flat directory in Ruby?

From Dev

How to use grep on all files non-recursively in a directory?

From Dev

How can I move all files in subdirectories recursively to a single directory?

From Dev

How to use grep on all files non-recursively in a directory?

From Dev

How can I move all files in subdirectories recursively to a single directory?

From Dev

How to find all files that contain the + sign on a directory recursively?

From Dev

How do I gunzip all files recursively in a target directory?

From Dev

How to list the last modified files in a specific directory recursively

From Dev

How to find all files except a specified file

From Dev

How can I find, recursively, all files in my directory tree that first character in first line of each file is a space, a tab or a line break?

From Dev

How can we list all files and folders recursively?

From Dev

How to list all files from the resources directory

From Dev

How to retrieve a list of all files in the Working Directory

From Dev

Android: How to list all the files in a directory in to an array?

Related Related

  1. 1

    How to list all files in named directory, file type included?

  2. 2

    Recursively list all files in directory (Unix)

  3. 3

    Recursively list all files in directory (Unix)

  4. 4

    How to make a specified directory to an archive file into a desired location?

  5. 5

    How to recursively remove all but list of files?

  6. 6

    How to recursively list all hidden files and directories?

  7. 7

    Apply Linux command recursively to all files in directory of single (.sh) type

  8. 8

    How can I recursively list Md5sum of all the files in a directory and its subdirectories?

  9. 9

    How to copy a directory with only specified type of files?

  10. 10

    How to copy all files by specified extension to another location recursively

  11. 11

    How to find files recursively by file type and copy them to a directory while in ssh?

  12. 12

    How to copy all files in directory recursively and unzip compressed files on fly

  13. 13

    Copying saved files from a List<File> into a directory recursively in java

  14. 14

    how to search for a line match from all files recursively found in a directory

  15. 15

    How to search and remove ^M in all files in a directory recursively?

  16. 16

    How do I copy all files recursively to a flat directory in Ruby?

  17. 17

    How to use grep on all files non-recursively in a directory?

  18. 18

    How can I move all files in subdirectories recursively to a single directory?

  19. 19

    How to use grep on all files non-recursively in a directory?

  20. 20

    How can I move all files in subdirectories recursively to a single directory?

  21. 21

    How to find all files that contain the + sign on a directory recursively?

  22. 22

    How do I gunzip all files recursively in a target directory?

  23. 23

    How to list the last modified files in a specific directory recursively

  24. 24

    How to find all files except a specified file

  25. 25

    How can I find, recursively, all files in my directory tree that first character in first line of each file is a space, a tab or a line break?

  26. 26

    How can we list all files and folders recursively?

  27. 27

    How to list all files from the resources directory

  28. 28

    How to retrieve a list of all files in the Working Directory

  29. 29

    Android: How to list all the files in a directory in to an array?

HotTag

Archive