Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I create a batch file to search for file(s) with certain extension within a folder?

Tags:

batch-file

Please help! I an new to creating batch files.

I am trying to create a batch file to do the following things :

  1. Search for file(s) with a certain file extension (i.e. .docx) within a folder
  2. Output both filename(s) and extension to a text file (.txt)
  3. In the text file, I want to add an index before the file name

For example, "folder 1" has these three files : test1.docx, test2.docx, test3.xlsx The batch file will search for these three files that have extension with .docx, and then output to a text file (i.e. search_result.txt)

In the search_result.txt, it will have this format :

1 test1.docx
2 test2.docx

here is what I have so far which is doing #1 and #2 items mentioned above, but I need help to implement #3.

@echo off
for /r %%i in (*.docx) do echo %%~nxi >> search_result.txt

Thank you in advance for the help.

like image 925
user2387527 Avatar asked May 15 '13 21:05

user2387527


1 Answers

@echo off
setlocal enabledelayedexpansion
set /a counter=1
for /r %%i in (*.docx) do (

  echo !counter! %%~nxi >> search_result.txt
  set /a counter=!counter!+1
)
endlocal
like image 112
npocmaka Avatar answered Oct 19 '22 07:10

npocmaka