How to read specific string in txt file by using windows batch? - windows

This is the content of my .txt file
123:456
789:333
I'm trying to use findstr to read string and search for 789:333, but it only print fist line 123:456
As I know, use cut can fulfill my requirement in Linux.
In Windows, do we have any method where we can search for a string in a file by using a batch-script?

it is simple. using a for loop.
#echo off
for /F "delims=" %%a in ('findstr /I "789:333" somefile.txt') do echo %%a
you can learn a lot about batch file commands by simply opening a cmd.exe window and typing help
It describes briefly each command and once you find one that you think might work, like let's say for, then you simply do for /? which will show you help content which will make your life easy.

Related

Using pipe command in a batch file

I'm trying to write a batch file that searches through devices using devcon and then runs "devcon enable" on these devices.
My batch file looks like this:
for /f "tokens=1 delims=:" %%i in ('devcon find *VENDER_INFO* ^| findstr /C:"DEVICEINFO"') do devcon enable "#"%%i
You can ignore the "VENDER_INFO" and "DEVICEINFO" as I simply redacted these to make the commands easier to read.
When I run this.bat file, It appears as though the
devcon find *VENDER_INFO* ^| findstr /C:"DEVICEINFO"
command is only running the first part and failing to pipe the results through
findstr /C:"DEVICEINFO"
Can you see anything wrong with my .bat file? Why is it ignoring the pipe and second part of the find command? I tried removing the "^" but this fails and says "|" was unexpected.
The find command requires double quotes.
Read the help with this:
find /?

How to use a command's return value with pipe as a variable in batch file

I want to extract a file in in windows, right after search for it, like this:
7z [args] | ls | grep filename
In my windows batch script, its:
7z [args] | dir /B | findstr filename
the search alone works perfectly like in linux, but i just cant pass it forward to a variable, or straight to 7z as an input.
I tried
pipe from the left
pipe from the right
for /f "delims=" %%a in ('dir /B ^| findstr onboard.zip') do #set Value=%%a
But all my solutions crashed with error.
My idea to pass the found file to any variable, OR give it straight forward to the extracting tool.
Do you have any working solution/workaround for this?
Thanks in advance!
After 1.5 days i realized that DIR have built in search, that can solve my problem without the pipe:
dir /b *filename
but still really interested in your - pipe included - solution :)
I am not quite sure of what you want. Does 7z produce a list of filenames?
for /f "usebackq tokens=*" %f in (`7z [args]`) do (if exist "%f" echo found file "%f")
If this is used inside a batch script, the percent characters must be doubled.

Renaming issue within command prompt

i'm trying to rename a file called Pawn.Stars.S01E02.Hello
i'm using
cmd ren "Pawn*Stars*" "Pawn Star$.*"
which does rename the file but renames it to Pawn Star$.Stars.S01E02.Hello
i have also tried
ren Pawn.Stars*.txt PawnStar$*.txt
which gives the same result.
The Name i'm trying to get is Pawn Star$.S01E02.Hello
Where am i going wrong or what needs to change?
You haven't stated what name you are trying to achieve, so I cannot give you a command that works.
But I can tell you that it is not possible to remove or change the dot between Pawn and Star$ using a simple REN command with wildcards. See How does the Windows RENAME command interpret wildcards? for an explanation.
One option would be to write a batch script to do the rename. There are also 3rd party utilities that provide more sophisticated file renaming capabilities.
Update based on comment from OP:
The following will do the rename from the command line:
for /f "delims=" %A in ('dir /b /a-d pawn.stars.*') do #for /f "delims=. tokens=2*" %B in ("%A") do #ren "%A" "Pawn Star$.%C"
Double up the percents if you use the command in a batch script.

How can I output a batch file to one line?

I'm new to scripting so I need some help with this. I've searched for two days and cannot grasp this!
What I'm doing is scanning a directory for particular files and then counting the results and piping it to a file.
dir *.DONE | find "04338" /c >>04338.txt
So the results look like this:
14
14
(blank line for carriage return)
My application is trying to read this file and run commands against it using RegEx. That part works fine except the application does not assign variables using multi-line match properly... it will grab the data fine but I cannot output it properly.
Because of this I have to write all of the data to one line. Eventually I want it to look like this:
14,14,14,14,14,14,14
I cannot for the life of me figure out how to get it onto one line. Please help!
try this:
#echo off
for /f "delims=" %%a in ('dir /b /a-d *.DONE ^| find "04338" /c') do <nul set/p "=%%a,">>4338.txt
type 4338.txt
Remove one , at the end of the line.

Delete a Batch file variable in a text file

I am currently trying to delete a variable from a batch file which is in a text file
e.g. delete %variable%
where %variable% = "test"
So the batch script would delete the instance of "test" in the specified text file
Can this be done?
#echo off
setlocal enabledelayedexpansion
set variable=test
for /f "delims=" %%l in (foo.txt) do (
set "line=%%f"
echo !line:%variable%=!
)
To avoid potential special character issues with the string you are searching for, it may be better to just call findstr directly like this:
type input.txt | findstr /b /e /v "remove_line" > input_without_remove_line.txt
/b and /e together will only match if the "remove_line" is the only text on a line. /v will switch the output to only print all lines that do not match.
If you are sure you're not passing special characters, it is pretty easy to wrap that in a small batch and replace remove line with %1 or %* to use your passed parameters.
Unfortunately, you will need to use a temporary file - replacing the file in place doesn't work with the DOS output redirection.
If you were wanting to delete all instances of a specified word within any line, batch and findstr are probably not the way to do it. Look at sed, which can do much more and is freely available. If you can't install another utility, even vbscript using cscript would be a better way to do this than batch.

Resources