I am just starting to get my feet wet with MS Batch files. I have created a small batch that searches the entered directory for files containing a certain string using findstr /m. It is returning a file that contains the string, but only the first one it finds. I have searched the findstr /? and online command reference, as well as this site. I cannot find a way for findstr to return ALL the files with an instance of the string. What am I missing?
#echo off
setlocal
ECHO This Program Searches for words inside files!
:Search
set /P userin=Enter Search Term:
set /p userpath=Enter File Path:
FOR /F %%i in ('findstr /M /S /I /P /C:%userin% %userpath%\*.*') do SET finame=%%i
if "%finame%" == "" (set finame=No matching files found)
echo %finame%
set finame=
endlocal
:searchagain
set /p userin=Do you want to search for another file? (Y/N):
if /I "%userin%" == "Y" GOTO Search
if /I "%userin%" == "N" GOTO :EOF ELSE (
GOTO wronginput
)
Pause
:wronginput
ECHO You have selected a choice that is unavailable
GOTO searchagain
If you replace this:
FOR /F %%i in ('findstr /M /S /I /P /C:%userin% %userpath%\*.*') do SET finame=%%i
if "%finame%" == "" (set finame=No matching files found)
echo %finame%
set finame=
with this then it may work the way you expect
findstr /M /S /I /P /C:"%userin%" "%userpath%\*.*"
if errorlevel 1 echo No matching files found
Thanks everyone. Just in case someone else searches this site, here is my final code.
#echo off
ECHO This Program Searches for words inside files!
:Search
setlocal
set /P userin=Enter Search Term:
set /p userpath=Enter File Path:
findstr /M /S /I /P /C:%userin% %userpath%\*.* 2> NUL
if ERRORLEVEL 1 (ECHO No Matching Files found) ELSE (
GOTO searchagain
)
endlocal
:searchagain
setlocal
set /p userin=Do you want to search for another file? (Y/N):
if /I "%userin%" == "Y" GOTO Search
if /I "%userin%" == "N" GOTO :EOF ELSE (
GOTO wronginput
)
endlocal
:wronginput
ECHO You have selected a choice that is unavailable
GOTO searchagain
In your for loop, when you assign to finame the value of %%i, you are replacing the previous value, so only the last file gets echoed to console.
If you try your findstr command (out of the for) you will see the list of files.
to place all in var:
setlocal enabledelayedexpansion
set nlm=^
set nl=^^^%nlm%%nlm%^%nlm%%nlm%
for %%i in ('dir %userpath% /b') do for /f %%a in ('type "%userpath%\%%i"^|find "%userin%" /i') do set out=!out!!nl!%%i
Related
I am trying to create a menu with submenus which their names without extension come from the directory. However, I am unable to make a variable for choice as number. This code does not work anyhow. I would also want to display a number at the beginning of each file name in the menu; in fact, number of the files will also be one of the number that user selects as input. I could not overcome the problem.
#echo off
cd C:\Users\Murray\Documents\ConfigFiles\
for /f %%A in ('dir /a-d-s-h /b *conf ^| find /v /c ""') do set count=%%A
echo File count = %count%
for %%F in ("C:\Users\Murray\Documents\ConfigFiles\*.conf") do echo %%~nxF
set choice=
set /C /N="Please choice: "
if "%choice%" == "%count%" goto SUBMENU
if NOT EXIST "C:\Users\Murray\Documents\ConfigFiles\%choice%" goto NOFILE
:SUBMENU
Echo You are here
goto end
:NOFILE
echo %choice% could not be found.
goto END
:end
Any help will be appreciated.
Here's a quick example of a method touted in the answer by on no. It will print a number to the screen followed by the names of each file matching file extension, so will allow a large number of matching files, (although the end user may have to scroll a long list). Once a file is chosen, your end user then just types its corresponding number. The code should not proceed beyond the input request until a number matching one in the list is ENTERed.
#Echo Off
SetLocal EnableExtensions DisableDelayedExpansion
Set "src=C:\Users\Murray\Documents\ConfigFiles"
Set "ext=.config"
If Not Exist "%src%\*%ext%" Echo No file matches.& GoTo End
For /F "Delims==" %%G In ('"(Set #) 2>NUL"') Do Set "%%G="
For /F "Delims==" %%G In ('%SystemRoot%\System32\wbem\WMIC.exe OS Call /? ^|
%SystemRoot%\System32\find.exe "=="') Do Set "HT=%%G"
For /F "Tokens=1* Delims=:" %%G In ('Dir /B /A:-D "%src%\*%ext%" 2^>NUL ^|
%SystemRoot%\System32\findstr.exe /E /I /L /N "%ext%"'
) Do Set "#%%G=%%H" & Echo( %%G.%HT:~-1%%%H
If Not Defined #1 Echo No file matches.& GoTo End
:Opt
Echo(
Set "HT=" & Set "opt="
Set /P "opt=Enter the number for your chosen file>"
If Not Defined opt (GoTo Opt) Else Set "opt=%opt:"=%"
Set # | %SystemRoot%\System32\findstr.exe /B /L "#%opt%=" 1>NUL || GoTo Opt
SetLocal EnableDelayedExpansion
For %%G In ("!#%opt%!") Do EndLocal & Set "opt=%%~G"
#Rem Your code goes below here
Echo(& Echo You Selected %opt%
#Rem Your code ends above here
:End
Setlocal EnableDelayedExpansion & For /F "Tokens=1,2" %%G In ("!CMDCMDLINE!"
) Do Endlocal & If /I "%%~nG" == "cmd" If /I "%%~H" == "/c" Echo(& Echo Press^
any key to exit.&Pause 1>NUL
All you need to do is to modify the variables values on lines 4 and 5, if necessary, in order to test it. I will not be supporting changes or additions beyond that. Once you have tested the code, you may insert your code between lines 25 and 29, replacing the example line I left there for your test.
Batch's limitations are to blame for sub-par corrections to this question.
Here's the best I could whip up in a few seconds:
#echo off
:one
cls
cd C:\Users\Murray\Documents\ConfigFiles\
for %%F in ("C:\Users\Murray\Documents\ConfigFiles\*.conf") do echo %%~nxF
echo.
echo Enter Configuration Name:
set/p "prompt=>"
if exist %prompt%.txt goto :two
if exist %prompt% goto :two
cls
echo File not found
pause >NUL
goto :one
:two
cls
REM When file is found, this code will run
pause >NUL
The set command does not prompt for user input unless specified with the /p switch. To make your code more friendly, i'd also recommend to prompt for the filepath earlier on in the code.
EDIT: A few alternative solutions: Declare the 9th option of the prompt of the choice command as a "second page" or "more" option. This would really be a pain for the user in a directory with tens or hundreds of files. Another; you can assign an integer to each file that matches your quasi-query and echo them before each filename on the screen, then allow the user to input the number to get that file. That seems fairly efficient, and if you'd like to explore one or both of those alternates I can help (if you need it).
Is it possible to pipe the command output to the following batch file as its input?
I'd like to feed output from commands such as dir c:\temp |find "05" |find "new" to the following batch file. Because my command have many variations and I don't want to edit the batch file every time I need it, thus, I'm looking for a way to feed the command output directly to the batch file, instead of having the batch file generate its input using dir /b. Basically, what I'd like to achieve is to find from a list of files (generated using the dir command) the file whose name contains the highest number (achieved using the batch file.) Example:
today123.txt
today456.txt
tomorrow123.txt
tomorrow456.txt
With the dir command, I can filter off today or tomorrow, leaving only two files. Then, feed these two files to the batch file and have it select the one which has 456 in the file name. Of course, this is a simplified example. I may have more files and more groups than those in the example.
for /f %%a in ('dir /b ^|sort /r ^|findstr /r [0-9]') do (
set "filename=%%a"
goto done
)
:done
echo the highest found is %filename%
exit /b 0
There are quite a number of ways. This is one of them:
#echo off & set filename=
if "%~5" == "" set "myfind=dir /b ^| find "%~2" ^| find "%~3" ^| find "%~4" ^|sort /r ^|findstr /r [0-9]"
if "%~4" == "" set "myfind=dir /b ^| find "%~2" ^| find "%~3" ^|sort /r ^|findstr /r [0-9]"
if "%~3" == "" set "myfind=dir /b ^| find "%~2" ^|sort /r ^|findstr /r [0-9]"
if "%~2" == "" set "myfind=dir /b ^|sort /r ^|findstr /r [0-9]"
pushd "%~1"
for /f %%a in ('%myfind%') do (
set "filename=%%a"
goto done
)
:done
popd
if not defined filename echo Not match found & exit /b 1
echo the highest found is %filename%
exit /b 0
Typically you would run it as:
batch-file-name.cmd "C:\path\to\search" "search1" "search2" "search3"
for instance, using your example:
batch-file-name.cmd "c:\temp" "05" "new"
or even extend the search:
batch-file-name.cmd "c:\temp" "05" "new" ".txt"
How it works:
We set the search string each time in the case an additional find command is required. for now we have up to three finds and one path, but it can be extended to more. You have to set them in descending order though.
I also added an additional statement if not defined filename to ensure you are alerted if a find is not matched.
The following gets the highest of a given group:
#echo off
setlocal enabledelayedexpansion
set "search=today"
set "max=0"
for %%a in (%search%*.txt) do (
set "name=%%~na"
set "number=!name:%search%=!"
if !number! gtr !max! set /a max=number
)
echo max number for %search% is %max%
set "highest=%search%%max%.txt"
echo %highest%
Attention, there is no errorchecking at all, so it depends on the correct format of the file names. (errorchecking can be added when needed)
To get the search string as a parameter, simply replace set "search=today" with set "search=%~1"
I need to find multiple strings but the strings I look up flag for being in the batch itself. I search the string Incognito and the batch including it flags. How do I prevent this?
Edit: This is a snippet of my current code. Basically it opens the text file and shows this batch file in it and other stuff too which is good, but I need it not to flag this batch file.
findstr /i /s /m "Obligatory" *.* > str-unfixed.txt
if %errorlevel%==0 (
Call :TypeWriter "Obligatory Strings Found."
) else (
Call :TypeWriter "No Obligatory Strings Found."
)
start "" "C:\Users\%USERNAME%\Desktop\str-unfixed.txt"
Don't worry about the TypeWriter, this is only a snippet.
Edit2: Thanks for the edit Mofi, but it blanks out at console and doesn't complete.
Full script. Here we go.
#echo off
goto Next
::*************************************************************
:TypeWriter
echo(
(
echo strText=wscript.arguments(0^)
echo intTextLen = Len(strText^)
echo intPause = 30
echo For x = 1 to intTextLen
echo strTempText = Mid(strText,x,1^)
echo WScript.StdOut.Write strTempText
echo WScript.Sleep intPause
echo Next
)>%tmp%\%~n0.vbs
#cscript.EXE /noLogo "%tmp%\%~n0.vbs" "%~1"
echo(
exit /b
::**************************************************************
:Next
#echo off
set "OutputFile=%USERPROFILE%\Desktop\str-unfixed.txt"
%SystemRoot%\System32\findstr.exe /i /s /m "Vape" *.* | %SystemRoot%\System32\findstr.exe /E /L /V /C:"%~nx0" >"%OutputFile%"
for %%I in ("%OutputFile%") do if %%~zI == 0 goto VapeNoFileFound
for %%I in ("%OutputFile%") do if %%~zI == 1 goto VapeFileFound
:VapeFileFound
Call :TypeWriter "Vape Strings Found."
goto Kurium
:VapeNoFileFound
Call :TypeWriter "No Vape Strings Found."
goto Kurium
:Kurium
set "OutputFile=%USERPROFILE%\Desktop\str-unfixed.txt"
%SystemRoot%\System32\findstr.exe /i /s /m "Kurium" *.* | %SystemRoot%\System32\findstr.exe /E /L /V /C:"%~nx0" >>"%OutputFile%"
for %%I in ("%OutputFile%") do if %%~zI == 0 goto KuriumNoFileFound
for %%I in ("%OutputFile%") do if %%~zI == 1 goto KuriumFileFound
:KuriumFileFound
Call :TypeWriter "Kurium Strings Found."
goto Spook
:KuriumNoFileFound
Call :TypeWriter "No Kurium Strings Found."
goto Spook
:Spook
set "OutputFile=%USERPROFILE%\Desktop\str-unfixed.txt"
%SystemRoot%\System32\findstr.exe /i /s /m "Spook" *.* | %SystemRoot%\System32\findstr.exe /E /L /V /C:"%~nx0" >>"%OutputFile%"
for %%I in ("%OutputFile%") do if %%~zI == 0 goto SpookNoFileFound
for %%I in ("%OutputFile%") do if %%~zI == 1 goto SpookFileFound
:SpookFileFound
Call :TypeWriter "Spook Strings Found."
goto Aimassist
:SpookNoFileFound
Call :TypeWriter "No Spook Strings Found."
goto Aimassist
:Aimassist
set "OutputFile=%USERPROFILE%\Desktop\str-unfixed.txt"
%SystemRoot%\System32\findstr.exe /i /s /m "Aimassist" *.* | %SystemRoot%\System32\findstr.exe /E /L /V /C:"%~nx0" >>"%OutputFile%"
for %%I in ("%OutputFile%") do if %%~zI == 0 goto AANoFileFound
for %%I in ("%OutputFile%") do if %%~zI == 1 goto AAFileFound
:AAFileFound
Call :TypeWriter "Aim Assist Strings Found."
goto Triggerbot
:AANoFileFound
Call :TypeWriter "No Aim Assist Strings Found."
goto Triggerbot
:Triggerbot
set "OutputFile=%USERPROFILE%\Desktop\str-unfixed.txt"
%SystemRoot%\System32\findstr.exe /i /s /m "Triggerbot" *.* | %SystemRoot%\System32\findstr.exe /E /L /V /C:"%~nx0" >>"%OutputFile%"
for %%I in ("%OutputFile%") do if %%~zI == 0 goto TRNoFileFound
for %%I in ("%OutputFile%") do if %%~zI == 1 goto TRFileFound
cscript //nologo "C:\Users\%USERNAME%\Desktop\dedup.vbs" < "C:\Users\%USERNAME%\Desktop\str-unfixed.txt" > "C:\Users\%USERNAME%\Desktop\str-fixed.txt"
del /s /q "C:\Users\%USERNAME%\Desktop\str-unfixed.txt" > nul
:TRFileFound
Call :TypeWriter "Triggerbot Strings Found."
start "" "C:\Users\%USERNAME%\Desktop\str-fixed.txt"
:TRNoFileFound
Call :TypeWriter "No Triggerbot Strings Found."
start "" "C:\Users\%USERNAME%\Desktop\str-fixed.txt"
pause > nul
::************************************************
And if you need dedup.vbs:
Set Inp = WScript.Stdin
Set Outp = Wscript.Stdout
Set Dict = CreateObject("Scripting.Dictionary")
Do Until Inp.AtEndOfStream
On Error Resume Next
Line=Inp.readline
Dict.Add Line, ""
Loop
For Each thing in Dict.Keys()
Outp.writeline thing
Next
Edit3: Thanks Mofi!
My problem: need to get the items in the text document to show up on cmd window.
Script:
#echo off
goto Next
::*************************************************************
:TypeWriter
echo(
(
echo strText=wscript.arguments(0^)
echo intTextLen = Len(strText^)
echo intPause = 30
echo For x = 1 to intTextLen
echo strTempText = Mid(strText,x,1^)
echo WScript.StdOut.Write strTempText
echo WScript.Sleep intPause
echo Next
)>%tmp%\%~n0.vbs
#cscript.EXE /noLogo "%tmp%\%~n0.vbs" "%~1"
echo(
exit /b
::**************************************************************
:Next
#echo off
findstr /i /s /m /C:"Vape" /C:"Kurium" /C:"Spook" /C:"Aimassist" /C:"Triggerbot" /C:"Smoothaim" *.* > str-unfixed.txt
cscript //nologo "C:\Users\%USERNAME%\Desktop\dedup.vbs" < "C:\Users\%USERNAME%\Desktop\str-unfixed.txt" > "C:\Users\%USERNAME%\Desktop\str-fixed.txt"
if %errorlevel%==0 (
Call :TypeWriter "Blacklisted Strings Found."
start "" "C:\Users\%USERNAME%\Desktop\str-fixed.txt"
) else (
Call :TypeWriter "No Blacklisted Strings Found."
start "" "C:\Users\%USERNAME%\Desktop\str-fixed.txt"
)
pause > nul
::************************************************
The batch file could be filtered out for example with using this code:
#echo off
set "OutputFile=%USERPROFILE%\Desktop\str-unfixed.txt"
%SystemRoot%\System32\findstr.exe /I /M /S "Obligatory" *.* | %SystemRoot%\System32\findstr.exe /E /L /V /C:"%~nx0" >"%OutputFile%"
for %%I in ("%OutputFile%") do if %%~zI == 0 goto NoFileFound
call :TypeWriter "Obligatory strings found."
start "" "%OutputFile%"
set "OutputFile="
goto :EOF
:NoFileFound
del "%OutputFile%"
set "OutputFile="
call :TypeWriter "No obligatory string found."
goto :EOF
:TypeWriter
echo %~1
The output of first FINDSTR is filtered with a second FINDSTR which outputs all lines not ending with the name of the batch file searched as case-sensitive literal string.
The FOR command is used to check the file size of the output file.
Is the output file an empty file, i.e. the file size is equal 0 bytes, the string Obligatory was not found in any file except the batch file itself resulting in deleting the output file and printing the appropriate message.
Otherwise after printing the message that the string was found, the output file is opened in the application associated with file extension .txt.
The batch file above was tested for both scenarios on a subdirectory and not on entire drive C: as it can take a quite long time to finish on C:. There are hundred thousands of files on drive C: with dozens of GiB in total to search for the string when using *.* as file name pattern and start in root of drive C:.
EDIT 1: Define string to find in two parts
A much better solution as above is avoiding search string being in batch file. This can be done by using an environment variable for the string to find and define the value of the environment variable in two steps.
#echo off
setlocal
set "OutputFile=%USERPROFILE%\Desktop\str-unfixed.txt"
set "StringToFind=Obli"
set "StringToFind=%StringToFind%gatory"
%SystemRoot%\System32\findstr.exe /I /M /S "%StringToFind%" *.* >"%OutputFile%"
if errorlevel 1 (
del "%OutputFile%"
call :TypeWriter "No %StringToFind% strings found."
) else (
call :TypeWriter "%StringToFind% strings found."
start "" "%OutputFile%"
)
endlocal
goto :EOF
:TypeWriter
echo %~1
This batch file also searches for Obligatory, but it does not contain this string, just Obli and gatory.
EDIT 2: Define strings to find with omitted escape character
Here is one more solution where the multiple strings to find are assigned to environment variables with escape character ^ anywhere in the middle of the search strings to exclude the batch file.
^ is removed by Windows command interpreter on assigning the search strings to the environment variables as not being found within a double quoted string. Please note that this solution requires that the lines
set SearchX=S^earch String
do not have 1 or more trailing spaces/tabs as the trailing whitespace(s) would be also assigned to the environment variable. The search would not work anymore as expected with a not visible space or horizontal tab at end of those lines as being part of the search string.
Additionally this batch code outputs first each file name found by FINDSTR containing one of the 6 search strings to console window and next redirects the file name into the output text file.
#echo off
setlocal EnableExtensions
set "OutputFile=%USERPROFILE%\Desktop\str-unfixed.txt"
rem Define all search strings with omitted escape character.
set Search1=V^ape
set Search2=K^urium
set Search3=S^pook
set Search4=A^imassist
set Search5=T^riggerbot
set Search6=S^moothaim
rem Delete the output file from a previous run if existing at all.
del "%OutputFile%" 2>nul
for /F "delims=" %%I in ('%SystemRoot%\System32\findstr.exe /I /M /S /C:"%Search1%" /C:"%Search2%" /C:"%Search3%" /C:"%Search4%" /C:"%Search5%" /C:"%Search6%" *') do (
echo %%I
echo %%I>>"%OutputFile%"
)
if exist "%OutputFile%" (
call :TypeWriter "Obligatory strings found."
start "" "%OutputFile%"
) else (
call :TypeWriter "No obligatory string found."
)
endlocal
goto :EOF
:TypeWriter
echo %~1
For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.
call /?
del /?
echo /?
endlocal /?
findstr /?
for /?
goto /?
rem /?
set /?
setlocal /?
start /?
See also the Microsoft support article Testing for a Specific Error Level in Batch Files and Microsoft TechNet article Using command redirection operators.
Just pipe the results through another check for the script name.
Replace your first line, findstr /i /s /m "Obligatory" *.* > str-unfixed.txt with this:
findstr/ism "Obligatory" *.*|findstr/ixvc:"%~nx0">str-unfixed.txt
I will add however that I would strongly suggest not searching through every single file on the C: drive looking for a specific search term. You really do need to filter this somehow perhaps by altering your file wildcard/mask.
Using the following code I am able to create a list of folders in a directory -
Setlocal EnableDelayedExpansion
set /p pattern=Enter Search Term:
echo.
dir /b /A:D %pattern%*
For example, if the user inputs con, the result may be
-construction1
-construction2
-construction_Docs
etc.
I would like to be able to attach a value to each item eg.
1_Construction1
2_Construction_Docs
etc.
Am I correcting in thinking that I would have to output the initial list I created to a .txt and then read and attach a variable to every line?
The end result would be a user being able to select one of the items based on the number we attach to it, and then have further actions being taken on that item.
dir /b /A:D %pattern%*|find /n /v ""
would yield
[1]construction1
[2]construction2
[3]construction_Docs
or
dir /b /A:D %pattern%*|findstr /n /v ":"
would yield
1:construction1
2:construction2
3:construction_Docs
You could then try
set /a max=0
for /f "delims=:" %%a in ('dir /b /A:D %pattern%*^|findstr /n /v ":" 2^>nul') do set /a max=%%a
which would set max to 0 if none found or the maximum number
The ^ tells batch that the pipe/> is part of the command to be executed
2>nul suppresses error messages in the case of no matches found
It's then a simple matter of
if %max%==0 echo none found&goto askagain
set /p "selection=Please choose [1..%max%] ? "
for /f "tokens=1*delims=:" %%a in ('dir /b /A:D "%pattern%*"^|findstr /n /v ":" 2^>nul') do if "%%a"=="%selection%" set "dirselected=%%b"&goto found
echo %selection% is invalid
goto askagain
:found
echo %dirselected% selected.
to make a complete job.
This task is relatively simple to achieve if you use the right tools, like an array:
#echo off
setlocal EnableDelayedExpansion
set /P "pattern=Enter Search Term: "
echo/
rem Get the folders, store they in the array and show the menu
set "num=0"
for /D %%f in (%pattern%*) do (
set /A num+=1
set "folder[!num!]=%%f"
echo !num!- %%f
)
if %num% equ 0 echo No folders found & goto :EOF
echo/
:selectFolder
set /P "num=Enter the desired folder: "
echo/
if not defined folder[%num%] goto selectFolder
set "item=!folder[%num%]!"
echo Folder selected: %item%
For further details on array management in Batch files, see this post.
The purpose is to scan a given folder for media files and send the mediainfo to info.txt BUT also to give user an option to scan only for files having a particular text string.
My bat file:
#echo off
setLocal EnableDelayedExpansion
echo. >info.txt
set /P "folder=Enter folder path: "
set /P "_input=Search all files (y/n): "
if /I "%_input%" == "y" (
dir %folder% /B /O:N | findstr /I ".wmv$ .mpg$ .mkv$ .mpeg$ .mp4$ .avi$" >filename.txt
for /f "tokens=* delims= " %%a in ('type filename.txt') do (
set _in=%_in%%%a
mediainfo --Inform=file://template.txt "%folder%!_in!" >>info.txt
echo. >>info.txt
)
) else (
set /P "_str=Enter file string: "
dir %folder% /B /O:N | findstr /I "%_str%" >filename.txt
for /f "tokens=* delims= " %%a in ('type filename.txt') do (
set in=%in%%%a
mediainfo --Inform=file://template.txt "%folder%!in!" >>info.txt
echo. >>info.txt
)
)
del filename.txt
cls
pause
Though the first part of if loop works correctly but the 'else' part don't, I can't get the error because with a flicker of an eye it disappears & I can't troubleshoot it
Well you could troubleshoot if you left ECHO ON and removed the CLS or put the pause before CLS.
Your ELSE block is failing because you are setting _str within the block and then attempting to use the value via normal expansion. You need to use delayed expansion.
Even your first IF block looks wrong because your SET statement is using normal expansion. The end result is each iteration is simply passing the current filename to mediainfo, which actually makes sense. It looks to me like you don't even need the _in variable in either block.
Your code is way more complicated than need be: I believe the following will give the result you are looking for:
#echo off
setlocal
echo. >info.txt
set /P "folder=Enter folder path: "
set /P "_input=Search all files (y/n): "
if /i "_input" == "y" (
set "_mask=*.wmv *.mpg *.mkv *.mpeg *.mp4 *.avi"
) else (
set /P "_mask=Enter file string: "
)
if /i "_input" neq "y" set "_mask=*%_mask%*"
pushd "%folder%"
set "popped="
for %%F in (%_mask%) do (
if not defined popped popd
set popped=1
mediainfo --Inform=file://template.txt "%%F" >>info.txt
echo. >>info.txt
)
pause
I would take it one step further and expect the user to supply any wildcards in the file string. That would give the user more control, and would simplify the code a bit more.
#echo off
setlocal
echo. >info.txt
set /P "folder=Enter folder path: "
set /P "_input=Search all files (y/n): "
if /i "_input" == "y" (
set "_mask=*.wmv *.mpg *.mkv *.mpeg *.mp4 *.avi"
) else (
set /P "_mask=Enter file mask: "
)
pushd "%folder%"
set "popped="
for %%F in (%_mask%) do (
if not defined popped popd
set popped=1
mediainfo --Inform=file://template.txt "%%F" >>info.txt
echo. >>info.txt
)
pause