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
Related
I want to write a batch file to find all .vsdm files and the file name must contain a substring "2.4". But my code is telling me that all my .vsdm files contains the substring "2.4" which is not correct.
FOR /R %completepath% %%G IN (*.vsdm) DO (
set file=%%~nG
If not "%file%"=="%file:2.4=%" (
echo Filename contains 2.4
) else (
echo Filename does NOT contains 2.4
)
)
Can anyone tell me where did I get it wrong?Thanks
If "%file%"=="%file:2.4=%" (
echo Filename "%file%" does NOT contain 2.4
) else (
echo Filename "%file%" contains 2.4
)
Including the filename in the echo may reveal more. I can see no reason for the double-negative approach. The way the code operates may depend on precisely where in code the instructions are located, for instance if these lines are contained within any variety of loop or code-block, operation may depend on other elements, so it's important to present the code in-context and with an example of what was expected and what actually happened.
correct fomatting makes all clear.
There are one or two SO articles about delayed expansion which OP should become familiar with.
SETLOCAL ENABLEDELAYEDEXPANSION
FOR /R %completepath% %%G IN (*.vsdm) DO (
set "file=%%~nG"
If not "!file!"=="!file:2.4=!" (
echo Filename contains 2.4
) else (
echo Filename does NOT contains 2.4
)
)
ENDLOCAL
You can use the command Where /? that let you use wildcard characters ( ? * ) and UNC paths.
#echo off
Title Find the location of a file with substring by Hackoo
Color 0A
Call :inputbox "Enter the file name to search :" "Enter the file name to search"
If "%input%" == "" Color 0C & (
echo(
echo You must enter a filename to continue with this program
pause>nul & exit
) else (
Call :Browse4Folder "Select the source folder to scan %input%" "c:\scripts"
)
Set "ROOT=%Location%"
::We check whether the input string has an anti-Slach in the end or no ? if yes, we remove it !
IF %ROOT:~-1%==\ SET ROOT=%ROOT:~0,-1%
set whereCmd=where.exe /r %ROOT% %input%
for /f %%a in ('%whereCmd%') do echo %%~nxa --^> %%a
pause & exit
::***************************************************************************
:Browse4Folder
set Location=
set vbs="%temp%\_.vbs"
set cmd="%temp%\_.cmd"
for %%f in (%vbs% %cmd%) do if exist %%f del %%f
for %%g in ("vbs cmd") do if defined %%g set %%g=
(
echo set shell=WScript.CreateObject("Shell.Application"^)
echo set f=shell.BrowseForFolder(0,"%~1",0,"%~2"^)
echo if typename(f^)="Nothing" Then
echo wscript.echo "set Location=Dialog Cancelled"
echo WScript.Quit(1^)
echo end if
echo set fs=f.Items(^):set fi=fs.Item(^)
echo p=fi.Path:wscript.echo "set Location=" ^& p
)>%vbs%
cscript //nologo %vbs% > %cmd%
for /f "delims=" %%a in (%cmd%) do %%a
for %%f in (%vbs% %cmd%) do if exist %%f del /f /q %%f
for %%g in ("vbs cmd") do if defined %%g set %%g=
goto :eof
::***************************************************************************
:InputBox
set "input="
set "heading=%~2"
set "message=%~1"
echo wscript.echo inputbox(WScript.Arguments(0),WScript.Arguments(1)) >"%temp%\input.vbs"
for /f "tokens=* delims=" %%a in ('cscript //nologo "%temp%\input.vbs" "%message%" "%heading%"') do (
set "input=%%a"
)
exit /b
::***************************************************************************
I have created a batch file in a windows server to parse the name of the files stored in a folder.
The name of the file contains a set of parameters splitted by the hyphen, e.g.
ACC-INV-APR-2015
I need to check the syntax correctness of the first two parameters (department and document type) e.g. I would avoid that the hyphen is inserted more than a time in the file name.
On the basis of the check, the files with wrong syntax will be moved to a folder.
We have to consider that, apart the first two parameters that are mandatory, the other ones could be skipped and therefore the file name could have some repetitive hypens after the first two parameters, e.g.
FIN-DOC-APR-2015--MFH-P01
We would avoid to have some file name like: FIN--DOC-APR-2015-MFH-P01
I have created the following batch file but I don't know how to skip the filename with wrong syntax....
Thank you.
setlocal EnableDelayedExpansion
set source=\\fileserver\share$\archive
set dest_ok=\\fileserver\share$\fileok
set dest_not=\\fileserver\share$\error
FOR /R %source% %%f in (*.*) do call :Proc1 "%%f"
goto End
:Proc1
Set filename=%1%
For %%A in (%filename%) do (
Set Folder="%%~dpA"
Set Name=%%~nxA
)
for /f "tokens=1,2 delims=- " %%a in ("%Name%") do call :Proc2 %%a %%b
goto :eof
:Proc2
set department=%1
set typedoc=%2
FINDSTR /x "%department%" c:\0_scripts\arch\departments.txt
if %errorlevel%==0 FINDSTR /x "%typedoc%" c:\0_scripts\arch\typedocs.txt
if %errorlevel%==0 move /Y %filename% %dest_ok%
if %errorlevel%==1 move /Y %filename% %dest_not%
goto :eof
:End
Sounds like a job for regular expressions. The Windows utility findstr will let you match based on a regular expression. It exits status 0 if found, non-zero otherwise. This lends itself to conditional execution. In a cmd console, enter findstr /? for details on supported regexp syntax.
It'll also speed things up to cache the contents of departments.txt and typedocs.txt into variables, rather than open, read, close, repeat for each file checked.
So, with that in mind, here's how I'd do it:
#echo off
setlocal
set "source=\\fileserver\share$\archive"
set "dest_ok=\\fileserver\share$\fileok"
set "dest_not=\\fileserver\share$\error"
set "departments.txt=c:\0_scripts\arch\departments.txt"
set "typedocs.txt=c:\0_scripts\arch\typedocs.txt"
setlocal enabledelayedexpansion
for /f "usebackq delims=" %%I in ("%departments.txt%") do set "dept=%%~I;!dept!"
for /f "usebackq delims=" %%I in ("%typedocs.txt%") do set "type=%%~I;!type!"
endlocal & set "dept=%dept%" & set "type=%type%"
for /r "%source%" %%I in (*) do (
rem // Does filename match /^[a-z]+-[a-z]+/i ?
echo %%~nxI | findstr /i "^[a-z][a-z]*-[a-z][a-z]*" >NUL && (
rem // Yep. Check whether department and doc type is in allowed list.
for /f "tokens=1-2 delims=- " %%a in ("%%~nxI") do (
// if %dept% doesn't equal itself with %%~a removed, and so on...
setlocal enabledelayedexpansion
if not "%dept%"=="!dept:%%~a=!" if not "%type%"=="!type:%%~b=!" (
// All checks passed. Moved to OK folder.
move /y "%%~fI" "%dest_ok%"
)
endlocal
)
)
// If the file hasn't been moved by now, it must've failed a test.
if exist "%%~fI" move /y "%%~fI" "%dest_not%"
)
C:\0_scripts\arch>(
echo MFH3-FHW-20150529-F001MD14895-20301231-V01-OP20-TRIFLEX-CP1_H--.pdf | findstr /i "^[a-z][a-z]*-[a-z][a-z]*" 1>N
UL && (for /F "tokens=1-2 delims=- " %a in ("MFH3-FHW-20150529-F001MD14895-20301231-V01-OP20-TRIFLEX-CP1_H--.pdf") do (
setlocal enabledelayedexpansion
if not "TEF6;TEF10;QMM8;QMM73;QMM72;QMM71;QMM7;QMM6;QMM13;QMM1;QMM;MFP2;MFP1;MFH3;MFH2;MFH1;MFH ;MFG3;MFG22;MFG21;MFG2;
MFG11;MFG1;MFG;MFB;HSE;COS;" == "!dept:%~a=!" if not "WPL;WP;WBP;WB;WAL;WAG;WA;VTL;VTK;VDP;VBT;VBL;VB;VAW;VAP;VA;UVA;UMV
;TSS;TRN;TKU;TDC;SYM;SWD;SWC;SW;SVS;SVA;SV;STR;STL;STF;STB;SPC;SBT;SAM;RTZ;RTP;RPL;RP;RNO;RHW;RAW;QMP;QMA;QM;QBG;QB;QAM;
PZB;PUM;PRV;PRS;PRJ;PRA;PQP;PPM;PPK;PP;PNR;PLB;PH;PFH;PDV;PDR;PDC;PDB;PAP;PAL;PAG;OPS;OPL;OEE;NOR;NKA;MUB;MSZ;MON;MOD;MB
B;MNT;LZT;LZS;LZN;LPV;LPN;LPL;LPC;LPA;LHT;LDP;LBA;KSB;KPV;KPA;KOE;KOB;KBU;KBL;KB;IAM;HZG;HZ;HSE;HRB;HFG;HF;HE;HAZ;GMD;GE
Z;GBB;FVT;FRM;FPL;FPK;FPI;FPA;FP;FMP;FME;FMD;FMA;FLP;FLB;FIM;FHW;FGY;FGV;FGS;FGP;FGL;FGK;FGE;FGD;FGB;FGA;FDA;FA;EZZ;EWZ;
EWS;EVT;EV;ETZ;ETL;ESZ;EPB;EP;ECM;DVL;ECR;DV;DRX;DRW;DRV;DRQ;DRK;DRF;DMD;DIF;DLP;DER;DDI;DBL;DB;DAT;D01;CPC;CIP;CHL;CE;C
AP;BVT;BVS;BVB;BV;BUG;BSV;BST;BSS;BS;BPZ;BLD;BDL;BBL;BBD;BB;BAL;BAD;ANH;AGZ;AFK;AEN;AED;AAW;AA;" == "!type:%~b=!" (
move /y "\\server1\digit$\deposito\MFH3\MFH3-FHW-20150529-F001MD14895-20301231-V01-OP20-TRIFLEX-CP1_H--.pdf" "\\server1\digit$\errori"
pause
)
endlocal
) )
if exist "\\server1\digit$\deposito\MFH3\MFH3-FHW-20150529-F001MD14895-20301231-V01-OP20-TRIFLEX-CP1_H--.pdf" move
/y "\\server1\digit$\deposito\MFH3\MFH3-FHW-20150529-F001MD14895-20301231-V01-OP20-TRIFLEX-CP1_H--.pdf" "\\server11\digit$\ok"
pause
)
1 file(s) moved.
Press any key to continue . . .
Blockquote
I have changed the script and came back to your original version .
This is the output of the batch file when a file correct is processed:
if exist "\server1\digit$\deposito\MFH3\MFH3--FHW-20150512-F01MD14861-20301231-V02-OP20-TRIFLEX-CP1H--.pdf" move
/y "\server1\digit$\deposito\MFH3\MFH3--FHW-20150512-F01MD14861-20301231-V02-OP20-TRIFLEX-CP1H--.pdf" "\server1\
digit$\errori"
pause
)
1 file(s) moved.
Press any key to continue . . .
C:\0_scripts\arch>(
echo MFH3-AFK-20150511-F01MD12340-20301231-V07-OP20-TRIFLEX-CP1_H--.pdf | findstr /i "^[a-z][a-z]-[a-z][a-z]" 1>NU
L && (for /F "tokens=1-2 delims=- " %a in ("MFH3-AFK-20150511-F01MD12340-20301231-V07-OP20-TRIFLEX-CP1_H--.pdf") do (
setlocal enabledelayedexpansion
if not "TEF6;TEF10;QMM8;QMM73;QMM72;QMM71;QMM7;QMM6;QMM13;QMM1;QMM;MFP2;MFP1;MFH3;MFH2;MFH1;MFH ;MFG3;MFG22;MFG21;MFG2;
MFG11;MFG1;MFG;MFB;HSE;COS;" == "!dept:%~a=!" if not "WPL;WP;WBP;WB;WAL;WAG;WA;VTL;VTK;VDP;VBT;VBL;VB;VAW;VAP;VA;UVA;UMV
;TSS;TRN;TKU;TDC;SYM;SWD;SWC;SW;SVS;SVA;SV;STR;STL;STF;STB;SPC;SBT;SAM;RTZ;RTP;RPL;RP;RNO;RHW;RAW;QMP;QMA;QM;QBG;QB;QAM;
PZB;PUM;PRV;PRS;PRJ;PRA;PQP;PPM;PPK;PP;PNR;PLB;PH;PFH;PDV;PDR;PDC;PDB;PAP;PAL;PAG;OPS;OPL;OEE;NOR;NKA;MUB;MSZ;MON;MOD;MB
B;MNT;LZT;LZS;LZN;LPV;LPN;LPL;LPC;LPA;LHT;LDP;LBA;KSB;KPV;KPA;KOE;KOB;KBU;KBL;KB;IAM;HZG;HZ;HSE;HRB;HFG;HF;HE;HAZ;GMD;GE
Z;GBB;FVT;FRM;FPL;FPK;FPI;FPA;FP;FMP;FME;FMD;FMA;FLP;FLB;FIM;FHW;FGY;FGV;FGS;FGP;FGL;FGK;FGE;FGD;FGB;FGA;FDA;FA;EZZ;EWZ;
EWS;EVT;EV;ETZ;ETL;ESZ;EPB;EP;ECM;DVL;ECR;DV;DRX;DRW;DRV;DRQ;DRK;DRF;DMD;DIF;DLP;DER;DDI;DBL;DB;DAT;D01;CPC;CIP;CHL;CE;C
AP;BVT;BVS;BVB;BV;BUG;BSV;BST;BSS;BS;BPZ;BLD;BDL;BBL;BBD;BB;BAL;BAD;ANH;AGZ;AFK;AEN;AED;AAW;AA;" == "!type:%~b=!" (
move /y "\server1\digit$\deposito\MFH3\MFH3-AFK-20150511-F01MD12340-20301231-V07-OP20-TRIFLEX-CP1_H--.pdf" "\bars
rv11\digit$\ok"
pause
)
endlocal
) )
if exist "\server1\digit$\deposito\MFH3\MFH3-AFK-20150511-F01MD12340-20301231-V07-OP20-TRIFLEX-CP1_H--.pdf" move
/y "\server1\digit$\deposito\MFH3\MFH3-AFK-20150511-F01MD12340-20301231-V07-OP20-TRIFLEX-CP1_H--.pdf" "\server1\
digit$\errori"
pause
)
1 file(s) moved.
Press any key to continue . . .
I have an application that reads through all the files in the directory and will rename those files (if necessary) and create log files from the innards of them, however there are a couple of catches....
If the files 15th character is something other than a "." or "_", substring the 14 characters previous plus the 15th and store as variable.
If the files 15th character is a "." or "_" substring the 14 characters previous and store as variable.
For example, if filename is: DIY_0000000000a.xml the variable would be DIY_0000000000a and if the filename is: DIY_0000000000.xml the variable would be DIY_0000000000
There is a catch to all of this...
If the user picks Option 2, which is to rerun the application, then the application should rename all files in the directory to DIY_xxx_rerun.xml or DIY_xxxa_rerun.xml (should now explain the "_" in the aforementioned options.
The problem I have and what I need assistance with is that I can't seem to get the conditional logic correct. The application works, but if I have a mix of files in the directory (ie: DIY_0000000000a.xml, DIY_0000000000.xml & DIY_0000000000_rerun.xml, DIY_0000000000a_rerun.xml) it may work on one kind but not the other. Help is much appreciated.
Below is a snippet of my current code:
SETLOCAL EnableDelayedExpansion
:EXECUTE
::IF AN XML FILE EXISTS ADD IT TO A LOG FILE, PROMPT USER IF THEY WANT TO VIEW IT
ECHO.
ECHO =============================================
ECHO **SELECT YOUR OPTION BY USING YOUR KEYBOARD**
ECHO =============================================
ECHO.
ECHO 1 - Generate DIY Log Files for normal orders
ECHO 2 - Generate DIY Log Files for _rerun orders
ECHO 3 - EXIT
ECHO.
ECHO.
SET /P CHOICE=Type your option, then press ENTER:
IF %CHOICE%==1 (
SET RERUN=
GOTO PROCESS
)
IF %CHOICE%==2 (
SET RERUN=_rerun
GOTO PROCESS
)
IF %CHOICE%==3 EXIT
IF NOT '%CHOICE%'== SET CHOICE=%CHOICE:~0,1%
ECHO.
ECHO "%CHOICE%" is not a valid option.
PAUSE
GOTO EXECUTE
:PROCESS
IF EXIST DIY*.xml (
SET /a FILECOUNTER=0
ECHO.
ECHO Processing...
FOR /f "delims=" %%a IN ('DIR /b /a-d /on DIY*.xml') DO (
SET /a LOGCOUNTER=!FILECOUNTER!/1+1
SET ORDERNUMBER=%%a
IF "!ORDERNUMBER:~14,1!" NEQ "." (
IF "!ORDERNUMBER:~14,1!" NEQ "_" (
SET ORDERNUMBSTR=!ORDERNUMBER:~0,15!
) ELSE (
SET ORDERNUMBSTR=!ORDERNUMBER:~0,14!
)
)
SET ORDERNUMBSTR=!ORDERNUMBER:~0,14!
IF "%RERUN%"=="_rerun" (
REN !ORDERNUMBSTR!.xml !ORDERNUMBSTR!%RERUN%.xml >NUL 2>&1
) ELSE (
REN *!ORDERNUMBSTR!*.xml !ORDERNUMBSTR!.xml >NUL 2>&1
)
FOR /f "tokens=4 delims=<>" %%i IN ('TYPE !ORDERNUMBSTR!%RERUN%.xml ^|find "DIY_LOG_ID"') DO (
SET DIYLOGID=%%i
)
FOR /f "tokens=7 delims=<>" %%j IN ('TYPE !ORDERNUMBSTR!%RERUN%.xml ^|find "ACCOUNT_KEY"') DO (
SET ACCOUNTKEY=%%j
FOR /f "tokens=1-3 delims=-" %%x IN ("!ACCOUNTKEY!") DO (
SET CONTRACTNR=%%x
SET PLANSEQ=%%y
SET SUBSEQ=%%z
)
)
...
ENDLOCAL
Just needed a day to think about it...
Was able to get what I wanted by using the following FOR loop instead of IFs
FOR /f "delims=" %%a IN ('DIR /b /a-d /on DTL*.XML') DO (
SET /a LOGCOUNTER=!FILECOUNTER!/1+1
SET ORDERNUMBER=%%a
**FOR /f "tokens=1,2 delims=_." %%p IN ("!ORDERNUMBER!") DO (
SET ORDERNUMBSTR=%%p_%%q**
)
IF "%RERUN%"=="_rerun" (
REN !ORDERNUMBSTR!.XML !ORDERNUMBSTR!%RERUN%.XML >NUL 2>&1
) ELSE (
REN *!ORDERNUMBSTR!*.XML !ORDERNUMBSTR!.XML >NUL 2>&1
)
...
I have recently started to learn how to make batch files. I have a folder that contains bunch of internet related log files. When I run the .cmd file (located in the same folder) I want it to be able to find out how many log files are in the folder and make a numbered menu from it. So lets say there are twenty files in the folder, then the user must be able to select from 1 to 21. 21 will close the batch file. Here is what I have done so far:
#echo off
setlocal enableextensions enabledelayedexpansion
set RawData1=TempData%random%.tmp
set FileCtr=0
:MAIN
dir *.log /b | findstr /i /n ".log" > %RawData1%
for /f "tokens=1 delims=:" %%a in (%RawData1%) do set FileCtr=%%a
set /a ExitCode=%FileCtr% + 1
set UserChoice=%ExitCode%
echo.
echo +++++++++++++++++++++++++++
echo Weblog File Viewer
echo +++++++++++++++++++++++++++
for /f "tokens=1-2 delims=:." %%a in (%RawData1%) do echo %%a. %%b
echo %Exitcode%. To Quit.
set /p UserChoice= Choose item number from menu (%UserChoice%):
echo\
echo user entered: %UserChoice%
pause
:THEEND
del /q %RawData1%
So what this batch file can do for now is that it figures out the number of log files and makes a numbered menu from it. Of course it won't show the filetype which is how I wanted it. So "Kelley-Blue-Book.log" for example is shown only as "Kelley-Blue-Book". However, if the user selects say number 4 from the list the program will terminate because I couldn't figure out how to make it actually open the desired log file using notepad.
This should do what you want:
#echo Off
setlocal EnableDelayedExpansion
set "Count=0"
pushd "%~dp0"
echo.
echo +++++++++++++++++++++++++++
echo Weblog File Viewer
echo +++++++++++++++++++++++++++
for %%A in (*.log) do (
set /a "Count+=1"
set "Menu[!Count!]=%%~fA"
set "Number= !Count!"
echo !Number:~-3!. %%~nA
)
set /a "Count+=1"
set "Number= %Count%"
echo %Number:~-3%. To Quit.
:Prompt
set "UserChoice="
set /p "UserChoice= Choose item number from menu (%Count%):"
if not defined UserChoice goto Prompt
set "UserChoice=%UserChoice:"=%"
if "%UserChoice%"=="%Count%" goto Done
for /f "tokens=1,* delims==" %%A in ('set Menu') do (
if /i "Menu[%UserChoice%]"=="%%~A" (
notepad "%%~fB"
set "UserChoice="
)
)
if defined UserChoice echo Invalid Choice.
goto Prompt
:Done
popd
endlocal
exit /b 0
Let me know if you want any explanations.
#echo off
setlocal enableextensions
set RawData1=TempData%random%.tmp
rem Get numbered list of files
dir /b "*.log" | findstr /i /n ".log" > %RawData1%
rem We could use 0 as exitCode,
rem but to keep original behaviour
rem lets count the number of files
for /F "tokens=*" %%f in ('type %RawData1% ^| find /c /v "" ') do set /A ExitCode=%%f + 1
if %ExitCode%==0 (
echo No log files
goto endProcess
)
rem show menu
for /f "tokens=1-2 delims=:." %%a in (%RawData1%) do echo %%a. %%b
echo %Exitcode%. To Quit.
set UserChoice=%ExitCode%
set /p UserChoice= Choose item number from menu (%UserChoice%):
if "%UserChoice%"=="" goto :EOF
if "%UserChoice%"=="%ExitCode%" goto endProcess
rem Search indicated file in list
set SelectedFile=
for /f "tokens=2 delims=:" %%f in ('findstr /B "%UserChoice%:" %RawData1%') do set SelectedFile=%%f
if "%SelectedFile%"=="" (
echo Incorrect selection
goto endProcess
)
if not exist %SelectedFile% (
echo File deleted
goto endProcess
)
notepad %SelectedFile%
:endProcess
del /q %RawData1%
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