-2

I need a code to find jpg files and get a list of directory's name where files exists. I have write a code that finds all folders my code is

For /r %%i in (.) do ( echo %%~nf >>list1.txt
)
For /f "tokens=*" %%i in (list1.txt) do ( echo %%~nxi >>list2.txt
)

And it saves a list of all subfolders but i want to get name of folder where file exists.

Gray
  • 115,027
  • 24
  • 293
  • 354
Mhwmd
  • 71
  • 8
  • My language is batch – Mhwmd May 19 '18 at 16:19
  • Never heard of batch. Do you mean the Windows .bat files? – Gray May 19 '18 at 16:23
  • @Mhwmd looks like it is possible. Did you tried https://stackoverflow.com/questions/1707058/how-to-split-a-string-by-spaces-in-a-windows-batch-file – Iman Nia May 19 '18 at 16:31
  • Yes it is .bat language – Mhwmd May 19 '18 at 16:32
  • Just use the `FOR /R` to get the list of the jpg files. It will give you the folder names and the file names at the same time. – Squashman May 19 '18 at 16:34
  • 3
    This is now your 5th question on StackOverFlow and you have not accepted an answer to any of your questions. If you do not understand how StackOverFlow works please take the [tour](https://stackoverflow.com/tour). – Squashman May 19 '18 at 16:39
  • 2
    `for /R %%I in (*.jpg) do echo %%~dpI.`? or `for /R %%I in (.) do if exist "%%~I\*.jpg" echo %%~fI`? – aschipfl May 19 '18 at 18:07
  • 1
    @Mhwmd please go back and accept answers to all your questions. That is how you give thanks on StackOverFlow! – Squashman May 19 '18 at 19:54

1 Answers1

1

It seems you do not properly understand what you read when you do research. As an example.

For /r %%i in (.) do ( echo %%~nf >>list1.txt

tells me you have seen this %%~nf somewhere but did you try read up on what it does? You give a variable of %%i but you try and use %%f and you want the path (%%~p) but use the name (%%~n). If you actually ran the help from cmd for /? you would have had some great tips.

This:

For /r %%i in (*.jpg) do echo "%%~dpi" >>list1.txt

Would give drive and path to file, where this:

For /r %%i in (*.jpg) do echo "%%~pi" >>list1.txt

Would give just the path, while this:

For /r %%i in (*.jpg) do echo "%%~nxi" >>list1.txt

Would give the name and extention of the file and finally this:

For /r %%i in (*.jgp) do echo "%%~ni" >>list1.txt

Would give only the name of the file without extension.

So I challenge you to run help switches for your commands via cmd and I promise you, you will be astonished to see what you can learn from this.

Gerhard
  • 22,678
  • 7
  • 27
  • 43