matandked
matandked

Reputation: 1565

Windows CMD - loops for subfolders

I know that over whole Internet there was many topics about windows command line, but it's still not enough for me. Although I haven't got any problems when I'm writing loops in real programming language like Python to check all subfolders, I am unable to do it under windows CMD. I'm really annoyed (and really sorry if I am annoying you).

I tried both /r and /d switches, but I can't accomplish simply operation - get a list of files in subfolders:

for /d %%p in ("pathHERE*.jpg") DO echo %%p

I get an error message:

%%p was unexpected at this time.

Could you tell me where I can find WORKING (over Internet I found many script which I am unable to run - I am aware that there was some differencies between batch files and commands manually typed from keyboard) examples of operations such as copying all files with specified extension from all subfolders to another location or prepare list of all subfolders?

Upvotes: 0

Views: 1211

Answers (2)

OSH
OSH

Reputation: 2937

if you are running this line from a cmd window you should change to:

for /d %p in ("pathHERE*.jpg") DO echo %p

if you want to place this line in a batch file (x.bat) and run that file you should add an additional % to the var names, so the command would be:

for /d %%p in ("pathHERE*.jpg") DO echo %%p

when running a batch file the command interpreter removes the first % from any var name.

Upvotes: 3

William Stearns
William Stearns

Reputation: 479

Try placing quotes around your DO use of %%p

for /d %%p in ("pathHERE*.jpg") DO echo "%%p"

Upvotes: 0

Related Questions