Extract Strings from a text file into another text file? In .bat - windows

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.

Related

Unconcatenating files using jeb's tricky method

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"?

Renaming a computer against CSV from a batch file

We're doing a mass rename of computers at a company and while I know there is a way to do this mass rename via Powershell from a DC, there are a lot of computers that are turned off and the script will need to be run again or stragglers will need to be individually fixed.
We have a remote monitoring/management system for this company that can deploy .bat files to computers as soon as they turn on, so I figured this way would be more thorough, and also has less chance of screwing things up seeing as it's a computer renaming itself as opposed to one script renaming everything.
I came across this, and it seemed like a really good idea, but for some reason the string for "%name%" can't be found in my semicolon delimited text file.
Here's what I think the script is / should be doing:
The script finds the name of the computer that it's being run on, sets it as %oldname% variable. Then it searches the semicolon delimited text file. This file contains current names and desired new names of computers. The two names are delimited by semicolon.
So the script tries to find the previously found name, then move over to the second part of the line to find the new name (tokens2 delims=;).
Then it loads that into the newname variable, that way the wmic command can run using oldname and newname.
However, the batch file cannot find the string.
for /f %%i in ('hostname') do; set oldname=%%i
for /f "tokens=2 delims=;" %%i in ('type \\server\shared\test\names.txt ^| findstr /i "%name%"') do; set newname=%%i
wmic computername where name="%oldname%" call rename name="%newname%"
I get "Findstr: No search strings" This tells me that it cannot properly find one of the names in the text file, and I'm a bit lost. I don't know if it has to do with syntax or how the txt file is set up.
The text file is laid out as such (using three fake lines to demonstrate formatting):
Test-L7;New-3284
Kitchen-7;New-1249
MikeS-7;New-3290

I need a batch file that will create a directory of text files based on a text file

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"

How to find particular contents in list of files

Please can someone tell how to find particular pattern in list of files.
I use the command:
dir *.txt /b /s >> C:\Users\Amrendra\Downloads\NIT_Testing\fileList.txt
In fileList.txt I have a list of files.
Now I want to find a certain pattern say abc in all the files listed.
So I want each line containing that pattern to be written into a new file.
Please suggest as I am completely new to batch command.
Thanks
I'm not sure if I got you completely right. You've echoed the names of all .txt files from some folder into one text file. Now you want to check it line by line for filenames containing some substring in the file name. Is that correct? If it is, here is the solution:
#ECHO OFF
SET targetFile=C:\Some\Path\SomeTextFile.txt
SET sourceFile=C:\Some\Path\Source.txt
SETLOCAL EnableDelayedExpansion
TYPE NUL>%targetFile%
FOR /F "tokens=*" %%L IN (sourceFile) DO (
SET templine=%%L
SET templine=!templine:abc=!
IF NOT !templine!==%%L (
ECHO %%L>>%targetFile%
)
)
Let's take a look on how it works: first we put the path to the file containing the file names into the variable %sourceFile% and the path to the target file into the variable %targetFile%. We need EnableDelayedExpansion to be able to work with changing variables inside the FOR-loop. TYPE NUL>%targetFile% simply clears the target file in case there are some entries from former runs.
The main work is done inside the FOR-loop. We read the source file line by line and want to check if the line contains the substring we are looking for. To do this we first store the line (%%L) in a temporary variable. Then we replace any occurrences of the search string with an empty string, means we simply remove abc from the temporary string. Finally we compare our modified temporary string with the original one. If they are equal we can be sure that the serch string abc was not in the original string. The other way around, if the strings differ, the substring was in the original string. So in the second case we do ECHO %%L>>%targetFile% which means we write the whole file name containing the search substring into our target file. Et voilà, we're done!
EDIT: As you want to search all listed files for the substring, here the new code:
#ECHO OFF
SETLOCAL EnableDelayedExpansion
SET sourceFile=C:\Users\Amrendra\Downloads\NIT_Testing\fileList.txt
SET targetFile=C:\Users\Amrendra\Downloads\NIT_Testing\newList.txt
SET searchString=abc
TYPE NUL>%targetFile%
FOR /F "tokens=*" %%L IN (%sourceFile%) DO (
FINDSTR %searchString% "%%L">NUL
IF !ERRORLEVEL!==0 ECHO %%L>>%targetFile%
)
Here we are using findstr to check if a text file contains the string we are searching. As findstr usually would output the line which contains the searched string (which we don't want) we simply mute the command using >NUL. What we actually need is the ERRORLEVEL! If the string was found in a file findstr will set ERRORLEVEL to 0 or to 1 otherwise. So the only thing we have to do is to check if !ERRORLEVEL!==0.

Copying files from a list

I have a list of 4000 .tif images that I need copied from a folder containing 20,000+ images.
I am trying to use command prompt to do so by using code found from googling my problem.
the code is as follows:
for /f %a in H:\list.txt do copy %a H:\new
"H:\list.txt" is my list file
"H:\new" is where i want my files to be copied to
I am running this command in the file folder that contains the .tif files H:\Doc2 and I keep getting:
H:\list.txt was unexpected at this time.
What is causing the issue? Is my list not correctly set up?
It looks like this:
ABBA.TIF
ABBD.TIF
ABBQ.TIF
Do i need commas or colons after the file names?
H:\list.txt is a literal, you want to loop over each of the lines of the file. Check this out:
How do you loop through each line in a text file using a windows batch file?
for /F "tokens=*" %%A in (myfile.txt) do [process] %%A
Suggestion: tag this with "windows" as well.
Amir diagnosed your immediate problem - your code is missing the parentheses around the IN clause.
But you have another potential problem. Your list may include file names and or paths that contain spaces or other special characters. If so, then the name must be quoted.
The values in the list may already by quoted, or they may not. You can use the ~ modifier to strip any existing enclosing quotes that may or may not be there, and then explicitly add your own.
for /f %a in (H:\list.txt) do copy "%~a" H:\new
If you want to include the line in a batch file, then each % must be doubled as %%.

Resources