I once got this program from somewere to make the text different colours and it works.
but, what I want to know is how does it work
> #echo off
> <nul set /p .=. > "%~2"
> findstr /v /a:%1 /R "^$" "%~2" nul
> echo(%DEL%%DEL%%DEL%
> del "%~2" > nul 2>&1
> goto :eof
findstr is a command that locates files containing a given string of plain text. An interesting feature of this command is that it lets you specify the colors it outputs in, which is exploited in your code.
Your code, which resembles some code over here, makes a file with the text you want to colorize and uses findstr to print out the name of the file. Then, it deletes the temporary file.
Related
I need a batch file that will add the text "Write In" in a new line at the beginning of the content of hundreds of .txt files without removing any existing text. I found something on here that did not work for me. Anyone have suggestions?
This is the code I was working with:
for /r %%a in (*.txt) do (
echo ---- %%a before ----
type "%%a"
echo --------------------
echo Write In > "%%a.tmp"
type "%%a" >> "%%a.tmp"
del "%%a"
move "%%a.tmp" "%%a"
echo ---- %%a after ----
type "%%a"
echo --------------------
)
pause
It did nothing
I would most probably do it like this:
rem // Create temporary header file:
> "head.txt" echo Write In
rem // Iterate all text files in current directory:
for %%F in ("*.txt") do (
rem /* Combine header and currently iterated text file into a temporary file;
rem there cannot arise any file name conflicts (like temporary files becoming
rem iterated also unintendedly, or temporary files overwriting files to handle),
rem because the extension of the temporary files differ from the text files: */
copy /B "head.txt"+"%%~F" "%%~F.tmp"
rem // Overwrite original text file by temporary file, erase the latter:
move /Y "%%~F.tmp" "%%~F"
)
rem // Erase the temporary header file:
del "head.txt"
I found a way that is not very clean and will take a while for bigger files, but it works out for me:
#echo off
for /r "%~dp0" %%f in (*.txt) do (
echo Processing file %%f
>"%%~f.2" (
echo "Text to append here"
type "%%~f"
)
del "%%~f"
ren "%%~f.2" "%%~nxf"
)
pause
Loops recursively through all .txt -files in the batch-files directory.
Creates a new file with the name oldName.txt.2 and files it with the text to add and the rest of the oldfiles content.
Deletes the old file and renames the new file to the name of the old one.
The addition of .2 to the file ending is needed to make sure it does not get processed again by the loop.
Of course you can add multiple lines by multiplying the echo lines.
You can as well add text to the end of the file like this with adding the echo lines after the type line.
You can simply use the Linux Sed command to insert a header into a file.
sed -i '1s/^/This is my header\n/' filename
e.g. sed -i '1s/^/Write In\n/' myfile.txt
This can be applied to multiple files under a directory:
For .txt files
for file in *.txt
do
sed -i '1s/^/This is my header\n/' $file
done
For CSV files
for file in *.csv
do
sed -i '1s/^/This is my header\n/' $file
done
Suppose we have 2 files
First.txt
123
456
And Second.txt
789;123
123;def
482;xaq
What i need is to find the lines in the second file only containing entries of the first file in first column (token 1, delim ; ).
This is what i need:
Output.txt
123;def
Of course,
findstr /g:first.txt second.txt
will output both lines:
789;123
123;def
Any idea how i can mix findstr and for /f to get the needed output?
Thank you!
If all of the elements in the first column are of the same length, then the simple answer would be
findstr /b /g:first.txt second.txt
Note however that if first.txt contains a line 12 then this would match 123;abc and 129;pqr in the second file.
You can take advantage of the super-limited regex capabilities of findstr and compare each line of first.txt to only the very beginning of each line of second.txt.
#echo off
for /F %%A in (first.txt) do findstr /R /C:"^%%A;" second.txt
The /R flag means that the search string should be treated as a regular expression. The ^ in the search string means that %%A comes at the very beginning of the line. The ; is a literal semicolon that will prevent the 123 line from picking up 1234;abcd in second.txt.
Without executing a separate findstr for each value and to avoid the problem with partial matches at the start of the line, you can try with
#echo off
setlocal enableextensions disabledelayedexpansion
( cmd /q /c"(for /f "delims=" %%a in (first.txt) do echo(%%a;)"
) | findstr /g:/ /l /b second.txt
What it does is read first.txt and echo each line with the delimiter. This output is retrieved by the findstr using /g:/ to use the standard input as the source for the elements to match, that will be considered as literals (/l) at the start of the line (/b) in the second.txt file
Is the general form for CSV. Note in batch %A becomes %%A.
for /f "delims=," %A in (csv.txt) do findstr /c:"%A" file2.txt
Here's the output
C:\Users\User>for /f "delims=," %A in (csv.txt) do findstr /c:"%A" csv1.txt
C:\Users\User>findstr /c:"55" csv1.txt
55,61,hi there, Good
C:\Users\User>findstr /c:"60" csv1.txt
54,60,hi there, Bad
C:\Users\User>findstr /c:"Bad" csv1.txt
54,63,hi there, Bad
54,60,hi there, Bad
C:\Users\User>findstr /c:"55" csv1.txt
55,61,hi there, Good
Contents of two files.
55,60
60,60
Bad,60
55,60
and
55,61,hi there, Good
54,62,hi there, Good
54,63,hi there, Bad
54,60,hi there, Bad
I have a txt file that contains about 500 values, one per line. I need to check to see of any of those 500 values appear in any of 6 csv files each containing 100k lines. I can search for one value in those 6 csv files using
for /f "delims==" %%f in ('dir /s /b "P:\*.txt"') do FIND /N "[SEARCHSTRING]" "%~1%%f" >> "C:\found.txt"
but how do I do multiple searches automatically via command-line or batch file (CaSe SenSiTIve)?
#ECHO OFF
SETLOCAL
SET "sourcedir=c:\sourcedir"
SET "destdir=C:\destdir"
for /f "delims=" %%a in ('dir /s /b "%sourcedir%\*.csv"') do (
FINDSTR /N /g:"yourtextfilecontaining500linestomatch.txt" "%%~fa") > "%destdir%\%%~nafound.txt"
GOTO :EOF
What you are asking is rather unclear. I used c:\sourcedir as the location of the .csv files and c:\destdir as the location for the reports. Replacing
FINDSTR /N /g:"yourtextfilecontaining500linestomatch.txt" "%%~fa") > "%destdir%\%%~nafound.txt with your original (with the double > would accumulate the lines into a single file - if that's what you want. As it stands, a new file will be created with name the same as your .csv+found.txt
An easy way is to use a batch script. You can loop through each of the files one by one. If you want to do them all at once you need to thread your program.
for /L %%A in (1,1,6) do (
Your code goes here
)
That batch script will loop six times. I am not really sure how you specify the file but if you loop through each file it will work.
So put your current batch script where I said "Your code goes here"
for /f "delims==" %%f in ('dir /s /b "P:\*.txt"') do FIND /N "[SEARCHSTRING]" "%~1%%f" >> "C:\found.txt"
But you need to edit it to point to the file that you want to search. If your files are 1.txt, 2.txt 3.txt then all you need to do is set your file name to the current loop iteration number.
I've been using variants of this shell function for years:
ematch () {
for f in $(find . -type f | grep -v '~' | grep -v \.svn\/) ; do
egrep "$1" "$f" /dev/null 2> /dev/null
done
}
-> ematch "(string1|string2|string3)"
Feel free to adapt to your needs and post your mods here.
I have devised a way to remove a files name from its path and extension (replacing the files name with an asterisk).
However the out put file adds an extra space just before the closing quotation marks and i dont know why or how to fix this?
#echo off
SET EXTENT=%~x1
SET PATH=%~dp1
SET /P FILETYPE=
rem SET FILETYPE="%PATH%*%EXTENT%"
echo %FILETYPE%
pause
Type C:\HELLO.txt | findstr /I /V /C:%FILETYPE% >>C:\TEMP.txt
DEL /S/Q "C:\HELLO.txt"
ren "C:\TEMP.txt" "HELLO.txt"
DEL /s/q "C:\TEMP.txt"
UPDATED
I changed this answer after gaining a better understanding of the question.
I think this code is close to what you want. You may need to play around with regexes for the FINDSTR command.
#ECHO OFF
SET EXT=%~x1
FINDSTR /I /V /C:%EXT% HELLO.TXT >>TEMP.TXT
DEL /Q HELLO.TXT
REN TEMP.TXT HELLO.TXT
I've made several attempts at this but get nothing but "can't open..." errors, so I'm asking here:
I want to find all instances of the string "SOME TEXT" within a directory full of HTML files. Then the search results should be output to a file in that same directory (D:\myfiles)
Here's a sample batch file that'll do the trick.
#echo off
setlocal
pushd D:\myfiles
rem case-insensitive search for the string "SOME TEXT" in all html files
rem in the current directory, piping the output to the results.txt file
rem in teh same directory
findstr /ip /c:"SOME TEXT" *.html > results.txt
popd
endlocal
Update: Some caveats to using findstr command.
If your string contains angle brackets, you have to escape them using the CMD escape character - ^. So, if you want to search for <TITLE>, you have to specify it as /c:"^<TITLE^>".
If you want only file names, change /ip to /im. Also, you can add /s to search subfolders. In general, you can play with the different findstr options as listed in findstr /?.
Findstr will find the text only in UTF-8 encoded files. If the HTML files are UTF-16 encoded (ie, each character takes two bytes), findstr will not find the text.
I would also suggest running the command without the piping to the results.txt first to get the right findstr options and make sure it outputs what you need.
for %%f in (*.html) do findstr /i /m /p /c:"SOME TEXT" "%%f" >> results.txt