0

im quite new to cmd-scripts and i encountered a problem. i want to iterate through all files in the current folder and all its subfolders having certain endings that are stored in the array %list%. so i started with:

@for /R %%d in (.) do @( 
    cd %%d
    @(for %%a in (%list%) do @( 
       for %%f in (*.%%a) do @echo %%d/%%f
    ))          

)

this works however it produces paths like C:\test\.\myfile.txt so i want to get a substring of the dir. since this does not work on variables like %%d, i tried to store %%d in a seperate variable:

@for /R %%d in (.) do @( 
    cd %%d
    set dir=%%d
    @(for %%a in (%list%) do @( 
       for %%f in (*.%%a) do @echo %dir%/%%f
    ))          

)

but this gives me the same output as @echo /%%f what am i doing wrong?

Ginso
  • 459
  • 17
  • 33
  • 2
    another [delayed expansion](https://stackoverflow.com/questions/30282784/variables-in-batch-not-behaving-as-expected/30284028#30284028) issue. – Stephan Aug 15 '18 at 09:29
  • How long is your list? If it's short, there might be a better solution. – Stephan Aug 15 '18 at 09:31

1 Answers1

0

First I don't understand why you insist to silent each command where you can simply silent all by a single @echo off

There is absolutely no need to change the current directory on each iteration, you can perform your task from where you are.

You can eliminate the trailing \. from the FOR /R variable by applying the ~f modifier to %%d So with %%~fd the path C:\test\. becomes C:\test

@echo off
for /R %%d in (.) do for %%e in (%list%) do (
    for %%f in ("%%~fd\*.%%e") do echo %%f
)
sst
  • 1,443
  • 1
  • 12
  • 15
  • well that might be helpfull but it doesn't really answer the question. How can i store %%d in a variable dir, so that i can use something like %dir:~3,-2%? – Ginso Aug 15 '18 at 10:13
  • 1
    I missed that point, because the way you asked looked like you are just trying resolve the issue with trailing `\.` and even in your second attempted code, you didn't try to get any substring from `%dir%` like what you have showed in your above comment and without trying to do some manipulation to the variable using a variable doesn't make sense.So please update your question and code and show exactly what substring you want to get how you want to use it. – sst Aug 15 '18 at 10:46
  • If your original intend for using a variable and getting substring from it was to resolve the issue with `\.` then this is a the correct answer I gave you there is no need to use a variable and your question becomes an instance of [XY Problem](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem) which has been resolved but if you want to use a variable for something else or if your are just curious how can use variable in a code block then your question is duplicate which is already marked as such as I'm writing this comment. – sst Aug 15 '18 at 11:24