简体   繁体   中英

How to copy file paths including extension into .txt file

For example in a folder named sample there are 3 images namely img1.jpg & img2.jpg & img3.jpg

I want to copy all the paths of all images to a .txt file

content of .txt will be

C:/sample/img1.jpg
C:/sample/img2.jpg
C:/sample/img3.jpg

Run this in your directory that you want to tree. It extends the path name.

> "dirs.txt" (for %A in (*) do echo %~fA)

Edited in regards to aschipfl's comments

I would start here: http://ss64.com/nt/for_d.html . This link shows how to iterate through files. in the example you can use this and modify for output to a file. The second one is a starting point for output.

FOR /D /r %G in ("User*") DO Echo We found %G 
FOR /D /r %G in ("User*") DO Echo "We found %G" >> C:\test.txt

You can use the dir command with recursion and bare format option like so

dir /s /b *.jpg > files.txt

Reference:

http://ss64.com/nt/dir.html

Clayton Wahlstrom's answer is a good start, but it can be improved:

  • every single item/line is redirected individually, because the >> operator is placed in the body of the for loop; however, redirecting all data once would improve the overall performance;
  • the used redirection syntax causes every line to contain a trailing SPACE , because there is one before the >> redirection operator;
  • you need to ensure that dirs.txt does not yet exist or is empty initially, because the >> redirection appends data if applicable;
  • I recommend to use upper-case for loop variables, to be able to distinguish them from the ~ modifiers;

To overcome all these issues, just put the entire for loop into parentheses () and do the redirection > once only, like this:

> "dirs.txt" (for %A in (*) do echo %~fA)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM