I have this script to replace some words at text file.
I don't know if is correct:
#echo off
setlocal EnableDelayedExpansion
set count=0
set j=0
set i=0
for /f "tokens=* delims=" %%c in ( '"type test.txt"') do (
SET string=%%b
echo %%c >>test2.txt
)
for /F %%a in (params.txt) do (
set array[!i!]=%%a
set /A i+=1
)
for /F "tokens=1 delims==" %%p in (params.txt) do (
set array2[!j!]=%%p
set /A j+=1
)
set /A i-=1
for /l %%r in (%count% , 1, %i%) do (
set replace=!array2[%%r]!
echo %repace%
set replaced=!array[%%r]!
echo replaced
set /A count+=1
call :reemplaza
)
:reemplaza
if %count% LSS %i% (
for /f "tokens=* delims=" %%b in ( '"type test2.txt"') do (
SET string=%%b
SET modified=!string:%replace%=%replaced%!s
echo.!modified! >>auxiliar.txt
)
pause
del test2.txt
ren auxiliar.txt test2.txt
)
First loop is correct but at second loop do this:
I wanna replace:
name=awachens for name=awachenss
So take next parameter and print:
name=awachens=surname=lol
Regards !
Related
I would like to call variables that contain other variables in their name when I have enabledelayedexpension so I would have concentric exclamation points in my variable call.
I apologize for the unclear wording; I'm not very familiar with this subject.
Here's a portion of my code that shows my issue:
set /a Freq[%%z]value[!value!]+=%%y
echo Freq %%z value !value! is !Freq[%%z]value[!value!]!
As is, batch interprets !Freq[%%z]value[!value!]! broken up into different variables with the !'s, and I don't think I can use %'s instead because I'm in a for loop where this variable is changing. I don't think I can use another for loop to replace !value! with a %%a either.
Here's a more complete look at my code:
set /a line=0
set /a goodLine=0
FOR /F "tokens=* delims=" %%x in (%file%) DO (
set /a line+=1
echo Line is !line!
set data[!line!]=%%x
set /a value=0
set /a checkGood=0
FOR %%y in (%%x) DO (
set /a value+=1
if !value!==1 (
set /a Freq=%%y
set /a checkFreq=%%y %% 10
if !checkFreq!==0 (
set /a checkGood=1
) else (echo bad)
) else (
if !checkGood!==1 (
for /l %%z in (40, 10, 330) do (
if !Freq!==%%z (
set /a Freq[%%z]value[!value!]+=%%y
echo Freq %%z value !value! is !Freq[%%z]value[!value!]!
set /a Freq[%%z]quantity[!value!]+=1
echo Freq %%z value !value! quantity is !Freq[%%z]quantity[!value!]!
)
)
) else (echo checkGood bad)
)
)
)
#ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
SET /a value=4
FOR /L %%z IN (1,1,5) DO set /a Freq[%%z]value[!value!]=%%z+10
SET Freq
ECHO ========================
FOR %%y IN (2,7) DO FOR /L %%z IN (1,1,5) DO (
set /a Freq[%%z]value[!value!]+=%%y
SET Freq[%%z]value[!value!]
CALL echo Freq %%z value !value! is %%Freq[%%z]value[!value!]%%
)
GOTO :EOF
Or you could use a for/f "tokens=2delims==" %%e in ('set Freq[%%z]value[!value!]') do echo %%e
depends on what you really want to do.
I have a Batch file with the following code:
#echo off
set "quelle=C:\Users\User-01\quelle_001.txt"
set "ziel=C:\Users\User-01\ziel_001.csv"
>"%ziel%" (for /f "usebackq tokens=1-9 delims=;" %%a in ("%quelle%") DO for /L %%z in (1 1 %%i) do echo %%a;%%b;%%c;%%d;%%e;%%f;%%g;%%h;%%i;)
Input:
TEST0;TEST1;WERT1;WERT2;WERT3;WERT4;WERT5;WERT6;3;
Output:
TEST0;TEST1;WERT1;WERT2;WERT3;WERT4;WERT5;WERT6;3;
TEST0;TEST1;WERT1;WERT2;WERT3;WERT4;WERT5;WERT6;3;
TEST0;TEST1;WERT1;WERT2;WERT3;WERT4;WERT5;WERT6;3;
Now I would like to have the following Output:
TEST0;TEST1;WERT1;;;;;;3;
TEST0;TEST1;WERT2;;;;;;3;
TEST0;TEST1;WERT3;;;;;;3;
because in %%i has the value 3.
If in %%i the value is 4 the output should be:
TEST0;TEST1;WERT1;;;;;;4;
TEST0;TEST1;WERT2;;;;;;4;
TEST0;TEST1;WERT3;;;;;;4;
TEST0;TEST1;WERT4;;;;;;4;
etc.
#echo off
SETLOCAL EnableDelayedExpansion
set "quelle=t.csv"
set "ziel=t1.csv"
>"%ziel%" (
for /f "usebackq tokens=1,2,* delims=;" %%a in ("%quelle%") DO (
set "i=0"
for %%k in (%%c) do set "nr=%%k"
for %%m in (%%c) do (
set /a i+=1
if !i! leq !nr! echo %%a;%%b;%%m;;;;;;!nr!;
)
)
)
type "%ziel%"
Input:
TEST0;TEST1;WERT1;WERT2;WERT3;WERT4;WERT5;WERT6;3;
TEST0;TEST1;WERTa;WERTb;WERTc;WERTd;WERTe;WERTf;4;
Output:
TEST0;TEST1;WERT1;;;;;;3;
TEST0;TEST1;WERT2;;;;;;3;
TEST0;TEST1;WERT3;;;;;;3;
TEST0;TEST1;WERTa;;;;;;4;
TEST0;TEST1;WERTb;;;;;;4;
TEST0;TEST1;WERTc;;;;;;4;
TEST0;TEST1;WERTd;;;;;;4;
#echo off
set "quelle=C:\Users\User-01\quelle_001.txt"
set "ziel=C:\Users\User-01\ziel_001.csv"
>"%ziel%" (for /f "usebackq tokens=1-9 delims=;" %%a in ("%quelle%") DO (
echo %%a;%%b;%%c;;;;;;%%i;
echo %%a;%%b;%%d;;;;;;%%i;
echo %%a;%%b;%%e;;;;;;%%i;
echo %%a;%%b;%%f;;;;;;%%i;
echo %%a;%%b;%%g;;;;;;%%i;
echo %%a;%%b;%%h;;;;;;%%i;
)
)
I've removed the For /L loop as it's purpose isn't clear, and the %%z value does not get used. The only thing it appears to do is cause the same line to be echoed multiple times depending on the value of the %%i token retrieved in the first loop.
This seems as if it will do what your samples show, is it what you mean?
#Set "quelle=C:\Users\User-01\quelle_001.txt"
#Set "ziel=C:\Users\User-01\ziel_001.csv"
#(For /F "UseBackQ Tokens=1-3,9 Delims=;" %%G In ("%quelle%") Do #For /F "Delims=0123456789" %%K In ("%%I") Do #For /L %%L In (1 1 %%J) Do #Echo %%G;%%H;%%K%%L;;;;;;%%J;)>"%ziel%"
Here i am printing line no 1 to 5 from a file to another file.its working fine but one small issue that the lines are trimming from left side,i do not want to trim,it should be same as the input file.
infile.txt:
<RCO-XXX-AGENT>
<CREATED>2018-06-28 10:19:09</CREATED>
<FORMAT>
<VARIABLE>
<EOR>/010</EOR>
<EOC>/009</EOC>
<CTR>5</CTR>
code:
echo off
setlocal enabledelayedexpansion
call :Print_Lines > outfile.txt
endlocal
:Print_Lines
setlocal enabledelayedexpansion
set cur=0
for /f "delims==" %%i in (infile.txt) do (
set /a cur=cur+1
if !cur! geq 1 (
if !cur! leq 5 (
for /f "tokens=*" %%j in ( "%%i") do (
echo %%j
)
)
)
)
endlocal
exit /b 0
goto :eof
outfile.txt:
<RCO-XXX-AGENT>
<CREATED>2018-06-28 10:19:09</CREATED>
<FORMAT>
<VARIABLE>
<EOR>/010</EOR>
The second loop is useless
It should work :
echo off
setlocal enabledelayedexpansion
call :Print_Lines > outfile.txt
endlocal
:Print_Lines
setlocal enabledelayedexpansion
set cur=0
for /f "delims==" %%i in (infile.txt) do (
set /a cur=cur+1
if !cur! geq 1 (
if !cur! leq 5 (
echo %%i
)
)
)
endlocal
exit /b 0
goto :eof
In your exemple, the spaces at left are not printed because they are considered as delimiter character
I would suggest this method:
#Echo Off
Set "Num=5"
<"infile.txt" (For /L %%A In (1,1,%Num%) Do (Set "_="
Set /P "_="
SetLocal EnableDelayedExpansion
Echo=!_!
EndLocal))>"outfile.txt"
Where you can adjust the value to Num as necessary.
Here is my code
ECHO off
CLS
ECHO List of VMs:
ECHO .............
cd /D "F:\VMs"
setlocal enabledelayedexpansion
set num=0
for /f "tokens=*" %%i in ('dir /s/b *.vmx') do set /a num+=1&set VM[!num!]=%%i
set numberOfVMs=0
for /F "tokens=2 delims==" %%s in ('set VM[') do (
set /a numberOfVMs+=1
echo !numberOfVMs! -^> %%s
)
ECHO.
ECHO List of Running VMs:
ECHO .....................
set num=-1
for /f "tokens=*" %%i in ('vmrun -T ws list') do (
set /a num+=1
if !num! NEQ 0 set RUNNINGVM[!num!]=%%i
)
set numberOfRunningVMs=0
for /F "tokens=2 delims==" %%s in ('set RUNNINGVM[') do (
set /a numberOfRunningVMs+=1
echo !numberOfRunningVMs! -^> %%s
)
ECHO.
ECHO List of Available VMs:
ECHO .......................
for /l %%x in (1, 1, %numberOfVMs%) do (
::echo !VM[%%x]!
for /l %%y in (1, 1, %numberOfRunningVMs%) do (
echo !VM[%%x]!
echo !RUNNINGVM[%%y]!
if "!VM[%%x]!"=="!RUNNINGVM[%%y]!" (
echo "success"
)
)
)
Array varibale 'VM' has below values:
F:\VMs\PD-UI-Tests-Win10-x64-Office2016-32bit.vmwarevm\Windows-10-x64.vmx
F:\VMs\PD-UI-Tests-Win10-x64-Office2016-64bit.vmwarevm\Windows-10-x64.vmx
F:\VMs\PD-UI-Tests-Win7-x64-Office2013-32bit.vmwarevm\Windows7-x64-RTM.vmx
Array varibale 'RUNNINGVM' has below values:
F:\VMs\PD-UI-Tests-Win7-x64-Office2013-32bit.vmwarevm\Windows7-x64-RTM.vmx
But, the final if condition never becomes 'true' although the VM[3] is equal to RUNNINGVM[1]. What am i missing? Please help
Don't use ::-style comments within a code-block as it is actually a broken label, and labels break code-blocks. Use rem instead.
#ECHO OFF & SETLOCAL
copy /Y C:\LOG.DIR LOG.DIR
set "old=INACTIVE"
set "new=ACTIVE"
FOR /f %%a IN ('^<log.DIR find /v /c ""') DO SET /a length=%%a
SET /a length-=20
SETLOCAL ENABLEDELAYEDEXPANSION
<log.DIR (
FOR /l %%a IN (1,1,%length%) DO (
SET "line="
SET /p "line="
ECHO !line!
))>newfile
ENDLOCAL
for /f "skip=%length% delims=" %%a in (LOG.DIR) do (
set "str=%%a"
SETLOCAL ENABLEDELAYEDEXPANSION
set "str=!str:%old%=%new%!"
>>newfile ECHO !str!
endlocal
)
I just want to change last 20 lines from INACTIVE to ACTIVE. And if file has less than 20 lines, all lines of file should be changed.
But currently the new file is empty, if the number of lines in file is less than 20.
What to change to avoid an empty new file?
#echo off
setlocal enableextensions enabledelayedexpansion
copy /y c:\log.dir log.dir
set "old=INACTIVE"
set "new=ACTIVE"
for /f %%a in ('^<log.dir find /v /c ""') do set /a "length=%%a"
if %length% gtr 20 ( set /a "startPoint=length-20+1" ) else ( set "startPoint=1" )
<log.dir (
for /l %%a in (1,1,%length%) do (
set "line="
set /p "line="
if not defined line (
echo(
) else if %%a lss %startPoint% (
echo(!line!
) else (
echo(!line:%old%=%new%!
)
)
)>newfile
endlocal
Adjust where to start depending of the file length. If you have more than 20 lines, the starting point is calculated. If you have less lines, process from the start.