I have a simple batch file, which reads a txt file containing simple lines and looks for a specific word in it, for example "Error". If the word is in the txt file, it opens a CMD with the message "FOUND". So far so good.
Now...is there a way to show the whole line in the CMD containing the searched word, for example:
"1. There is an Error in building A."
Thanks...
Use the findstr command, like this:
findstr /l /s Error file.txt
Related
I am making an inventory for which I need to copy all file names in an excel sheet. Right now I am using cmd command "dir /s /w /p > file.txt" for every folder. Problem is that when I get the txt files, file names are in same line, and I have to select them and copy paste in Excel sheet. I need a command that gives me all files names should come in new line so that I can easily copy all file names at once and paste them.
You are getting file names in the same line because you use the /w (wide) option, which does exactly this, check /w parameter here https://learn.microsoft.com/en-us/windows-server/administration/windows-commands/dir
To solve your problem simple don't use the /w parameter. You can see the result below
I have a text file that contains names of all the files in my directory followed by its date.
Is it possible in batch/bash to extract those files names from the text file and its date based on a value on the string.
I have already checked Extracting strings from text file
but this does not answer my query. I have also checked numerous others including Get specific string from a text file using batch command and seems to hit a dead end as it doesn't get all the lines from the whole text file.
a typical string/file name looks like this
5f367f72-9e2f-4f89-b3d6-2cdafed17d94_IRO_RMUSI_SONG_TimayaKingPerryy_KomKom.mxf_A1.MXF,25/07/2019 05:47:24
The identifier is IRO. i have more files in the text file as well but I want it to be able to look at the text file, find everything that has the identifier and make a new text file with the IRO file and date populated.
There are about 150,000 +/- line of string so manually doing this wont be easy.
If its possible to be done in a batch-file i would appreciate it.
That's quite easy if you think about it. Filter your file with findstr "_IRO_"), then split the filename from the date with a for /f loop (according to your example, they are delimited with a comma):
(for /f "delims=," %%a in ('type "names.txt" ^|findstr "_IRO_"') do #echo %%a) > "newnames.txt"
Note: to execute it directly from a command line instead of in a batch file, replace every %%a with %a.
I am trying to change the file name of "summary-SUCCESS.txt" or "summary-FAILED.txt" to add text to the front of the file name such as:
"Name1-summary-SUCCESS.txt"
"Name2-summary-SUCCESS.txt"
"Name3-summary-SUCCESS.txt"
... and so on
I am currently using the cmd
ren summary-* name1-summary*
and it takes away characters from the original file name, an example of what is output below:
"Name1-summary-S.txt"
"Nam1-summary-SS.txt"
"Na1-summary-ESS.txt"
"N1-summary-CESS.txt"
"N-summary-CCESS.txt"
"-summary-UCCESS.txt"
How would I go about adding a certain name to the beginning of a file name without losing any of the other characters in the original file name?
You need to use a for loop to iterate through all the matching files.
In a command prompt window (cmd) it looks like this:
for %I in ("summary-*.txt") do ren "%~I" "Name1-%~nxI"
In a batch script (batch-file) it looks like this:
for %%I in ("summary-*.txt") do ren "%%~I" "Name1-%%~nxI"
You may be interested in this thread on Super User:
How does the Windows RENAME command interpret wildcards?
EDIT: My essential question (without the specific setting for which I need a solution, as described in my original posting):
BinFile.bin is a file concatenated from binary files and a text file. The included text file consists only of lines beginning with a specific string, e.g. ;;;===,,,
With a batch file:
findstr /v "^;;;===,,," "BinFile.bin" > output.bin
an output bin file is generated in which the text file is completely removed.
How to use findstr (or another dos command) to not only remove all lines beginning with the specified string, but also the part of the bin before first such line (i.e. the complete binary part preceeding the text file)?
>>> My original posting:
jeb invented a method to concatenate files using Windows native tools which can be unconcatenated (in a specific way) using native tools. His solution is just ingenious!
copy /a batchBin.bat + /b myBinaryFile.bin /b combined.bat
with batchBin.bat:
;;;===,,,#echo off
;;;===,,,echo line2
;;;===,,,findstr /v "^;;;===,,," "%~f0" > output.bin
;;;===,,,exit /b
"The key is the findstr command, it outputs all lines not beginning with ;;;===,,,.
And as each of them are standard batch delimiters, they can be prefix any command in a batch file in any combination."
So myBinaryFile.bin can be extracted from the combined.bat––only by means of native tools!
My question:
In jeb's example the combined file is a batch file, because the first file in the copy command is a batch file. Could jeb's tricky method be used for the following task too, where the combined file would be combined.exe, an exe file?
copy /b aBat2ExeFile.exe + /a delimiter.bat + /b myBinaryFile.bin /b combined.exe
where delimiter.bat would be something like this:
;;;===,,,REM
and aBat2ExeFile.exe would be a batch file (aBat2ExeFile.bat) converted to exe, with a tricky use of findstr like in batchBin.bat, but with the result
[...] > output.exe
In aBat2ExeFile.bat findstr should be used with the result that all lines of combined.exe before and including the line ';;;===,,,REM' would be ignored and output.exe would be equal to myBinaryFile.bin again?
In think the concept is correct. But how this could be implemented in the aBat2ExeFile.bat?
EDIT: My question can be simplified (the frame described above is not essential):
How the findstr method used by jeb could be adapted to process a binary file in such a way that not only lines starting with ';;;===,,,' but also all lines preceding the first such line are "ignored"?
I would like a batch file that reads every line of a text file and creates a blank text file whose name corresponds to each line in the text file, regardless of spaces, and then puts them into C:\temp. I should be able to drag the text file onto the batch file to make it produce everything.
Example:
The original text file sample.txt says:
This is a file (haha)
This is another file
I need this file.
This should create these three files in the directory C:\temp:
This is a file (haha).txt
This is another file.txt
I need this file..txt
I think it should be something like this:
for /f %%i in (c:\temp\sample.txt) do echo %%i > "c:\temp\%%i.txt"
But this only makes a file for the first word in the text file, not each line.
Please help.
You need to add the "tokens=*" option so that it accepts characters like spaces. Your code would look like this:
for /f "tokens=*" %%i in (c:\temp\sample.txt) do copy /y nul "c:\temp\%%i.txt"