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

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%

Related

findstr not working as expected

I am trying to find last line in a text file using the regex ^.*\z, it's working fine in notepad++ but when I try it in cmd using findstr /R "^.*^Z" file.txt not working.
Open a command prompt window and run findstr /?. The output help explains what FINDSTR supports. The regular expression feature is limited in FINDSTR. It does not support all the features as supported by Boost Perl Regular Expression library used by many text editors in various versions.
This batch code could be used to get last non empty line from a file assigned to an environment variable:
#echo off
setlocal EnableExtensions DisableDelayedExpansion
set "LastLine="
if exist "file.txt" for /F "usebackq eol= delims=" %%# in ("file.txt") do set "LastLine=%%#"
echo Last line is: "%LastLine%"
endlocal
Command FOR skips all empty lines and by default also all lines starting with a semicolon. For that reason eol= is used to define form-feed control character as end of line. In case of last line of file surely never starts with ; it would be best to remove eol= from the FOR command line.
In case of file to process always has at least X lines, it would make sense to add to the FOR options after usebackq the option skip=X to skip the first X lines of the file for faster processing.
For details on command FOR open a command prompt window and run for /?.

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

Can text lines be modified while writting them to a file?

Can we add any string along with the TEXT we are writing or appending to a file.
For example
dir filepath/filename.txt >> file1.txt
I want to add a string (,ab) at the end of every line in file1.txt. In the same line and not in the next line.
somewhat like
source file
data1
data2
data3
target should be
data1,ab
data2,ab
data3,ab
You can use the FOR command for this purpose (see HELP FOR on the command prompt):
FOR /F "delims=" %i IN (filepath\filename.txt) DO ECHO %i,ab >> file1.txt
That will read each line from filepath\filename.txt and write it to file1.txt, with your ab appended.
Now, if you really wanted to execute the dir filepath\filename.txt command, and add the ab to its output, then you'd do the following
FOR /F "usebackq delims=" %i IN (`dir filepath\filename.txt`) DO ECHO %i,ab >> file1.txt
or
FOR /F "delims=" %i IN ('dir filepath\filename.txt') DO ECHO %i,ab >> file1.txt
Finally, note that if you want to put the above commands in a batch file, you need to escape the %i by writing %%i.
Additionally, to reduce noise if executed on the command line, you can use "#ECHO" instead of "ECHO", i.e. as #dbenham commented, prefix the DO-command with the "#" character.
Christian K's answer using FOR /F is how you would do this using pure batch. There could be complications depending on the command output.
By default, FOR /F will skip lines beginning with ; (the default EOL character). If you know of a character that cannot appear in the output, then you can simply set EOL to that character. But sometimes you have no control or knowledge of the potential output. There is an awkward syntax that disables both DELIMS and EOL, thus solving that problem.
Note that I prefix the DO command with # to prevent the command line from being echoed. I also enclose the entire construct within parentheses and redirect only once, as it is more efficient:
(for /f delims^=^ eol^= %A in ('someCommand') do #echo %A,ab)>file1.txt
But there is still a potential problem in that empty lines will be skipped. There is a solution using FINDSTR or FIND to prefix each line with the line number, and then remove the prefix within the loop. But that can slow things down.
It is much simpler to use my JREPL.BAT utility that performs a regular expression search and replace on text. This is also much faster if you are dealing with a lot of output.
The following will append ,ab to all lines of output, including empty lines:
someCommand | jrepl "$" ",ab" >>file1.txt
Your example implies that you do not want to append anything to empty lines. The following accomplishes that:
someCommand | jrepl ".$" "$&,ab" >>file1.txt

Why does FOR loop command not work in a batch file which works on Windows command prompt?

FOR /L %A IN (1,1,100) DO echo %A
The code above in a batch script results in this error:
A was unexpected at this time.
The same command line works fine on Windows command prompt.
What is the problem?
You need to use double percent characters:
FOR /L %%A IN (1,1,100) DO echo %%A
If you run FOR /? you'll find that the first paragraph after the parameter list starts as follows:
To use the FOR command in a batch program, specify %%variable instead
of %variable.
As an example, let's start with a simple FOR loop:
FOR %x in (*) DO ECHO %x
This will run just fine from a command prompt, printing out the name of each file in the current directory, but if we use this verbatim in a Batch file, we'll get an error saying this:
x was unexpected at this time.
This is because Batch files have some extra abilities that use the percent sign immediately followed by some text, so when FOR is called from inside a Batch file it instead looks for two percent signs. So if we want to use that same FOR loop in a Batch script, we need to replace each instance of %x with %%x.
What we end up putting in our Batch file is this:
FOR %%x in (*) DO ECHO %%x
The problem is with %, %A is for use on command lines only.
when used in batch files %A should be substituted with %%A.

Adding and removing codes in every line in windows

I have lines like the ones shown below.
abcbasndo
bacmaisca
ascmasoc
Now, I need to take out the first three characters of every line and add AAA at the start and end of each line, so that it looks like the one shown below.
AAAabcAAA
AAAbacAAA
AAAascAAA
I am using windows.
Please help.
This little cmd script will do the job for you:
#setlocal enableextensions enabledelayedexpansion
#echo off
for /f "delims=" %%a in (qq.txt) do (
set var=%%a
echo AAA!var:~0,3!AAA
)
endlocal
See the following transcript:
C:\Pax> type qq.txt
abcbasndo
bacmaisca
ascmasoc
C:\Pax> qq
AAAabcAAA
AAAbacAAA
AAAascAAA
The for loop grabs each line in the qq.txt file (without delims=, it would use spaces within the line as delimiters) and puts it in %%a.
The body of the for loop puts that value into var and then uses the substring operator to get the first three characters.
I haven't tested what will happen if the line has less than three characters since (1) you didn't specify what you expected; and (2) it should be fairly easy to expand this script to handle it.

Resources