I have a list in a text file.
Using a batch file, I need to search for references of each item in the list.
I will need to be able to determine where the item is referenced.
Here is a what I have tried:
REM WINDOWS COMPILE FORMS
cls
#echo off
for %%f IN (LIST.TXT) do
findstr /m "$item_name$" *.* > $item_name_$ || results.txt
if %errorlevel%==0 (
echo Found! logged files into results.txt
) else (
echo No matches found
)
pause
My problem is I cannot find a way to plug the item in my text list into the batch file.
you can achieve the results you want just using findstr command. Read HELP FINDSTR and then try
findstr /m /F:txt.lst "$item_name$"
Asuming that the elements on the list can be used as file names
for /f "tokens=*" %%f IN (LIST.TXT) do (
findstr /m /l /c:"%%f" *.* > "%%f"
)
For each element in the list, search the files for the element and output the list of files containing it to a file named as the element in the list
Related
With a bat file I sorted the subfolders list in a Directory but what are the 2 folders named "." 1 dot and ".." 2 dots ? actually they are not present in windows explorer and most of all how I can avoid to show them in the list ?
#echo off
setlocal EnableExtensions EnableDelayedExpansion
> ".\Utils\Check last modified Profile.txt" (
for /F "delims=" %%D in ('
dir %APPDATA%\Mozilla\Firefox\Profiles\ /A /all-D /TW /A:D /O:-DE
') do (
rem print each item:
echo %%~D %
)
)
endlocal
image here > https://imgur.com/8XyQk6q
dir %APPDATA%\Mozilla\Firefox\Profiles\ /A /all-D /TW /A:D /O:-DE ^|findstr /v /e /L /c:"."
worked for me.
The dir output is sent to findstr which allows through lines which do not (/v) end (/e) with the literal (/L) string "."
Directories cannot be created that end with .
I believe I do not need to comment further on the attributes-selection specification.
The ^ is required to have the pipe applied to the dir command, not the for.
I'll make no further comment on the attributes-selection specified.
. is the current directory. .. is it's parent directory.
Explorer hides them.
Dir C:\windows\.\.\..
I have decided to post this because the existing answers appear to be using the same crazy and incorrect options for the Dir command. The following examples use the appropriate findstr.exe method of eliminating the . and .. directory entries from your listing, sorted according to most recently modified to least recent. I have included the full path to findstr.exe to eliminate the possibility of %PATH% and/or %PATHEXT% modifications from not being able to locate it.
If you wanted the same output format, then this should work for you:
#For /F "Tokens=1*Delims=:" %%G In ('"Dir "%AppData%\Mozilla\Firefox\Profiles" /AD/O-DE 2>NUL|%SystemRoot%\System32\findstr.exe /ELNV ".""')Do #Echo(%%H
If you don't need the empty lines then, this should omit those for you:
#For /F "Delims=" %%G In ('"Dir "%AppData%\Mozilla\Firefox\Profiles" /AD/O-DE 2>NUL|%SystemRoot%\System32\findstr.exe /ELV ".""')Do #Echo %%G
And if you only needed the directory name lines, then perhaps this will satisfy you:
#For /F EOL^=^ Delims^= %%G In ('"Dir "%AppData%\Mozilla\Firefox\Profiles" /AD/O-DE 2>NUL|%SystemRoot%\System32\findstr.exe /ELV ".""')Do #Echo %%G
You can use and add this switch /B
#echo off
#for /F "delims=" %%D in ('
dir %APPDATA%\Mozilla\Firefox\Profiles\ /A /all-D /TW /A:D /O:-DE /B
') do (
echo %%~D
)
I have made a Windows batch file based on some examples I have found elsewhere.
What it does is it parses a folder for a specific file type (.mkv) and then runs mkvmerge.exe from the MKVToolNix folder.
The command produces an output, listing the different tracks in the container.
The core of the file is
set rootfolder="Z:\Movies"
for /r %rootfolder% %%a in (*.mkv) do (
for /f %%b in ('mkvmerge -i "%%a" ^| find /c /i "chapters"') do (
if [%%b]==[0] (
echo "%%a" has no chapters
) else (
echo Doing some interesting stuff!
)
)
)
The above example is just part of the file, rootfolder is set to the folder I want parsed, of course, and upon finding a file with chapters in it, it will run additional commands.
It all works beautifully but I also want to check for subtitles at the same time. The find command doesn't take regular expressions or I could just have added "chapters subtitles". My efforts using other commands, like findstr, haven't really worked.
How do I go about using RegEx here?
This is an example output, running mkvmerge.exe on an .mkv file
mkvmerge.exe -i "Snow White and the Seven Dwarfs (1937) [tt0029583].mkv"
File 'Snow White and the Seven Dwarfs (1937) [tt0029583].mkv': container: Matroska
Track ID 0: video (MPEG-4p10/AVC/h.264)
Track ID 1: audio (DTS)
Track ID 2: audio (AC-3)
Track ID 3: subtitles (HDMV PGS)
Track ID 4: subtitles (HDMV PGS)
Chapters: 26 entries
This example has both subtitle and chapter tracks and the batch file will find the keyword "chapters" (it's set to ignore case). I also want to catch the files that contain the keyword "subtitles" even when there are no chapters.
To clarify my intent here, I want the code to:
Parse through the given folder
For all .mkv files, do mkvmerge /i which will output (as text) the streams in that file
Look at that output and if it contains the word(s) "chapters" and/or "subtitles" trigger some action.
Since you appear to make no distinction between whether one string or the other (or both) is detected, then
#Echo off
set rootfolder="Z:\Movies"
for /r %rootfolder% %%a in (*.mkv) do (
mkvmerge -i "%%a" | findstr /i "^Chapters subtitles" >nul
if errorlevel 1 (
echo Neither Chapters nor subs found in "%%a"
) else (
echo Chapters or subs found in "%%a"
)
)
would likely be easier.
Thanks LotPings for your effort. I learned something about tokens that will be very useful. Your script also got me on the right track (learning a few more commands on the way.
Your script ended up looking like this:
:: Q:\Test\2018\05\27\SO_50555308.cmd
#Echo off
set rootfolder="Z:\Movies"
for /r %rootfolder% %%a in (*.mkv) do (
set "found="
for /f "tokens=1-4* delims=: " %%b in (
'mkvmerge -i "%%a" ^| findstr /i "^Chapters subtitles"'
) do (
if /i "%%b"=="Chapters" set found=1
if /i "%%e"=="subtitles" set found=1
)
if defined found (
echo Chapters or subs found in "%%a"
) else (
echo.
)
)
All I needed was to check whether one of my keywords were present in any of the tokens and then set a flag accordingly and after the loop do the appropriate action, resetting the flag for the next file.
Still unclear how a line with Subtitles would look like.
Presuming the values Chapters and Subtitles appear in 1st column
The for /f splits the lines at colon and space (adjacent delims count as one) into tokens 1=%%b and 2=%%c the possible unsplitted rest in token 3=%%d
The space separated (ORed) RegEx search words anchored ^ at line begin will only match chapters/subtitles.
:: Q:\Test\2018\05\27\SO_50555308.cmd
#Echo off
set rootfolder="Z:\Movies"
for /r %rootfolder% %%a in (*.mkv) do (
for /f "tokens=1-4* delims=: " %%b in (
'mkvmerge -i "%%a" ^| findstr /i "^Chapters subtitles"'
) do (
if /i "%%b"=="Chapters" if "%%c"=="0" (
echo "%%a" has no chapters
) else (
echo "%%a" has %%c chapters
echo Doing some interesting stuff!
)
if /i "%%e"=="subtitles" echo "%%a" %%b %%b %%d: %%e %%f
)
)
Thanks in Advance.
Using a DOS batch file, I am trying to read a text file that contains several full paths with quotes, separated by a space and write a new file containing one path per line.
For example, I want to turn this file:
"C:\path\filename.doc" "C:\path\filename.doc" "C:\path\filename.doc" "C:\path\filename.doc"
into this:
"C:\path\filename.doc"
"C:\path\filename.doc"
"C:\path\filename.doc"
"C:\path\filename.doc"
I have had some success using the wonderful repl.bat (by dbenham).
type "files.txt" | repl " " "\r\n" x l >"newfile.txt"
But when there are spaces in the filenames or path it breaks a new line in the middle of the path and wrecks it.
Ive tried passing as the search variable into repl using the escape character ^, i.e. repl "^" ^"" and other ways with no joy.
At the end of the day, I simply need to move all the files into another directory, and so was going to then pass the resulting text file to another bulk delete batch file for processing, but perhaps there is a better way im missing ?
This has a limitation in the length of the line, of around 8 KB.
Less than that and it will move the files to your new folder.
#echo off
for /f "usebackq delims=" %%a in ("c:\folder\file.txt") do (
for %%b in (%%a) do move "%%~b" "d:\existing\new\folder"
)
The code below should work to move all files in except the ones in the list.
It adds a hidden attribute to the files in the list, moves all the other files, then removes the hidden attributes again.
#echo off
for /f "usebackq delims=" %%a in ("c:\folder\file.txt") do (
for %%b in (%%a) do attrib +h "%%~b"
)
cd /d "c:\folder"
move *.* "d:\already\existing folder"
for /f "usebackq delims=" %%a in ("c:\folder\file.txt") do (
for %%b in (%%a) do attrib -h "%%~b"
)
Test code for Windows 2012 as mentioned in the comments
#echo off
(echo "c:\widget1\test 1.txt" "2:\widget2\test 2.doc")>"file.txt"
for /f "usebackq delims=" %%a in ("file.txt") do (
for %%b in (%%a) do echo move "%%~b" "d:\existing\new\folder"
)
pause
You could use the following batch file split.bat and call it redirecting the content of your text file into it and redirecting the output into another file like split.bat < files.txt > newfiles.txt:
#echo off
set /P INFILE=
call :SPLIT %INFILE%
exit /B
:SPLIT
shift
if "%~0"=="" exit /B
echo "%~0"
goto :SPLIT
If you do not provide an input file (< files.txt) the scripts prompts you for a space-separated list.
If no output file is given (> newfiles.txt), the created new-line-separated list is shown on screen.
Notice that this does not verify whether your input file fulfills the described formatting.
This method is limited to a list length of 1021 bytes (characters), everything after will be truncated!
Assuming you can guarantee that each file path is enclosed within double quotes, then you just need to tweak your REPL.BAT command a bit:
type "files.txt" | repl "(\q.*?\q) *" "$1\r\n" x >"newfile.txt"
But REPL.BAT has been superseded by JREPL.BAT - it has even more functionality, and a slightly different syntax.
A JREPL solution can be as simple as:
jrepl "\q.*?\q" $0 /x /jmatch /f file.txt /o newfile.txt
If you want, you can overwrite the original file with the result by specifying - as the output file.
jrepl "\q.*?\q" $0 /x /jmatch /f file.txt /o -
If each line in the original file is <8k, then the following pure batch script should work, and it is pretty simple:
#echo off
>newfile.txt (
for /f "delims=" %%A in (files.txt) do for %%B in (%%A) do echo %%B
)
How can I check contains of substring in string variable of batch file?
for example:
set var1=foobarfoo
set regexp=.*bar.*
if [check var1 by regexp] echo "YES"
In my case, it's must be only check by regular expression and only in .bat file.
Adjusting the answer found here give
#echo off
set var1=foobarfoo
set "regexp=.*bar.*"
echo %var1%
echo %regexp%
setlocal enableDelayedExpansion
echo(%var1%|findstr /r /c:"%regexp%" >nul && (
echo FOUND
rem any commands can go here
) || (
echo NOT FOUND
rem any commands can go here
)
I need help converting the following Linux/Mac commands into a Windows batch script.
find / -regex ``^.*/Excel_[a-z]*.xls'' -delete 2>/dev/null
find / -regex ``^.*/presentation[0-9]*[a-z]*.ppt'' -delete 2>/dev/null
Basically using regular expressions, I need to find any of the .xls/.ppt files (in the format above) in a given Windows box and delete them.
Sorry, I'm new to Windows batch files.
You really don't explain what your hieroglypics mean.
In a batch file,
#echo off
setlocal
dir /s "c:\local root\Excel_*.xls"
would show all of the files matching starting Excel_ with extension .xls
(and if that's what you want, simply replacing dir with del would delete them; adding >nul would suppress messages; adding 2>nul suppresses error messages)
If you want files starting Excel_ then followed by any alphas-only, then
for /f "delims=" %%a in ('dir /b /s /a-d Excel_*.xls ^| findstr /E /R "\\Excel_[a-z]*\.xls" ') do echo "%%a"
The dir produces a directory list in /b (basic) form (ie. filename-only) /s - with subdirectories (which attaches the full directory name) and the /a-d suppresses directorynames. This is piped to findstr to filter out the required filenames. The result is assigned to %%a line-by-line, and the delims= means "don't tokenise the data"
should show you all the files matching that criterion - but it would be case-sensitive; add /i to the findstr switches to make it case-insensitive. (/r means "regex" within findstr's restricted implementation; /e means "ends" - I tend to use these over $) The \\ in intended to implement escaped \ to ensure the filename match is complete (ie do't find "some_prefixExcel_whatever.xls) - I'll leave what \. is intended to do to your imagination...
(again, change echo to del to delete and add in the redirection palaver if required)
And the other regex - well, follow the bouncing ball. It would appear you want .ppt files with names starting presentation followed by a possible series of numerics then by a series of alphabetics. I'd suggest
findstr /e /r "\\presentation[0-9]*[a-z]*\.ppt" for that task.
Use PowerShell.
get-childitem | where-object { $_.Name -match '<put a regex here>' } | remove-item
get-childitem returns file system objects, and the where-object filter selects only those file system objects whose name property matches a regular expression. These filtered items are then passed through the pipeline to remove-item.
There is good information about the PowerShell pipeline in the about_pipelines help topic, which you can read using the following command:
help about_pipelines
Plain Batch can't do this task. But you could make use of tools like findstr which come with Windows and support Regex.
This line can be executed from CMD and deletes all files in the current folder which match the RegEx:
for /f "eol=: delims=" %F in ('findstr /r "MY_REGEX_HERE"') do del "%F"
So try to get your expected results by playing around with this command. If your fine with the output/results, you can embed this line in your batch script. (Be careful, when embedding this line in batchscript you have to double the percentage signs!)
for /f "eol=: delims=" %%F in ('findstr /r "MY_REGEX_HERE"') do del "%%F"
Here a small batch to do what you are looking for:
#echo off
set /p dirpath=Where are your files ?
:: set dirpath=%~dp0 :: if you want to use the directory where the batch file is
set /p pattern=Which pattern do you wanna search (use regex: *.xml e.g.) :
:: combinason /s /b for fullpath+filename, /b for filename
for /f %%A in ('dir /s /b "%dirpath%\%pattern%" ^| find /v /c ""') do set cnt=%%A
echo File count = %cnt%
call :MsgBox "Do you want to delete all %pattern% in %dirpath%? %cnt% files found" "VBYesNo+VBQuestion" "Click yes to delete the %pattern%"
if errorlevel 7 (
echo NO - quit the batch file
) else if errorlevel 6 (
echo YES - delete the files
:: set you regex, %%i in batch file, % in cmd
for /f "delims=" %%a in ('dir /s /b "%dirpath%\%pattern%"') do del "%%a"
)
:: avoid little window to popup
exit /b
:: VBS code for the yesNo popup
:MsgBox prompt type title
setlocal enableextensions
set "tempFile=%temp%\%~nx0.%random%%random%%random%vbs.tmp"
>"%tempFile%" echo(WScript.Quit msgBox("%~1",%~2,"%~3") & cscript //nologo //e:vbscript "%tempFile%"
set "exitCode=%errorlevel%" & del "%tempFile%" >nul 2>nul
endlocal & exit /b %exitCode%