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 /?
Related
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.
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.
I have a list of files like these:
icone1.gif
icone2.gif
icone1.gif
icone11.gif
icone12.gif
icone13.gif
icone14.gif
icone15.gif
I want to remove 'e' from it so they will icon1.gif, icon2.gif and so on...
I tried this from the DOS Command Prompt:
ren icone*.gif icon*.gif
Didn't work.
Create a batch file in the folder by typing "notepad go.bat" at the command prompt and hitting enter and drop this in then save and exit notepad:
for %%i in ("*.gif") do (set fname=%%i) & call :rename
goto :eof
:rename
::Cuts off 1st five chars, then appends Icon and the remaining chars
ren "%fname%" "Icon%fname:~5%"
goto :eof
Double click the batch file in windows or from the command prompt type go and press enter
See How does the Windows RENAME (REN) command interpret wildcards? for rules that can explain why your command does not work.
Assuming all your file names have a digit after "icone" (really only care that you never have another "e" following "icone"), then the following one liner will work from the command line.
for /f "tokens=1* delims=eE" %A in ('dir /b icone*.gif') do #ren "%Ae%B" "%A%B"
Double up the percents if you put the command within a batch script.
For a really simple and robust solution, use my JREN.BAT regular expression renaming utility, pure script (hybrid JScript/batch) that runs natively on any Windows machine from XP onward.
jren "^(icon)e(.*\.jpg)" "$1$2" /i
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.
I need to find the location of a specific directory, and then store that directory path into a variable within a Windows batch script.
I also want the command to return when it finds a match (to avoid searching the entire hard drive once the directory has already been found).
So far I've tried this on the command line:
dir c:\ /s /b /ad | find "DirectoryName"
The problem with this is that it searches the entire drive, even after a match is found. Plus, I still can't figure out how to store the result in a variable within a batch file. There should only be a single result.
Basically I need the equivilent of somehting like this on Linux/bash:
export DIRPATH=`find / -name "DirectoryName" -print -quit`
Thanks for looking!
In batch you need FOR /F to get the output of a command.
FOR /F "usebackq delims=" %%p IN (`dir c:\ /s /b /ad ^| find "DirectoryName"`) DO (
set "DIRPATH=%%p"
)
echo %DIRPATH%
As there are quotes in the find command you need the usebackq-option.
And it's necessary to escape the pipe character one time, as it should pipe the dir command, not the for command