Hi i am trying to replace the contents of a text file using batchscript.
setlocal enableextensions enabledelayedexpansion
set line=0
set position=0
set replacetoken=POSITION
set newfile = new.txt
for /f "tokens=* delims=" %%x in (resproprty.txt) do (
#echo off
set /a line+=1
if not !line!==1 (
set /a position+=1
)
set newcount=0
set newcount=!position!
set content=%%x
echo !content!
set content=!content:%replacetoken%=POSITION#!position!!
echo !newcount!
echo !position!
#echo on
echo !content! >> NEW.TXT
)
set test=Hello!
echo %test%
endlocal
here i am trying to replace my token POSITION with POSITION#1 i.e POSITION#number which is being incremented. however its not working !position! is not giving any output, i even tried %position% but that just gives output as 0. please help
Try like this :
#echo off
if exist output.txt del output.txt
setlocal EnableDelayedExpansion
set /a $count=1
for /f "delims=" %%a in ('type "resproprty.txt"') do (
set line=%%a
call set line=!line:POSITION=POSITION#%%$count%%!
echo !line!>>output.txt
set /a $count+=1
)
Echo ok
That will create the file output.txt with the change done
#ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
(
FOR /f "tokens=1*delims=:" %%a IN ('findstr /n /r "." q22988413.txt') DO (
SET "line=%%b"
SET "line=!line:position=position#%%a!"
ECHO(!line!
)
)>newfile.txt
GOTO :EOF
I used a file named q22988413.txt containing
Hello position one
Hello position two
Hello position three
Hello position four
Hello position five
which produced this output in newfile.txt
Hello position#1 one
Hello position#2 two
Hello position#3 three
Hello position#4 four
Hello position#5 five
Related
I am new to batch scripting and would like to process my input file with the content :
MD5 hash of sample.js,
81f87dd81ef88f59a57d95d9ede5f92e
MD5 hash of searchReplace.js,
3493b216e1024f0d6de417ef6c8b3962
MD5 hash of Select Anything.js,
009f2b911b50550502b87aeeeb969b55
The output should look like :
MD5 hash of sample.js,81f87dd81ef88f59a57d95d9ede5f92e
MD5 hash of searchReplace.js,3493b216e1024f0d6de417ef6c8b3962
MD5 hash of Select Anything.js,009f2b911b50550502b87aeeeb969b55
Can someone please help me out with it ?
try this:
#echo off
setlocal enableDelayedExpansion
set "info_file=.\example1.txt"
for /f "useback tokens=* delims=" %%a in ("%info_file%") do (
set "line=%%a"
if "!line:~0,3!" EQU "MD5" (
set "to_echo=!line!"
) else (
set "to_echo=!to_echo!,!line!"
echo !to_echo!
)
)
endlocal
This works:
#echo off
setlocal
call :procFile < input.txt > output.txt
goto :EOF
:procFile
set /P "line1="
if errorlevel 1 exit /B
set /P "line2="
echo %line1%%line2%
goto procFile
You may also read the file via a for /F command this way:
#echo off
setlocal EnableDelayedExpansion
set "line="
(for /F "delims=" %%a in (input.txt) do (
if not defined line (
set "line=%%a"
) else (
echo !line!%%a
set "line="
)
)) > output.txt
I have a text file like this
myFile.txt:
apple
banana
grapes
I want to drag text file to batch file and set variables into an array like this:
array[0]=apple
array[1]=banana
array[2]=grapes
But i couldn't do that. My problem is not just printing them but i can't even do that. I'll do parse operations at the rest of batch file.
My Code:
#echo off
setlocal EnableDelayedExpansion
set i=0
for /f %%a in %1 do (
set /a i+=1
set array[!i!]=!a!
)
echo %array[0]%
echo %array[1]%
echo %array[2]%
endlocal
#echo off
setlocal EnableDelayedExpansion
set i=0
for /f "usebackq" %%a in ("%~1") do (
set /a i+=1
set array[!i!]=%%a
)
echo %array[1]%
echo %array[2]%
echo %array[3]%
rem Or:
for /L %%i in (1,1,%i%) do echo !array[%%i]!
endlocal
pause
I suggest you to read this answer.
I have a textfile (filename.txt) which contains
ProductABC_Test.txt
ProductDEF_Test.txt
ProductHIG_Test.txt
ProductIJK_Test.txt
I will be getting a variable passed (ex: product=ABC which will be substring of ProductABC_Test.txt). So I need to fetch the correct test name (ProductABC_Test.txt) from the filename.txt.
I have tried the following code -
SETLOCAL ENABLEEXTENSIONS
#echo off
set product=ABC
SETLOCAL EnableDelayedExpansion
for /F "tokens=*" %%A in (filename.txt) do
(
set str=%%A
if NOT %str% == !%str:product=%
(
set test_suite=%%A
)
)
ENDLOCAL
echo %test_suite%
But I am not getting the right result.
You can use the following code to look for the line that contains your substring:
#echo off
SETLOCAL ENABLEEXTENSIONS
set product=ABC
set test_suite="not found"
SETLOCAL EnableDelayedExpansion
for /F "tokens=*" %%A in (filename.txt) do (
set str=%%A
Echo.!str! | findstr /C:"!product!">nul
if !errorlevel!==0 set test_suite=!str!
)
echo %test_suite%
pause
I want to replace new line char to "|" use Window bat.
eg file1:
1
2
3
output:
1|2|3
I try this bat:
echo off
setlocal enabledelayedexpansion
for /f "delims=" %%a in (123.txt) do (
set a=%%a
set a=!a:"\r\n"=^|!
for %%b in ("!a!") do (
echo.%%~b>>1245.txt
))
pause
But, new line char is not "\r\n". How can I get the new line char expression ?
#echo off
setlocal EnableDelayedExpansion
rem Initialize the output line
set "line="
rem Catenate all file lines in the same variable separated by "|"
for /F "delims=" %%a in (123.txt) do set "line=!line!|%%a"
rem Show final line, removing the leading "|"
echo !line:~1!>>1245.txt
You can play with this a little bit:
FOR /F "Usebackq Tokens=*" %%# IN ("File.txt") DO (
<NUL Set /P "=%%#"
)
or this, the /p removes the newLines:
C:\> echo Hello World
Hello World
C:\> echo|set /p=Hello World
Hello World
with a some edits of Krekkon's script:
#echo off
FOR /F "Usebackq Tokens=* delims=" %%# IN ("123.txt") DO (
echo|set /p=%%#^^^|
)>>temp.file
move /y "temp.file" "123.txt"
I have a text file with text on each line. I would like to be able to put each line in one long line along with a space. So, if the text file has:
Bob
Jack
Sam
I want the result to be
Bob Jack Sam
Below are two methods that I am working on but I am stuck. Anything in brackets [] means that I know the syntax is completely wrong; I only put it there to show my thought process. The commented sections are just me experimenting and I have left them in case anyone wants to comment on what they would do / why they do what they do.
Method 1:
#echo off
SETLOCAL EnableDelayedExpansion
SET count=1
FOR /F "tokens=* delims= usebackq" %%x IN ("afile.txt") DO (
SET var!count!=%%x
for %%a in (!count!) do (
!var%%a! = !var%%a! & " "
echo !var%%a!
)
SET /a count=!count!+1
echo !count!
)
::echo !var1! !var2! !var3!
start "" firefox.exe !var%%a!-1
ENDLOCAL
::echo "endlocal" %var1% %var2% %var3%
Method 2:
#echo off
SETLOCAL EnableDelayedExpansion
SET count=1
FOR /F "tokens=* delims= usebackq" %%x IN ("afile.txt") DO (
SET var!count!=%%x
call echo %%var!count!%%
SET /a count=!count!+1
echo !count!
)
::echo !var1! !var2! !var3!
start "" firefox.exe ^
[i = 1]
[for i to !count! do (]
call echo %%var!count!%% & " " & " "^
ENDLOCAL
::echo "endlocal" %var1% %var2% %var3%
If you only have three values, you can directly retrieve them from the file
< input.txt (
set /p "line1="
set /p "line2="
set /p "line3="
)
set "var=%line1% %line2% %line3%"
echo("%var%"
If you don't know the number of values, use a for /f command to read the lines and concatenate the contents into a variable.
#echo off
setlocal enableextensions enabledelayedexpansion
set "var="
for /f "usebackq delims=" %%a in ("input.txt") do set "var=!var!%%a "
echo("%var%"
But if the data can contain exclamation signs, the delayed expansion state will remove them (and the text surounded by them). To avoid it, this can be used
#echo off
setlocal enableextensions disabledelayedexpansion
set "var="
for /f "usebackq delims=" %%a in ("input.txt") do (
setlocal enabledelayedexpansion
for /f "tokens=* delims=¬" %%b in ("¬!var!") do endlocal & set "var=%%b%%a "
)
echo("%var%"
where the setlocal/endlocal and the inner for are used to avoid problems with ! character
Or you can try something like this
#echo off
setlocal enableextensions disabledelayedexpansion
for /f "delims=" %%a in ('
"<nul cmd /q /c "for /f "usebackq delims=" %%z in ("input.txt"^) do (set /p ".=%%z "^)""
') do set "var=%%a"
echo("%var%"
It runs a cmd instance to output the input lines as only one output line (<nul set /p is used to ouput the data without line feeds, so all data is in the same line). This is wrapped in a for to retrieve the output inside a variable
Would this work for you?
#echo off
SET var=
SETLOCAL EnableDelayedExpansion
FOR /f %%i in (afile.txt) DO (
SET var=!var!%%i
)
echo !var!
ENDLOCAL
Notice there is a space after the SET var=!var!%%i[Space Here] to separate each word in each row
EDIT:
If you want to display the current value in the loop just print %%i with NO concatenation. After the FOR loop finishes it will print the last value assigned to the variable
#echo off
SET var=
SETLOCAL EnableDelayedExpansion
for /f %%i in (input.txt) do (
SET var=%%i
echo !var!
)
echo %var%
Will print:
Bob
Jack
Sam
Sam
Try this: Post Edited.
#Echo off
Set "File=Recent_Log.txt"
Set "Output=My_Log.txt"
(
For /F %%F in (%File%) Do (
Set /p ".=%%~F "
)
) > "%Output%" < Nul
Goto :Eof
Because this is the first link after a Google search, I post my solution(Windows 7/10 .bat):
for %%f in (h:\blaba\*.*) do (type "%%f" & echo.) >> h:\sfsdf\dd.txt
NOTE: When your directory/filename contains spaces use double quotes.