3

I'm coding a batch file that should search two folder paths for multiple file extensions and list them in a text file. Currently I'm trying to use a FOR loop with a list of file extensions (*.doc, *.docx, etc). I believe the file is erroring out because of the "*" character but I don't know how to correct this.

I've tried to straight list them: FOR %%G IN (*.one,*.mht,*.onepkg). I've tried quote marks: FOR %%G IN ("*.one","*.mht","*.onepkg"). I've tried carets: FOR %%G IN (^^*.one,^^*.mht,^^*.onepkg).

Here's my code:

set outputfilepath=d:\output.txt

FOR %%G IN ("*.one","*.mht","*.onepkg") DO (
echo Searching for %%G files
dir "C:\%%G" /s /b >> "%outputfilepath%"
Rem Add 2 blank lines between next search
echo. >> "%outputfilepath%"
echo. >> "%outputfilepath%" )

Nothing gets output to my text file.

Any help is appreciated.

2
  • What's wrong with simply using Where /R C:\ *.one *.mht *.onepkg > "D:\output.txt"? or Dir /B /S /A-D C:\*.one C:\*.mht > "D:\output.txt"? Of those two examples, the former will match the extensions exactly, the latter outputs extensions beginning with .one and .mht, (as does your current FOR loop), so will therefore output the .onepkg extensions too! Commented Jun 7, 2019 at 14:31
  • My only issue with that solution is that my supervisor would like something that displays on the screen "Searching for <extension> files" and formatting the output file with two spaces in between different lists of files found for readability. Would there be some way to do that with either of those commands? Commented Jun 7, 2019 at 15:38

3 Answers 3

3
@ECHO Off
SETLOCAL
set "outputfilepath=u:\output.txt"

(
FOR %%G IN (one,mht,onepkg) DO (
 echo Searching for %%G files>con
 dir ".\*.%%G" /s /b |FINDSTR /i /e /L ".%%G"
 Rem Add 2 blank lines between next search
 echo. 
 echo.  
)
)> "%outputfilepath%"

GOTO :EOF

Please note that I've changed drivenames to suit my system.

simply for meta in extensionlist then add the * in the dir command. Filter the dir output using findstr to ensure that only names matching /e at the end /L the literal ".%%G" are shown.

Also by enclosing the enitre for command in parentheses, you can send all stdout text (which would normally appear on the console) to the file. > naturally means create-file-anew. >> to append if that's your preference.

The >con appended to the Searching... echo overrides the redirection and specifically sends the text from that echo to the console.

Sign up to request clarification or add additional context in comments.

3 Comments

Dear Mr. @Magoo... how on earth does your highlighting end up so pretty? I have been reading links for 20 minutes now and can't figure it out. :^P Most of the links said that it would figure it out based on the tags but isn't working for me.
@SeñorCMasMas : see stackoverflow.com/help/formatting - simply insert <!-- language: lang-dos --> and a blank line directly before the code block.
This is brilliant and exactly what I'm looking for. Magoo, you've done it again!
0

I really like the existing suggestions but this is a different approach. I find that this style reads more like code.

This may seem very complicated but...

  1. This technique can be used to loop over parameters for ANYTHING to include ones passed via the command line.
  2. The logic of looping through passed parameters is isolated to its own function (enumerate_search_types).
  3. The logic of what you are going to do with each parameter is isolated to its own function (search_for_search_type).

This might make it easier for some, too complicated for others.

@echo off

:: The parameters we are working with...
set outputfilepath=d:\output.txt
set starting_path=c:\
set search_types="*.one" "*.mht" "*.onepkg"

pushd "%starting_path%"
call :enumerate_search_types %search_types%
popd
goto :EOF

:: ---------------------------------------------------------------
:enumerate_search_types
set "current_param=%~1"
if "%current_param%"=="" goto :EOF
call :search_for_search_type "%current_param%"
shift /1
goto :enumerate_search_types

:: ---------------------------------------------------------------
:search_for_search_type
set "current_search_type=%1"
set "had_output=false"

echo Searching for %current_search_type% files
for /f "delims=" %%f in ('dir /s /b %current_search_type% 2^>NUL') do set had_output=true&& echo %%f >> "%outputfilepath%"

:: If the dir command didn't produce anything, don't add the blank lines
if "false"=="%had_output%" goto :EOF
echo. >> "%outputfilepath%"
echo. >> "%outputfilepath%"
goto :EOF

Comments

0

Short answer if you refer to only the file extension. In order to use different extension in a for loop:

FOR %%G IN (one,mht,onepkg) do [command]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.