nightcrawler
nightcrawler

Reputation: 327

Findstr doesn't accept variable CMD

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

Upvotes: 0

Views: 295

Answers (1)

dbenham
dbenham

Reputation: 130849

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

Upvotes: 2

Related Questions