A
A
AlexandrMa2021-12-17 12:44:32
cmd/bat
AlexandrMa, 2021-12-17 12:44:32

How can I achieve the same result with for as with the dir command?

There is a perfectly working command dir /s /b
Displays folders and all files in the folder as a full path.

C:\myfolder\1\1.JPG
C:\myfolder\1\2.JPG
C:\myfolder\1\3.JPG
C:\myfolder\2\1.JPG
C:\myfolder\2\2.JPG
C:\myfolder\2\3.JPG


Some more data is needed, as a result of which we have to switch to the for command.
Nesting can be either 1 or 3 levels.

Question. How to display the same result as in the example, but only using for?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
R
res2001, 2021-12-17
@res2001

I would suggest stuffing dir into for /fand into for to process the output of dir, adding the necessary information:

for /f "tokens=* delims" %%a in ('dir /s /b *') in (
   rem Тут можно писать любой другой код. Имя текущего файла в %%a
   echo %%~a
)

W
wisgest, 2021-12-19
@wisgest

Help tried to read for /?:

Lists folders and all files in a folder as a full path.

Files - forwith the key /r. If subdirectories are needed, use the /d /r. In one list without intercepting the output of the command dir, most likely it is impossible.

O
Olgeir, 2021-12-20
@Olgeir

Guys, I warn against using multiline constructions in brackets
for /f "tokens=* delims" %%a in ('dir /s /b *') in (
there are many many lines
)
I got a number of glitches when using such a construction. At some point, the script completely broke down if you add an empty line inside ( ) or comment out one of those present.
Rewrote everything to subroutines and glitches are gone.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question