Display filepaths of specific files

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
ladduq
Posts: 31
Joined: 22 Jan 2012 01:08

Display filepaths of specific files

#1 Post by ladduq » 22 Jun 2012 04:47

Hi

I'm able to get the filepaths of all the files existing in a folder and all its subfolders.

However I need to get the filepaths of only specific files.

I have a folder which contains 300 subfolders and total number of files is 12332
i need to get the filepaths of only a 1000 files.

For example:
====================================
Input:
one.txt
two.txt
three.txt
.
.
.
.
.
.
thousand.txt
==========================================
output
C:\Users\ladduq\Desktop\New folder\okay\one.txt
C:\Users\ladduq\Desktop\New folder\okay\two\two.txt
C:\Users\ladduq\Desktop\New folder\three.txt
.
.
.
.
C:\Users\ladduq\Desktop\thousand.txt
===========================================
How can i acheive this.please suggest

Regards
Ladduq

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Display filepaths of specific files

#2 Post by foxidrive » 22 Jun 2012 05:52

Are the filenames unique amongst the 12,000 files?

If so then run this in the root of the folder tree and with the filelist.txt in the same folder.

Code: Select all

@echo off
dir /b /s /a-d >"%temp%\temp.list"
for /f "delims=" %%a in (filelist.txt) do (
find /i "\%%a" <"%temp%\temp.list" >>"filelist2.txt"
)
del "%temp%\temp.list"

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: Display filepaths of specific files

#3 Post by dbenham » 22 Jun 2012 06:23

@foxidrive - almost, but a few problems:

test.txt will match "c:\folder\test.txt.bak" and "c:\test.txt\somethingelse"

Can be fixed by switching to FINDSTR with /I /E and /C:"\%%a" options

Here is a similar but more efficient solution:

Create a text file containing the file names you want to find, one file per line. Prefix each file name with a backslash \

Code: Select all

\one.txt
\two.txt
etc.

Then simply run the following command (no need for a batch file, but it certainly works in one)

Code: Select all

dir /b /a-d /s "C:\Users\ladduq\Desktop\New folder\*" | findstr /lie /f:files.txt >output.txt

If you alrady have a text file of file names without the backslash prefix, you can use the following to add the prefix:

Code: Select all

>files.txt (for /f "eol=: delims=" %F in (noprefix.txt) do @echo \%F)

Change %F to %%F if you put the code in a batch file.


Another way to do this without FINDSTR. Assume you have a list of file names (no prefix) in a file named NOPREFIX.TXT

Code: Select all

>output.txt (for /f "eol=: delims=" %F in (noprefix.txt) do @dir /b /a-d /s "C:\Users\ladduq\Desktop\New folder\%F")

Again, change %F to %%F if using a batch file


Dave Benham

ladduq
Posts: 31
Joined: 22 Jan 2012 01:08

Re: Display filepaths of specific files

#4 Post by ladduq » 25 Jun 2012 05:28

@foxidrive and @Dave Benham Thank You for The Support :)

Actually each file has a single line starting with the word "start"

so can i also get the file paths along with the line which starts with the word "start"

For example:
====================================
Input:
one.txt(if one.txt file contains a start line like "Start this promo first")
two.txt(if two.txt file contains a start line like "Start this promo should be ended at last")
three.txt
.
.
.
.
.
.
thousand.txt
==========================================
output:

path
C:\Users\ladduq\Desktop\New folder\okay\one.txt
Start
Start this promo first


path
C:\Users\ladduq\Desktop\New folder\okay\two\two.txt
start
Start this promo should be ended at last

etc.

can we acheive this with file names please suggest.

Thanks & Regards
Ladduq

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: Display filepaths of specific files

#5 Post by dbenham » 25 Jun 2012 06:19

Well that is a completely different question.

This simple one liner:

Code: Select all

findstr /si "^start\>" "C:\Users\ladduq\Desktop\New folder\*"

Gives output like the following:

Code: Select all

C:\Users\ladduq\Desktop\New folder\okay\one.txt:Start this promo first
C:\Users\ladduq\Desktop\New folder\okay\two\two.txt:Start this promo should be ended at last


To get output as you have described, you just need to parse the results in a FOR /F loop:

Code: Select all

@echo off
for /f "tokens=1,2* delims=:" %%A in ('findstr /si "^start\>" "C:\Users\ladduq\Desktop\New folder\*"') do (
  echo Path
  echo %%A:%%B
  echo Start
  echo %%C
  echo(
)


If you only provide a relative path without the drive letter, then you need:

Code: Select all

@echo off
for /f "tokens=1* delims=:" %%A in ('findstr /si "^start\>" "New folder\*"') do (
  echo Path
  echo %%~fA
  echo Start
  echo %%B
  echo(
)

Post Reply