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

Add string to start of line

179 views
Skip to first unread message

andrewn...@gmail.com

unread,
Mar 12, 2012, 9:09:18 PM3/12/12
to
Hi,
I have created the below with a batch file and would like to know how i would add the words "Open Table " to the start of the file path below with a batch file

!Workspace
!Version 800
!Charset WindowsLatin1

D:\DCDB_Process\bpoly\woorabinda_i_bpoly.TAB
D:\DCDB_Process\bpoly\wujal_wujal_i_bpoly.TAB
D:\DCDB_Process\bpoly\yarrabah_i_bpoly.T

The above would become

!Workspace
!Version 800
!Charset WindowsLatin1

Open Table D:\DCDB_Process\bpoly\woorabinda_i_bpoly.TAB
Open Table D:\DCDB_Process\bpoly\wujal_wujal_i_bpoly.TAB
Open Table D:\DCDB_Process\bpoly\yarrabah_i_bpoly.T

Todd Vargo

unread,
Mar 13, 2012, 12:38:51 AM3/13/12
to
Where is your batch file that created the text? What OS is this for?
(Yes it makes a difference.)

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

foxidrive

unread,
Mar 13, 2012, 12:48:25 AM3/13/12
to
I suspect, Todd, that some utility spat out the info in that format.




To the OP: here is a method using a search and replace batch file below, and calling it like this:


:: ren-with-sar.bat
@echo off
call sar "file.txt" "file2.txt" ".:\\" "Open Table $&"


What it does is replace every drive:\ with "Open Table drive:\" and saves the info into file2.txt - so care needs to be taken to ensure that there are no other drive:\ instances in the file if you don't want them changed. It doesn't care if the drive is c:\ f:\ z:\ or whatever in this case.



:: sar.bat
@echo off
if "%~3"=="" (
echo.Search and replace
echo Syntax:
echo %0 "filein.txt" "fileout.ext" "regex" "replace_text" [first]
echo.
echo. if [first] is present only the first occurrence is changed
goto :EOF
)
if "%~5"=="" (set global=true) else (set global=false)
set s=regex.replace(wscript.stdin.readall,"%~4")
>_.vbs echo set regex=new regexp
>>_.vbs echo regex.global=%global%
>>_.vbs echo regEx.IgnoreCase=True
>>_.vbs echo regex.pattern="%~3"
>>_.vbs echo wscript.stdOut.write %s%
cscript /nologo _.vbs <"%~1" >"%~2"
del _.vbs





--
Mic

billious

unread,
Mar 13, 2012, 4:09:38 AM3/13/12
to

<andrewn...@gmail.com> wrote in message
news:32397669.213.1331600958939.JavaMail.geo-discussion-forums@pbnt10...
Well, it would probably be better to produce the prefix within your
unpublished batch.

Assuming that you are using W2000 or later, and the output file that you
have produced is say, babinda.txt then

@echo off
del babinda.cnv 2>nul
(set before=Open Table )
for /f "delims=" %%i in (babinda.txt) do (
call :decide %%i
if defined prefix (>>babinda.cnv echo %before% %%i
) else (
>>babinda.cnv echo %%i)
)
goto :eof

:decide
set prefix=%1
set prefix=%prefix:~1,1%
if not "%prefix%"==":" (set prefix=)
goto :eof


would process babinda.txt to babinda.cnv BUT would eliminate all of the
empty lines

and

@echo off
del wungu.cnv 2>nul
(set before=Open Table )
for /f "tokens=1,2delims=]" %%h in ( ' find /n /v "" ^<babinda.txt ' ) do (
call :decide %%i
if defined prefix (>>wungu.cnv echo %before% %%i
) else (
(set prefix=%%i)
if defined prefix (>>wungu.cnv echo %%i) else (
>>wungu.cnv echo.
)
)
)
goto :eof

:decide
set prefix=%1
set prefix=%prefix:~1,1%
if not "%prefix%"==":" (set prefix=)
goto :eof

would process babinda.txt to wungu.cnv and would preserve the empty lines.

Note that these techniques may be a little sensitive to some symbols like
"%" in particular. More information or diligent testing required...

Notes :

the 'del filename 2>nul' construct deletes the file if possible and
suppresses error messages
the (set ...) construct allows spaces to be safely added as terminal
characters without fear of having the editor being too helpful and deleting
them.
Note that the metavariable (loop-control variable) in wungu is h and babinda
is i (and is case-sensitive)
the ^<filename in the FIND substatement is to escape the redirection
character so that the FIND acts as though the file is typed in from the
keyboard
The spaces around the single-quotes enclosing the FIND substatement are for
emphasis. The single-quotes are required, but the spaces are optional.
The sequence ") else (" where used must have the close-parenthesis before
and the open-parenthesis after the else ON THE SAME PHYSICAL LINE.
the :eof label is DEFINED by the command-processor as PHYSICAL END-OF-FILE
and should not be defined by the programmer.
The :decide procedure
1) sets 'prefix' to the value of the first parameter supplied to the
procedure (not the first supplied to the batch itself) Then isolates the
SECOND character (at position 1 in the string (starting at character "0")
get 1 character) of the string; then sets PREFIX to nothing if that
character is not a colon.

==> consequently, PREFIX will be either a colon or not-defined on return.

All of these techniques are documented (if poorly) - use

for /?

from the prompt for help.

OR you could try the etensive discussion over the past few years in
alt.msdos.batch.nt where there is extensive information about "poison
characters" (the set of characters that batch may have problems processing)
and ways to get around them and other traps discovered over the years...




Tom Lavedas

unread,
Mar 13, 2012, 1:57:27 PM3/13/12
to
Yet another approach ...

:: Prefix.cmd
@echo off
for /f "tokens=1-2* delims=]:" %%a in ('find /v "" /n') do (
if [%%c]==[] (echo.%%b) else (echo Open Table %%b:%%c)
)

Use it thus ...

C:\>prefix < infile.txt > outfile.txt

It works with the file sample provided.
______________________________
Tom Lavedas

Todd Vargo

unread,
Mar 13, 2012, 5:32:31 PM3/13/12
to
On 3/12/2012 11:48 PM, foxidrive wrote:
> On 13/03/2012 15:38, Todd Vargo wrote:
>> On 3/12/2012 8:09 PM, andrewn...@gmail.com wrote:
>>> Hi,
>>> I have created the below with a batch file
<Snip>

>> Where is your batch file that created the text? What OS is this for?
>> (Yes it makes a difference.)
>
> I suspect, Todd, that some utility spat out the info in that format.

It would be appreciated if others would just let the OP respond in their
own words. I have asked for cooperation in this regard before. :(

andrewn...@gmail.com

unread,
Mar 13, 2012, 6:39:52 PM3/13/12
to
Thanks for all the replies, Sorry i should have explained myself better. This is the batch file:

Echo !Workspace >> "D:\DCDB_Process\Tab_List\bpoly_list.Wor"
Echo !Version 800 >> "D:\DCDB_Process\Tab_List\bpoly_list.Wor"
Echo !Charset WindowsLatin1 >> "D:\DCDB_Process\Tab_List\bpoly_list.Wor"
Echo. >> "D:\DCDB_Process\Tab_List\bpoly_list.Wor"
dir/b/s D:\DCDB_Process\bpoly\*.tab >> D:\DCDB_Process\Tab_List\bpoly_list.Wor

That creates this:

!Workspace
!Version 800
!Charset WindowsLatin1

D:\DCDB_Process\bpoly\Temp.TAB
D:\DCDB_Process\bpoly\woorabinda_i_bpoly.TAB
D:\DCDB_Process\bpoly\wujal_wujal_i_bpoly.TAB
D:\DCDB_Process\bpoly\yarrabah_i_bpoly.TAB

bpoly_list.Wor is a file that is opened by the GIS software MapInfo Pro what i was trying to do was create my own workspace file from the files in a directory for MapInfo to open the bpoly_list.Wor it would need the words "open table" to be added to the front of the file path line with quotes around the file path as well like this:
Open Table "D:\DCDB_Process\bpoly\Temp.TAB"
OS is XP Pro, i was hoping to achieve this in pure dos if possible without the use of a 3rd party addin

Regards

Andrew

billious

unread,
Mar 13, 2012, 7:32:11 PM3/13/12
to
Easy then.

Replace

dir/b/s D:\DCDB_Process\bpoly\*.tab >>
D:\DCDB_Process\Tab_List\bpoly_list.Wor

with

for /f "delims=" %%i in ( ' dir/b/s D:\DCDB_Process\bpoly\*.tab ' ) do
>>D:\DCDB_Process\Tab_List\bpoly_list.Wor echo Open Table %%i


(all as a single line)


andrewn...@gmail.com

unread,
Mar 13, 2012, 11:21:40 PM3/13/12
to
Thanks, one more question how would you enclose the file paths with quotes?

Regards

Andrew

billious

unread,
Mar 14, 2012, 1:08:03 AM3/14/12
to
Aargh!

"The" file paths where?


for /f "delims=" %%i in ( ' dir/b/s "D:\DCDB_Process\bpoly\*.tab" ' ) do
>>"D:\DCDB_Process\Tab_List\bpoly_list.Wor" echo Open Table "%%i"

Note three separate filepaths - in the DIR subcommand, the destination
filespec and on the data line generated...

Or elsewhere in the script?

Sometimes, dealing with quotes within FOR loops can be tricky, often solved
with the USEBACKQ switch. All depends on quite where the quotes are desired
to be.

Sorry to be picky - but that's the command-processor for you :)





andrewn...@gmail.com

unread,
Mar 14, 2012, 5:08:18 AM3/14/12
to
Excellent, Thanks very much, this is working very well now, i have just opened 50 files!

regards
Andrew

Todd Vargo

unread,
Mar 14, 2012, 7:19:31 PM3/14/12
to
Andrew, thanks for providing this information. It looks like billious
already acted on it (same as I would have) so nothing further for me to
post except, glad you have a working solution now.
0 new messages