Sorry for my bad English.
I want to remove space at the last Property value of WMIC and add another string.
del /f /q "GPU.txt"
for /f "skip=2 tokens=2,3,4 delims=," %%a in ('"wmic path Win32_VideoController get Caption,CurrentHorizontalResolution,CurrentVerticalResolution /format:csv"') do (
SETLOCAL EnableDelayedExpansion
For %%# in (*) do (
SET var=%%~n#
Set MyVar=!var!
set MyVar=!MyVar: =!
)
echo %%a (%%b x !MyVar!)>>"GPU.txt"
)
Nothing to display.
Thanks.
Why does the code in question not work?
Please read this answer for details about the commands SETLOCAL and ENDLOCAL. On using SETLOCAL inside a FOR loop it is highly recommended to use also ENDLOCAL in same FOR loop or a stack overflow could occur during execution of the loop. This does not occur here because there are not many loop iterations, but ENDLOCAL should be used nevertheless in same FOR loop containing also SETLOCAL.
For %%# in (*) do in code of question processes each file name of non-hidden files in current directory. The file name without file extension is assigned to environment variable var. Next this variable is assigned to another variable MyVar without any modification, except the line Set MyVar=!var! would contain trailing spaces or tabs. And last all spaces are removed from string value of environment variable MyVar which is a file name.
I have no idea what this FOR loop for processing file names in current directory has to do with name of video controller and current horizontal and vertical resolution.
I recommend reading answer on Why is no string output with 'echo %var%' after using 'set var = text' on command line?
The closing round bracket ) in command line echo %%a (%%b x !MyVar!)>>"GPU.txt" is interpreted as end of command block of first FOR. It would be necessary to escape ) with ^ and use echo %%a (%%b x !MyVar!^)>>"GPU.txt" to get closing parenthesis interpreted by cmd.exe as literal character to output by echo on parsing the entire command block starting with ( on first FOR command line.
wmic.exe outputs the text UTF-16 Little Endian (two bytes per character) instead of ANSI encoded (one byte per character). FOR respectively cmd.exe has a bug on interpreting the Unicode encoded character stream as explained for example in detail at How to correct variable overwriting misbehavior when parsing output?
What you think is a space at end of vertical resolution value is in real a carriage return appended to this value because of wrong processing of Unicode output of wmic.exe by command FOR respectively cmd.exe.
What is a working code and why does it work?
One possible solution for this task is using this batch code:
#echo off
setlocal EnableExtensions DisableDelayedExpansion
(for /F "tokens=2-4 delims=," %%I in ('%SystemRoot%\System32\wbem\wmic.exe PATH Win32_VideoController GET Caption^,CurrentHorizontalResolution^,CurrentVerticalResolution /FORMAT:CSV ^| %SystemRoot%\System32\findstr.exe /R ",[0123456789][0123456789]*$"') do (
set /A CurrentVerticalResolution=%%K
call echo %%I (%%J x %%CurrentVerticalResolution%%^)
))>"GPU.txt"
endlocal
The Unicode output of Windows Management Instrumentation Command is redirected to FINDSTR which filters the output on lines ending with a comma and one or more digits. So heading line of CSV output is removed and also video controller lines ending with ,, without values for current horizontal and vertical resolution. Such a CSV line is output also on my Windows computer in addition to the CSV line with the correct values.
The problem with wrong carriage return at end of last value of current vertical resolution remains despite filtering output of wmic.exe with findstr.exe.
For that reason the current vertical resolution value assigned to loop variable K is not used directly in echo command line because this would result in ) overwriting first character of video controller caption string. The solution used here is using an arithmetic expression to assign the current vertical resolution value to the environment variable CurrentVerticalResolution. The carriage return at end of the integer value is interpreted like any other whitespace character at end of an expression on evaluation of the arithmetic expression which means it is ignored by cmd.exe. So environment variable CurrentVerticalResolution has assigned the value without the unwanted carriage return at end.
The code above avoids usage of SETLOCAL and ENDLOCAL by using command CALL to parse the echo command line twice as explained by:
How does the Windows Command Interpreter (CMD.EXE) parse scripts?
The echo command line with escaped ) and with %%CurrentVerticalResolution%% instead of %CurrentVerticalResolution% is already modified to the line below on first parsing the echo command line by cmd.exe before executing command FOR:
call echo %I (%J x %CurrentVerticalResolution%)
The remaining environment variable reference %CurrentVerticalResolution% is replaced by current value of this environment variable before executing command ECHO on second parsing on each iteration of the loop because of command CALL.
It would be also possible to use inside the FOR loop:
setlocal EnableDelayedExpansion
echo %%I (%%J x !CurrentVerticalResolution!^)
endlocal
i am trying to code a simple script in batch that can find and replace a line
so far, I've found a snippet that works perfectly fine for my purpose the only problem is that it removes empty lines
and i can't figure out why!!
I've tried to add another if statement in this for loop but I fail
also I found that there is a bat called JREPL, i tried to run few simple commands from the docs and i failed again XD
here is the snippet:
:Variables
set InputFile=t.txt
set OutputFile=t-new.txt
set _strFind= old "data"
set _strInsert= new "data";
:Replace
>"%OutputFile%" (
for /f "usebackq delims=" %%A in ("%InputFile%") do (
if "%%A" equ "%_strFind%" (echo %_strInsert%) else (echo %%A)
)
)
i was expecting that this snippet won't remove my empty lines
and i can't figure out why
I am posting this without testing, as I do not have the environment to test as we speak.
But to explain your issue, cmd will ommit empty lines as it is built that way. It is the same as setting a variable to nothing and expecting it to return a result, so we simply assign values to each line by sort of simulating a detection of line breaks (Don't know exactly how to explain that one) but nevertheless, we will add some additional characters to the lines to ensure we get line breaks, the just get rid of them once we have them, So here goes:
#echo off
setlocal enabledelayedexpansion
set inputfile=t.txt
set outputfile=t-new.txt
set _strfind=old "data"
set _strinsert=new "data";
for /f "tokens=*" %%a in ('type "%inputfile%" ^| find /v /n "" ^& break ^> "%inputfile%"') do (
set "str=%%a"
set "str=!str:*]=!"
if "!str!"=="%_strfind%" set "str=%_strinsert%"
>>%outputfile% echo(!str!
)
That should send to output file.. You can however make the output file the same as the input as it would then be the same as replacing the text inline in the original file. Once I am able to test, I will fix the answer if there are any issues with it.
As a side note, be careful of where you have additional whitespace in your variables you set. For instance:
set a = b
has 2 issues, the variable, containing a space after a will be created with the space. So it will be seen as:
%a %
The aftermath of this is that the value of the variable will start with a leading space, so when you expected b as the value, it in fact became b
Then lastly, it is alsways a good idea to enclose your variables with double quotes, simply again to eliminate whitespace, because:
set a=b
Even though you cannot see it with your naked eyes, contains a space at the end, so doing a direct match like:
if "b"=="b"
Will result in a false statement as in fact we have:
if "b"=="b "
So the correct statement would be to set variables as:
set "a=b"
if "%a%"=="b"
which will be a perfect match.
Note I posted this from my phone, so any spelling, grammar and code issues I will resolved as I go though my answer.
…and one way using JREPL
JRepl "old \qdata\q" "new \qdata\q;" /I /XSEQ /F "t.txt" /O "t-new.txt"
I have a problem with reversing string list in a batch script. Let say I have a list L=string1,string2,string3 I would like to obtain reversed list L=string3,string2,string1. Any ideas??
You may also use this shorter/simpler approach:
#echo off
setlocal EnableDelayedExpansion
set "L=string1,string2,string3"
echo Input =%L%
set "revL="
set "str=%L:,=" & set "revL=,!str!!revL!" & set "str=%"
set "revL=%str%%revL%"
echo Output=%revL%
This method use the same procedure of the other answers, but in less lines. If you want to know what happens here, remove the #echo off line and run it. ;)
Without knowing what your input looks like, this might be a first attempt:
#ECHO OFF
SETLOCAL EnableDelayedExpansion
SET var=abc,def,ghi
SET rev=
:LOOP
IF NOT "!var!"=="" (
FOR /F "delims=, tokens=1,*" %%F IN ("!var!") DO (
SET rev=%%F,!rev!
SET var=%%G
)
) ELSE (
SET rev=!rev:~0,-1!
GOTO ENDLOOP
)
GOTO LOOP
:ENDLOOP
ECHO reversed list is: !rev!
EDIT: As requested, here is an explanation how it works:
var is your starting list of strings separated by commas.
rev will be the reversed string. At the beginning this string is empty.
Now let's take a look at the loop:
In each iteration, we are separating our string into two parts: %%F and %%G. %%F will be everything before the first comma and %%G will be the rest of the string: FOR /F "delims=, tokens=1,*" %%F IN ("!var!"). delims=, means that we are using comma as delimiter. tokens=1,* means that the first found substring will be stored in %%F while the rest will be stored in %%G (%%F is defined for the first token so Windows command interpreter will put every token afterwards in G, H, I, and so on - as we are using *, everything will land in %%G). Finally, we take the first token of our string (%%F) and append ,!rev! to it. Then we set the remaining string list to everything behind the first comma (%%G).
In the first iteration, this loop does the following (pseudo code):
var=abc,def,ghi
rev=
split the string into %%F=abc and %%G=def,ghi
set rev to %%F,rev //means abc,
set var to var but without the first token //means def,ghi
In the second iteration:
var=def,ghi
rev=abc,
split the string into %%F=def and %%G=ghi
set rev to %%F,rev //means def,abc
set var to var but without the first token //means ghi
In the third iteration:
var=ghi
rev=def,abc
split the string into %%F=ghi %%G=
set rev to %%F,rev //means ghi,def,abc,
set var to var but without the first token //means empty string
Now, after jumping back to :LOOP, the if condition is no longer fulfilled as !var! has shrunk from formerly abc,def,ghi to now an empty string. So IF NOT !var!=="" becomes false and we are jumping to the ELSE clause.
There is one problem left: as we are constructing our reversed string by pre-appending the first token from the original list AND a comma, we will end up with a comma at the end of the reversed string list: ghi,def,abc,
SET rev=!rev:~0,-1! fixes this. It takes a "substring" from our string, starting at index 0 and finishing at "end-1". So this line simply removes the last , at the end of our string. Then we are jumping to :ENDLOOP.
Here is a batch file code assuming L=string1,string2,string3 is assigned to an environment variable:
#echo off
setlocal EnableExtensions DisableDelayedExpansion
set "ListLine=L=string1,string2,string3"
for /F "tokens=1* delims==" %%I in ("%ListLine%") do (
set "LineBegin=%%I"
set "ListItems=%%J"
)
set "ReversedItems="
for %%I in (%ListItems%) do call set "ReversedItems=%%I,%%ReversedItems%%"
set "ListLine=%LineBegin%=%ReversedItems:~0,-1%"
echo %ListLine%
endlocal
Windows command interpreter interprets a comma in list of strings in a simple FOR loop like a space character as it can be seen on running this batch file without #echo off from within a command prompt window. Therefore the second FOR loop runs first with string1 assigned to loop variable I, second with string2 and third with string3.
Command CALL is used to do a double processing of the command SET to avoid the requirement to use delayed environment variable expansion as explained by help for command SET output on running set /? in a command prompt window.
For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.
call /?
echo /?
endlocal /?
for /?
set /?
setlocal /?
Aacini, definitely has the fastest code out of all of the answers. This is some longer code that uses a similar SET trick.
#echo off
setlocal EnableDelayedExpansion
set i=1
set "x=abc,def,ghi"
set "x!i!=%x:,=" & set /A i+=1 & set "x!i!=%"
FOR /L %%G IN (%i%,-1,1) DO (
IF "%i%"=="%%G" (
set "reverse=!x%%G!"
) ELSE (
set "reverse=!reverse!,!x%%G!"
)
)
echo %reverse%
pause
Just some quick timed testing of all 4 answers. First one uses the original string 3 characters in each of the 3 comma separated fields. The second one uses 3 characters in 9 comma separated fields. Each time I tested running each one 100 times and calculated the average. The differences are negligible.
Average of 100 tries using 3x3
Aacini 0.39254
Squashman 0.39851
Michael 0.3999
Mofi 0.40434
Average 100 tries using 3x9
Aacini 0.39925
Squashman 0.40278
Michael 0.41457
Mofi 0.43397
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%
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