Error when using findstr on file directory - batch-file

So I'm trying to learn the ins and outs of findstr since it's come up a few times on other batch script questions I've had. I'm trying to have it look for a word (in this case 'webview') through multiple files in a directory, ideally it would pull the line it was found as well as the file name. However, the program gets stuck in this sort of infinite loop with it and I have to force an exit. Any help on if its being caused by the findstr or what is causing it would be amazing as I've been staring at it for several hours now. My current code is below:
ECHO off
SETLOCAL enabledelayedexpansion
ECHO Please input the path to the app directory you'd like scanned
SET /p directorypath=
CD %directorypath%
ECHO Scanning files for Webview
(
FOR /F "delims=" %%a in ('findstr /I /S /M "webview" *.json') DO (
SET "line=%%a"
SET "line=!line:*webview=!"
FOR /F "delims=<" %%b in (!line!) DO ECHO %%b
)) > WebviewScanResults.txt
:eof
UPDATE: Code updated and functional for use as a reference. I pretty much just run the above code a couple of time with different file types replacing *.json and it works fine.

Just an untested try, too late for me:
#ECHO off
SETLOCAL enabledelayedexpansion
ECHO Please input the path to the app directory you'd like scanned
SET /p directorypath=
PushD "%directorypath%"
ECHO Scanning files for Webview
(
FOR /F "tokens=1*delims=:" %%a in ('findstr /I /S "webview" *.html') DO (
SET "line=%%b"
SET "line=!line:*webview=!"
FOR /F "delims=<>" %%b in ("!line!") DO ECHO %%a:%%b
)) > WebviewScanResults.txt

Related

Using batch file to list files created or modified in the last 7days

I'm trying to create a batch file that will look across a folder and its subfolders and list any file that has been created or modified within say the last 7 days. I only want to know files that meet the condition, not folders.
I've played around with DIR command, but whatever I do, it always seems to list everything.
I found this piece of code in Stackoverflow from 2017 which looked promising (I played around with the DIR switches), but it doesn't seem to create any output.
My knowledge of batch files and their commands is pretty limited. Running Windows 10.
TIA
Nigel
#echo off
setlocal EnableDelayedExpansion
echo Input the date(dd/mm/yyyy):
set /p compDate=
for /F "tokens=1-3 delims=/" %%a in ("%compDate%") do set compDate=%%c%%b%%a
echo Input the directory:
set /p directory=
SET Exit=%UserProfile%\Desktop\test.txt
pushd "%directory%"
(for /F "tokens=1-5*" %%a in ('dir /s /od /tc /a-d') do (
set "fileDate=%%a"
if "!fileDate:~2,1!!fileDate:~5,1!" equ "//" (
for /F "tokens=1-3 delims=/" %%x in ("!fileDate!") do set fileDate=%%z%%y%%x
if !fileDate! geq %compDate% (
set "fileSize= %%e"
echo %%a %%b %%c %%d !fileSize:~-16! %%f
)
)
)) > %Exit%
popd
If I misunderstood you, you can go deeper into the topic here
forfiles /P FOLDER_PATH\ /S /D -7

Batch - Returning full path from a dir /b

I am traversing folders on a drive, collecting file names with specific extensions, and building a string which is later used in a command line switch. When I find a qualifying file I need to know its full path as this is what is required by the command line. I currently use "%~dp0%%a\%%b" to build the full path, but I can see that may have limitations later on when the batch becomes more complex (e.g. it digs deeper into sub folders). I am hoping there is a way to replace "%~dp0%%a\%%b" with the path to the located file. Thank you:
#ECHO OFF
for /f "usebackq tokens=*" %%a in (`dir /b /a:d`) do (
pushd %%a
setlocal ENABLEDELAYEDEXPANSION
for /f "delims=" %%b in ('dir /b "*.E01" "*.L01" "*.AD1" 2^>nul') do (
SET EVIDENCE_STR=!EVIDENCE_STR! /e:"%~dp0%%a\%%b"
)
IF DEFINED EVIDENCE_STR (
ECHO !EVIDENCE_STR!
) ELSE (
ECHO No evidence files located in: %%a
)
endlocal
SET EVIDENCE_STR=
popd
)
PAUSE
Why do you need to create 2 loops, each running a dir command to find files? Why not just do for /R loop? Here is an example:
#echo off
set "files=*.E01 *.L01 *.AD"
for /R %%a in (%files%) do echo %%a
Simply use "Sub"-Option: /S of the DIR-Command:
Dir /B /S C:\*.jpg
cause it is that equivalent to:
#echo off
set "DirPath=C:\"
set "files=*.jpg"
for /R %DirPath% %%a in (%files%) do echo %%a
and so you should got the same result in each script.
The Problem: If you don't want any deep of SubDirectorys, you've to filter out these and so you may lose time - in both solutions.

Batch Script - Displaying files and directory in sorted order

So I'm trying to make my own dir command for cmd. So far it is working great, except I want to output the directories and files sorted by file extension, in the way
dir /o:ge
would display (folders first, then files sorted by file extension).
So far, my code looks like this
#echo off
rem Title
echo.
echo CURRENT DIRECTORY [%cd%]
echo.
rem Directories
for /d %%D in (*) do (
echo [DIR] %%~nD
)
rem Files
for %%F in (*) do (
echo %%~nxF
)
#echo on
This produces:
I'm not sure how to approach outputting the files sorted by file extension. I have searched the web and can't find a solution to this problem. I do realize batch script is very limited, but I still want to try and implement this. I have thought of using a for loop and storing all the file extensions into an "array" (if that exists in batch), and then outputting them by
*.fileExtension
Any suggestions?
Cheers,
Derek
As in my comment…
#Echo Off
Echo CURRENT DIRECTORY [%__CD__:~,-1%]&Echo(
For /F "EOL= Tokens=* Delims= " %%A In ('Dir /B/AD/ON') Do Echo [DIR] %%A
For /F "EOL= Tokens=* Delims= " %%A In ('Dir /B/A-D/OE') Do Echo %%A
Echo(&Pause>Nul
Alternatively…
#Echo Off
Echo CURRENT DIRECTORY [%__CD__:~,-1%]&Echo(
For /F "EOL= Delims=" %%A In ('Dir /OGE/-C'
) Do For /F "Tokens=3*" %%B In ("%%A"
) Do If "%%B"=="<DIR>" (If Not "%%C"=="." If Not "%%C"==".." Echo [DIR] %%C
) Else Echo %%C
Echo(&Pause>Nul

Search file with wildcard path

I want to write a script to prompt user for file path and list all files found. The file path can contain wildcards. Something similar to this. But the batch script version of it. For example:
C:\Somewhere\user*\app\version-*.*\start.exe
The files might be located like this:
C:\Somewhere\user345\app\version-1.0\start.exe
C:\Somewhere\user898\app\version-1.2\start.exe
C:\Somewhere\user898\app\version-1.3\start.exe
I tried to use FOR and it turns out to be so much harder than expected because FOR does not support wildcards in the middle of a path.
Is there a way to list these files? (Maybe without using for?)
I think this recursive solution works pretty well; you may name it WCDIR.bat:
#echo off
setlocal
if "%~1" neq "" set "next=%~1" & goto next
echo Show files selected by several wild-cards
echo/
echo WCDIR wildcardPath
echo/
echo Each folder in the path may contain wild-cards
echo the last part must be a file wild-card
goto :EOF
:next
for /F "tokens=1* delims=\" %%a in ("%next%") do set "this=%%a" & set "next=%%b"
if defined next (
for /D %%a in ("%this::=:\%") do (
setlocal
cd /D "%%~a" 2>NUL
if not errorlevel 1 call :next
endlocal
)
) else (
for /F "delims=" %%a in ('dir /B /A:-D "%this%" 2^>NUL') do echo %%~Fa
)
exit /B
EDIT: I fixed a small bug in the last for /F command.
For example, the output of WCDIR.bat C:\Windows\Sys*\find*.exe command in my Windows 8.1 64-bits computer is:
C:\Windows\System32\find.exe
C:\Windows\System32\findstr.exe
C:\Windows\SysWOW64\find.exe
C:\Windows\SysWOW64\findstr.exe
You can try with the command Where /?
The WHERE command is roughly equivalent to the UNIX 'which' command. By default, the search is done in the current directory and in the PATH.
#echo off
Where /R "%programfiles%" *winrar.exe
pause
#echo off
:: Example d'input
set UserInput=*drive*
:: building the Pattern
set cmd=%Userinput%.exe
:: storage Where.exe command in a macro, the execution will be faster
set whereCmd=where.exe /r c:\windows\ %cmd%
:: execution of macro and output formatting
for /f %%a in ('%whereCmd%') do echo %%~nxa --^> %%a
pause

Batch - Search for part/exact name and copy line from text file into batch as var

This information below is contained in a text file and formatted as such.
/var/www/xxx/html/videos/video_folder_1
/var/www/xxx/html/videos/video_folder_2
/var/www/xxx/html/videos/video_folder_3
/var/www/xxx/html/videos/video_folder_4
/var/www/xxx/html/videos/video_folder_5
/var/www/xxx/html/videos/video_folder_6
/var/www/xxx/html/videos/video_folder_7
I also have a variable called %file_name% in the batch file already defined.
So lets say that is it is %file_name% = V001-video_folder_6.mp4
As you can see there is some more extra information, V001- and .mp4.
I would like to use the var %file_name% to search the text file and return the entire line. In this case it would return /var/www/xxx/html/videos/video_folder_6 and then put this information in a new var, let us say, %folder_path%.
I think I would use findstr however I have been playing around and not getting the best results.
The problem with the methods that use findstr is that they are slow, because they require to execute findstr.exe (a ~30KB file) each time. A simpler/faster solution is to use just internal Batch commands with the aid of an array. If the number of names to process is large, the difference in time between the two methods may be marked.
#echo off
setlocal EnableDelayedExpansion
rem Load the lines from text file into an array with the last part as index:
for /F "delims=" %%a in (test.txt) do (
set "line=%%a"
for %%b in (!line:/^= !) do set "lastPart=%%b"
set "folder[!lastPart!]=%%a"
)
set "file_name=V001-video_folder_6.mp4"
rem Get the folder from file_name:
for /F "tokens=2 delims=-." %%a in ("%file_name%") do set "folder_path=!folder[%%a]!"
echo Folder path is: %folder_path%
Let us assume the posted lines are in file Test.txt in current working directory.
#echo off
set "file_name=V001-video_folder_6.mp4"
for /F "tokens=2 delims=-." %%A in ("%file_name%") do set "folder=%%A"
for /F "delims=" %%P in ('%SystemRoot%\System32\findstr.exe "/C:%folder%" Test.txt') do (
set "folder_path=%%P"
goto NextCommand
)
:NextCommand
echo Full folder path is: %folder_path%
Open a command prompt window, enter the command for /?, hit key RETURN or ENTER and read output help to understand this little code.
The command goto inside FOR loop results in an immediate exit from loop processing output of findstr.exe after first found line containing the folder path of interest.
Perhaps better in case of searched folder is not found in text file:
#echo off
set "file_name=V01-VIDEOS for school (Miss Patrick).mp4"
for /F "tokens=2 delims=-." %%A in ("%file_name%") do set "folder=%%A"
for /F "delims=" %%P in ('%SystemRoot%\System32\findstr.exe "/C:%folder%" Test.txt') do (
set "folder_path=%%P"
goto FoundFolder
)
echo "%folder%" not found in file Test.txt.
pause
goto :EOF
:FoundFolder
echo Full folder path is: "%folder_path%"
pause
This should work:
::file_name=V001-video_folder_6.mp4
::file containing folder paths is called paths.txt
for /f "tokens=2 delims=-." %%a in ("%file_name%") do set FN=%%a
for /f %%a in ('findstr /E /L "%FN%" "paths.txt"') do set folder_path=%%a
echo %folder_path%
Which does what you want in effectively two lines.

Resources