Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

find specific character then insert specific character

160 views
Skip to first unread message

meve

unread,
Dec 29, 2011, 5:42:30 PM12/29/11
to
Help !

I am attempting to create a batch file that will insert the character
2 into position 118 of a text file.
the 1st line is what is getting generated. I need to have the batch
file run to insert the character 2 into position 118
the 2nd line is what i'm looking for the end result.

the spaces between 0 (space #70) & 2 (space #118)


888888 **
-0001.30
888888 **
-0001.30 2

any help would be very much appreciated.

thanks
meve

foxidrive

unread,
Dec 29, 2011, 6:13:40 PM12/29/11
to
You can manipulate strings using this where %%a contains your line.

set "var=%%a"

set "string=%var:~0,117%2%var:~117,%"

echo "string"


This depends on the string always being more than 117 characters long, so that the first term will gather the right number of characters.
You can however extend the string to solve this limitation by adding 120 odd spaces at the end first, and then processing the above code.

Assume blank has more than 117 spaces

set "blank= "

set "var=%%a%blank%"

set "string=%var:~0,117%2%var:~117,%"

echo "string"



This is untested so if you can't make it work then reply and I'll see where I made a boo boo.





--
Regards,
Mic

foxidrive

unread,
Dec 29, 2011, 6:18:17 PM12/29/11
to
I should have added that the second term can include a number to determine the length of the line. Using %var:~117,3% for example will truncate every line at 120 characters, if you needed to pad the lines with spaces.

Todd Vargo

unread,
Dec 30, 2011, 12:32:30 AM12/30/11
to
On 12/29/2011 5:42 PM, meve wrote:
> Help !
>
> I am attempting to create a batch file that will insert the character
> 2 into position 118 of a text file.
> the 1st line is what is getting generated. I need to have the batch
> file run to insert the character 2 into position 118
> the 2nd line is what i'm looking for the end result.
>
> the spaces between 0 (space #70)& 2 (space #118)
>
>
> 888888 **
> -0001.30
> 888888 **
> -0001.30 2
>
> any help would be very much appreciated.

Does this batch do what you want?

@echo off&setlocal enabledelayedexpansion
set "line= "=20 spaces
for /f "delims=" %%a in (input.txt) do (
set "line=%%a!line!!line!!line!!line!!line!!line!"
echo !line:~0,117!2
)>output.txt

--
Todd Vargo
(Post questions to group only. Remove "z" to email personal messages)

meve

unread,
Dec 30, 2011, 2:30:28 PM12/30/11
to
That worked Todd ! Thank you very much.. now can I apply that to all
of the lines in the text file. This file will only contain the lines
that need to have the 2 in position 118.

Hope i'm describing what I need now, after you were able to get the
initial issue figured out for me.

thanks again

Todd Vargo

unread,
Dec 30, 2011, 7:40:59 PM12/30/11
to
On 12/30/2011 2:30 PM, meve wrote:
> On Dec 30, 12:32 am, Todd Vargo<tlva...@sbcglobal.netz> wrote:
>> On 12/29/2011 5:42 PM, meve wrote:
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>> Help !
>>
>>> I am attempting to create a batch file that will insert the character
>>> 2 into position 118 of a text file.
>>> the 1st line is what is getting generated. I need to have the batch
>>> file run to insert the character 2 into position 118
>>> the 2nd line is what i'm looking for the end result.
>>
>>> the spaces between 0 (space #70)& 2 (space #118)
>>
>>> 888888 **
>>> -0001.30
>>> 888888 **
>>> -0001.30 2
>>
>>> any help would be very much appreciated.
>>
>> Does this batch do what you want?
>>
>> @echo off&setlocal enabledelayedexpansion
>> set "line= "=20 spaces
>> for /f "delims=" %%a in (input.txt) do (
>> set "line=%%a!line!!line!!line!!line!!line!!line!"
>> echo !line:~0,117!2
>> )>output.txt
>
> That worked Todd ! Thank you very much.. now can I apply that to all
> of the lines in the text file. This file will only contain the lines
> that need to have the 2 in position 118.
>
> Hope i'm describing what I need now, after you were able to get the
> initial issue figured out for me.

It needs this modification to do that.

@echo off&setlocal enabledelayedexpansion
set "pad= "=20 spaces
for /f "delims=" %%a in (input.txt) do (
set "line=%%a!pad!!pad!!pad!!pad!!pad!!pad!"

meve

unread,
Dec 30, 2011, 7:55:11 PM12/30/11
to
When I updated the code, now it's only doing the first line. What am i
doing wrong, if you want, I can email you the file ?

I really appreciate all the help

Thanks
meve

Todd Vargo

unread,
Dec 31, 2011, 2:51:45 PM12/31/11
to meve
> When I updated the code, now it's only doing the first line. What am i
> doing wrong, if you want, I can email you the file ?
>
> I really appreciate all the help

Oops, I forgot modify for >output.txt to >>output.txt

@echo off&setlocal enabledelayedexpansion
type nul>output.txt

Todd Vargo

unread,
Dec 31, 2011, 2:52:29 PM12/31/11
to meve
> When I updated the code, now it's only doing the first line. What am i
> doing wrong, if you want, I can email you the file ?
>
> I really appreciate all the help

Oops, I forgot modify for >output.txt to >>output.txt

@echo off&setlocal enabledelayedexpansion
type nul>output.txt

Todd Vargo

unread,
Dec 31, 2011, 2:54:30 PM12/31/11
to
On 12/30/2011 7:55 PM, meve wrote:

> When I updated the code, now it's only doing the first line. What am i
> doing wrong, if you want, I can email you the file ?

Oops, I forgot modify >output.txt to >>output.txt

@echo off&setlocal enabledelayedexpansion
type nul>output.txt

meve

unread,
Dec 31, 2011, 5:33:01 PM12/31/11
to
I'm still having the same issue, it's inserting the 2 in position 118
but the script is only doing the first line. Help ! If you want I can
send you the file as well ?

thanks
meve

foxidrive

unread,
Dec 31, 2011, 5:38:01 PM12/31/11
to
Change the last two lines to this:


>>output.txt echo/!line:~0,117!2
)



--
Regards,
Mic

foxidrive

unread,
Dec 31, 2011, 5:46:58 PM12/31/11
to
On 1/01/2012 06:54, Todd Vargo wrote:
> On 12/30/2011 7:55 PM, meve wrote:
>
>> When I updated the code, now it's only doing the first line. What am i
>> doing wrong, if you want, I can email you the file ?
>
> @echo off&setlocal enabledelayedexpansion
> type nul>output.txt
> set "pad= "=20 spaces
> for /f "delims=" %%a in (input.txt) do (
> set "line=%%a!pad!!pad!!pad!!pad!!pad!!pad!"
> echo !line:~0,117!2
> )>>output.txt
>

Todd's code does process all the lines, assuming they don't contain any ! characters.

input.txt
"a"
"b"
"c"

output.txt
"a" 2
"b" 2
"c" 2


If you'd like, email your file to me: foxidrive lavabit . com
and correct the @ and spaces in the email address.





--
Regards,
Mic

foxidrive

unread,
Dec 31, 2011, 6:14:58 PM12/31/11
to
Rename your file to input.txt and it works fine.





--
Regards,
Mic

Todd Vargo

unread,
Jan 1, 2012, 1:39:16 AM1/1/12
to
My code above works (tested with multiple lines similar to the sample
text provided). The modification you suggested gives me the following
output for each line because of the / after echo.

line:~0,1172

Replace the / with a space and it will work as expected (assuming no !
characters in the input file as you mentioned in another post).
0 new messages