How can I output a batch file to one line? - windows

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.

Related

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

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.

Capturing first 4 characters of a Windows command line output to variable

I'm trying to capture the first 4 characters of output from the following Windows command.
nltest /server:%COMPUTERNAME% /dsgetsite
What is normally returned would be:
SITEAdSiteName
This command completed successfully.
I've tried using the for /F command but can't seem to figure out how to strip everything else except the first 4 characters of what is returned.
I'm thinking using the for /F may not be the best way to accomplish this.
Are there other suggestions on how I many accomplish this?
I think my challenge is defining (or not) the delimiter to being any character, I've tried the *, but didn't seem to do it for me.
When I use this:
for /F "tokens=1-4 delims=*" %A in ('nltest /server:%COMPUTERNAME% /dsgetsite') DO echo %A
I get both output lines, sort of stumped here.
To store the first line of the output of nltest /server:%COMPUTERNAME% /DSGETSITE in variable LINE, use the following command line (use %%F instead of %F to use this in a batch file):
set "LINE=" & for /F %F in ('nltest /server:%COMPUTERNAME% /DSGETSITE') do if not defined LINE set "LINE=%F"
To return the first four characters, use sub-string expansion:
set "LINE=%LINE:~,4%"
echo %LINE%

CMD Batch - Search for last occurence of character while looping through file

I have a .txt file which I loop through every line and spool to another file. Ok no problem so far. But I want NOT to spool lines, which have following criteria:
they contain more slashes. Find the last slash. After this one search the rest of the string for .*** (* = wildcard). If not found don´t spool, else spool.
Input file content for example:
c:/abc/abc/
c:/abc/abc/test.txt
c:/eee/
c:/eee/test.cfg
c:/test/abc/test/xxx/bbb/ccc/aaa/test.txt
c:/test/abc/test/xxx/bbb/ccc/aaa/
Output should look like:
c:/abc/abc/test.txt
c:/eee/test.cfg
c:/test/abc/test/xxx/bbb/ccc/aaa/test.txt
It is not static, where this lines appear, which should be removed. So I thought about finding the last slash and take all after that and look if there the last thing is ".***" If so keep else don´t echo
I don´t want to use other tools for this. It must be done via native command-line functionality.
Maybe somebody can help me out.
Code:
>OUTPUT.txt (
FOR /F "usebackq delims=" %%I IN ("FILE.txt") DO (
set "line=%%I"
setlocal enabledelayedexpansion
rem DO SOMEHTING HERE I DON`T KNOW HOW TO DO
echo(!line!)
)
)
just do it in one line using findstr and its regular expression mode (\....$ means all strings ending with . followed by 3 characters):
findstr /R \....$ FILE.txt
result:
c:/abc/abc/test.txt
c:/eee/test.cfg
c:/test/abc/test/xxx/bbb/ccc/aaa/test.txt

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.

Resources