Home > Software engineering >  How to list the names of all the files and directories in a folder using for loop in a batch file
How to list the names of all the files and directories in a folder using for loop in a batch file

Time:12-15

I want to list all the files and directories inside a directory using a for loop in a batch script. How can I do it?

I used below but it didn't work :

for /r %%I in (".") do ( ls -ltr '%%I') ## Listing only filenames and not directories name

Any help is appreciable.

Thanks!

CodePudding user response:

If you just want a list of dirs and files, recursively, what about:

dir /b/s "."

If you want to do something special with each of the stream item, using a for loop, you could do something like:

for /f "tokens=* delims=" %%i in ('dir /b/s "."') do ( echo "%%i" )

There I used echo for echoing, but you can put whatever you need.

CodePudding user response:

Create two loops, one for files

for /r %%i in (*.*) do <something>

and one for directories

for /r %%i in (.) do <something>

and use the same command after do

But, since you have Cygwin installed anyway, why not use that power and do

find . | xargs -L1 ls -ltr

where find . finds all files and directories, | xargs passes the output to xargs which -L1 splits the output after each line and passes each line to ls -ltr.

CodePudding user response:

"to list all the files and directories inside a directory using a for loop in a batch script." you should use the DIR command.

If you open a Command Prompt window, type dir /? and press the ENTER key you should see its usage information.

One important thing to note is the /A option. What is not mentioned specifically is that using it alone, (without additional parameters D, R, H, A, S, I, L or O), enables all attributes.

Therefore to list all items in the current directory recursively in bare format you'd use:

DIR /A /B /S

or

DIR . /A /B /S

If you wanted to list them in a specific location relative to the current directory, you'd use:

DIR "Location" /A /B /S

or:

DIR ".\Location" /A /B /S

And For a specific absolute path:

DIR "L:\ocation" /A /B /S

And if you wanted it to be in the same location as the batch file itself, you can use the special variable for the current script %0:

DIR "%~dp0." /A /B /S

To perform that command within a For loop, you should first open a Command Prompt window, type for /? and press the ENTER key, to read its usage information.

You should note that you are running a command, and should therefore use a FOR /F loop, i.e.

FOR /F ["options"] %variable IN ('command') DO command [command-parameters]

But should also note that:

To use the FOR command in a batch program, specify %%variable instead of %variable.

So:

FOR /F ["options"] %%variable IN ('command') DO command [command-parameters]

As you have your command already, the options now become important. The first you need to understand is eol which whilst it seems to mean End Of Line, is specific to only one end, the beginning! What this does it does not pass any result of 'command' to the DO if it begins with a single specific character. The defualt for eol is the semicolon ;, (probably because historically it was a common line comment marker in many files). Generally, a file or directory name could include, and begin with a semicolon, so in order to include all files, you would specify a character which cannot be included in a filename, for me the simplest is ?, although I've seen many examples using |. However, when you perform a recursive DIR command, every returned line is a fully qualified path, none of which can begin with a semicolon, so you can for this task ignore eol. You clearly want everything returned, so do not require skip any lines returned. tokens and delimiters, are adjusted according to what you want to do with the results, in this case, you want the entire content of each line returned by your 'command' with no splitting on specific characters. You should note that tokens by default is 1 and delims by default is both the space and a horizontal tab characters. You should stipulate therefore that you do not want any delimiters, so that the first token is everything returned on each line of 'command'. You rarely require the usebackq option, so for the purposes of this answer, and your task, just ignore it.

Now put it all together:

FOR /F "delims=" %%G IN ('DIR "Location" /A /B /S') DO command

Finally you can use your wanted DO command with each result from your parenthesized DIR command. That result will be held within your variable %%G.

For the purposes of just viewing each result, we'll use the ECHO command, (you would just replace that with your chosen command). Please note that as each result of the DIR command is a file or directory name string, you should generally doublequote it.

allObjects.cmd

FOR /F "delims=" %%G IN ('DIR "Location" /A /B /S') DO ECHO "%%G"

Please remember to replace "Location" as needed, before running the Windows Command Script

  • Related