Source file is present in below manner:-
abc
dfc
adbc
I am using below code to print the each line in the file.
for /f "tokens=* delims=" %%a in ('type input.txt') do (
set line=%%a
echo %line%
)
but the output is
adbc
adbc
adbc
What to do? Required output is:
abc
dfc
adbc
Unless you specifically need to manipulate the line or save the content of the last line in a variable there is absolutely no need to use a For loop:
Type input.txt
#Squashman provided the answer really, but here it is written out:
setLocal EnableDelayedExpansion
for /f "tokens=* delims=" %%a in ('type input.txt') do (
set line=%%a
echo !line!
)
Related
I want to remove exactly three numbers after dot or if its easier everything after . and before ; using a Windows batch script.
Before
ABC;CDEF;GH;123:456.XXX;EFG;789:123.XXXABC;CDEF;GH;123:456.XXX;EFG;789:123.XXX...
After
ABC;CDEF;GH;123:456;EFG;789:123ABC;CDEF;GH;123:456;EFG;789:123...
I've been trying something with myVar:~0,-4 but I don't know how to use it when replacing with other string:
set "str=;"
for /f "tokens=* delims=." %%A in (%input%) do set myVar=%%A & echo !myVar:%myVar:~0,-4%=%str%! >> %output%
Using your examples this method doesn't need to know the token count.
#Echo Off
SetLocal EnableDelayedExpansion
Copy/Y "input.txt" "output.txt">Nul
(For /F "UseBackQDelims=" %%A In ("output.txt") Do (Set "OL="
For %%B In (%%A) Do Set "OL=!OL!;%%~nB"
Echo=!OL:~1!))>"input.txt"
::Del "output.txt"
Pause
If you're happy with the content of input.txt you may remove the first two characters, :: from line 7. If not, don't worry nothing is lost, you can delete input.txt and rename output.txt to input.txt.
I have used input.txt and output.txt, please adjust those two names as necessary
for /f "tokens=1-3 delims=." %%A in (%input%) do set "myVar1=%%A"&set "myVar2=%%B"&set "myVar3=%%C" & echo !myVar1!!myVar2:~3!!myVar3:~3! >> %output%
or
for /f "tokens=1-3 delims=." %%A in (%input%) do set "myVar2=%%B"&set "myVar3=%%C" & echo %%A!myVar2:~3!!myVar3:~3! >> %output%
would be my first attempt - assuming your structure contains exactly 2 .s which are each followed by 3 characters to be removed.
This method works with any number of dots placed at any positions:
#echo off
setlocal EnableDelayedExpansion
rem Get file lines
(for /F "delims=" %%a in (input.txt) do (
rem Split line at dots
set "line=%%a"
set "myVar="
for %%b in ("!line:.=" "!") do (
if not defined myVar (
rem Copy first part
set "myVar=%%~b"
) else (
rem Eliminate first three chars from rest of parts
set "part=%%~b"
set "myVar=!myVar!!part:~3!"
)
)
echo !myVar!
)) > output.txt
In below code i am tring to fetch the line no of string "AXX0000XXXA" from file data.txt,then fetching line by line and printing target.txt file,in between if the line reach the find line no i am adding one more line from file temp.txt.The code is working fine with the less nos of records(tested with 150 lines-File Size 100 kb),but when i am processing with 50K records(File Size 25MB) it is taking more then 25 minutes to process.could you please help me how i will process same in less time.
#echo off
setlocal enabledelayedexpansion
for /f "delims=:" %%a in ('findstr /n "AXX0000XXXA" "C:\Users\23456\Desktop\data.txt"') do (set find_line=%%a)
set /a counter=0
for /f "usebackq delims=" %%b in (`"findstr /n ^^ C:\Users\23456\Desktop\data.txt"`) do (
set curr_line=%%b
set /a counter=!counter!+1
if !counter! equ !find_line! (
type temp.txt >> target.txt
)
call :print_line curr_line
)
endlocal
:print_line
setlocal enabledelayedexpansion
set line=!%1!
set line=!line:*:=!
echo !line!>>target.txt
endlocal
Your code uses three Batch file constructs that are inherently slow: call command, >> append redirection and setlocal/endlocal, and these constructs are executed once per each file line! It would be faster to include the subroutine into the original code to avoid the call and setlocal commands, and an echo !line!>>target.txt command imply open the file, search for the end, append the data and close the file, so it is faster to use this construct: (for ...) > target.txt that just open the file once. An example of a code with such changes is in Compo's answer.
This is another method to solve this problem that may run faster when the search line is placed towards the beginning of the file:
#echo off
setlocal enabledelayedexpansion
for /f "delims=:" %%a in ('findstr /n "AXX0000XXXA" "C:\Users\23456\Desktop\data.txt"') do (set /A find_line=%%a-1)
call :processFile < "C:\Users\23456\Desktop\data.txt" > target.txt
goto :EOF
:processFile
rem Duplicate the first %find_line%-1 lines
for /L %%i in (1,1,%find_line%) do (
set /P "line="
echo !line!
)
rem Insert the additional line
type temp.txt
rem Copy the rest of lines
findstr ^^
exit /B
This should create target.txt with content matching data.txt except for an inserted line taken from tmp.txt immediately above the line matching the search string, AXX0000XXXA.
#Echo Off
Set "fSrc=C:\Users\23456\Desktop\data.txt"
Set "iSrc=temp.txt"
Set "sStr=AXX0000XXXA"
Set "fDst=target.txt"
Set "iStr="
Set/P "iStr="<"%iSrc%" 2>Nul
If Not Defined iStr Exit/B
Set "nStr="
For /F "Delims=:" %%A In ('FindStr/N "%sStr%" "%fSrc%" 2^>Nul') Do Set "nStr=%%A"
If Not Defined nStr Exit/B
( For /F "Tokens=1*Delims=:" %%A In ('FindStr/N "^" "%fSrc%"') Do (
If "%%A"=="%nStr%" Echo %iStr%
Echo %%B))>"%fDst%"
I have made it easy for you to change your variable data, you only need to alter lines 3-6.
I have assumed that this was your intention, your question was not clear, please accept my apologies if I have assumed incorrectly.
What I have to do is a batch script that:
_read a file line by line
_remove all the double quote characters writing the result in a file
My attempt was a script like:
for /f "usebackq tokens=*" %%a in ("%GRUPPI3%.txt") do (
SET VARIAB=%%a
SET RESULT=%VARIAB:"=%
echo %RESULT% >> output.txt
)
After some try I realized that the PROBLEM IS THE VARIABLE "VARIAB"!
Doing an echo of VARIAB the result is null or an old value (like an old line read by the variable %%a). (Why "VARIAB" doesn't become a copy of "%%a" as I would expect?)
I can't understand such a behaviour...
Someone know the solution?
Thanks
Cristian
#ECHO OFF
SETLOCAL
(
for /f "usebackq tokens=*" %%a in ("%GRUPPI3%.txt") do (
SET "VARIAB=%%a"
IF DEFINED variab (
SETLOCAL enabledelayedexpansion
SET "RESULT=!VARIAB:"=!"
ECHO(!RESULT!
ENDLOCAL
)
)
)>newfile.txt
GOTO :EOF
This should fix your problem. I changed the detination file to suit my system. If you want to append to the destination file, use >> in place of >
I'm trying to make a code which will get first words from all lines of HELP's output to a variable and echo this variable. Here is my code:
#echo off
set a=
for /F "tokens=1,*" %%i in ('help') do (
set a=%a% %%i
)
echo %a%
But it returns first word from only last line. Why?
Bali C solved your problem as stated, but it looks to me like you are trying to get a list of commands found in HELP.
Some of the commands appear on multiple lines, so you get some extraneous words. Also there is a leading and trailing line beginning with "For" on an English machine that is not wanted.
Here is a short script for an English machine that will build a list of commands. The FINDSTR command will have to change for different languages.
#echo off
setlocal enableDelayedExpansion
set "cmds="
for /f "eol= delims=." %%A in ('help^|findstr /bv "For"') do (
for /f %%B in ("%%A") do set "cmds=!cmds! %%B"
)
set "cmds=%cmds:~1%"
echo %cmds%
EDIT
Ansgar Wiechers came up with a more efficient algorithm to extract just the command names at https://stackoverflow.com/a/12733642/1012053 that I believe should work with all languages. I've used his idea to simplify the code below.
#echo off
setlocal enableDelayedExpansion
set "cmds="
for /f %%A in ('help^|findstr /brc:"[A-Z][A-Z]* "') do set "cmds=!cmds! %%A"
set "cmds=%cmds:~1%"
echo %cmds%
You need to use delayed expansion in your for loop
#echo off
setlocal enabledelayedexpansion
set a=
for /F "tokens=1,*" %%i in ('help') do (
set a=!a! %%i
)
echo %a%
Instead of using %'s around the a variable, you use !'s to use delayed expansion.
Because the echo is outside the do ( ...... )
#echo off
for /F "tokens=1,*" %%i in ('help') do (
echo %%i
)
and no need to print a, you can use directly %%i.
Another very simple example could be a batch like this saved as help1.cmd
#echo off
for /F "tokens=1,*" %%i in ('help') do (
if /I "%%i" EQU "%1" echo %%j
)
and you call this batch like
help1 MKDIR
to get the short help text for the MKDIR command
I have a txt file that contains the following lines
jfo3 93jfl
lvls 29fdj
nskd jfuwe
xlkw eklwe
I'm trying to read the file line by line, and do something with it. What delimiter should I use?
The delim I'm using here reads each word separately.
#echo off
setlocal EnableDelayedExpansion
for /f "delims=" %%x in (lines.txt) do (
echo %%x
)
This reads line by line for me:
for /f "delims=" %x in (lines.txt) do echo %x
The problem is not related to delims, but to tokens:
for /f "tokens=*" %%x in (lines.txt) do echo %%x
If this is your input file:
abc,def
ghi,jkl
mno,pqr
then use
FOR /F "tokens=1,2,3 delims=," %%i in (test.txt) do (whatever u want)