user2488832
user2488832

Reputation: 33

batch - Read file path from file and use it as file path in next for loop

How can I read a line from .txt file and use it as path in next for loop. .txt file looks like this:

C:\Users\User\Desktop\kaust 1
C:\Users\User\Desktop\kaust 2


for /F %%A in (C:\Users\User\Desktop\asd.txt) do (
  for /R "%%A" %%f in (*) do (
   copy "%%f" "C:\Users\User\Desktop\New folder\1\"
  )
)

The idea of script is to use the file path as folder and take files from it and its subfolder(s) and copy those files into another folder, so all the files are in the same place without subfolders.

As an error it says:

C:\Users\User>(for /R "%A" %f in (*) do (copy "%f" "C:\Users\User\Desktop\New folder\1\" ) ) 

so It doesn't take %%A as variable.

Upvotes: 1

Views: 72

Answers (1)

PA.
PA.

Reputation: 29369

you are experiencing a very tricky behavior of the parsing of FOR and expanding of %%A variables when used as FOR parameters.

You might better try a workaround, by changing the current directory with PUSHD %%a, and invoking FOR over the current directory.

for /F %%A in (asd.txt) do (
  pushd %%A
  for /R %%f in (*) do (
    echo %%f
  )
  popd
)

Upvotes: 2

Related Questions