string replace...

Technical support and scripting issues

Moderators: JRL, Dorian (MJT support)

Post Reply
BlackWisdom
Pro Scripter
Posts: 58
Joined: Thu Oct 16, 2003 12:53 am

string replace...

Post by BlackWisdom » Tue Dec 20, 2005 3:09 am

I have made a text file of the structure of a CD - but I need to clean the file by removing the leading three characters containing the drive name - "D:\" how do I remove the firs three characters of each line regardless of what those characters are...any links...???
Last edited by BlackWisdom on Tue Dec 20, 2005 3:28 am, edited 1 time in total.

Me_again
Automation Wizard
Posts: 1101
Joined: Fri Jan 07, 2005 5:55 pm
Location: Somewhere else on the planet

Post by Me_again » Tue Dec 20, 2005 3:27 am

Are you sure the .scp didn't get saved in a different path? Worth a search...

BlackWisdom
Pro Scripter
Posts: 58
Joined: Thu Oct 16, 2003 12:53 am

sorry..

Post by BlackWisdom » Tue Dec 20, 2005 3:59 am

hi me_again - Im sorry - I resovled the previosu post and re-edited the topic but I guess you must have read it before I changed it ...

Me_again
Automation Wizard
Posts: 1101
Joined: Fri Jan 07, 2005 5:55 pm
Location: Somewhere else on the planet

Post by Me_again » Tue Dec 20, 2005 4:49 am

In the hope that the question doesn't get changed again :lol:

Read each line into a variable using ReadLn

Get the length of the variable using Length

Calculate length - 3

Extract the data you want with MidStr using a start position of 4 and a length of length - 3

Write the line to a new file

BlackWisdom
Pro Scripter
Posts: 58
Joined: Thu Oct 16, 2003 12:53 am

thanks Me_Again

Post by BlackWisdom » Tue Dec 20, 2005 5:06 am

and no the question wont get changed again :oops:

- but you were right there was a debug version in the MJT folder:

Iwill give you suggestioin a try and get back with you

BlackWisdom
Pro Scripter
Posts: 58
Joined: Thu Oct 16, 2003 12:53 am

input past end of file...

Post by BlackWisdom » Tue Dec 20, 2005 2:23 pm

hey guys I wrote this VBScript :


VBSTART

Set objFSO = CreateObject("Scripting.FileSystemObject")

arrFiles = Array("BackTemp\D_results.txt")

For Each strFile In arrFiles
Set objFile = objFSO.OpenTextFile(strFile, ForReading)
strText = objFile.ReadAll
objFile.Close
strNewText = Replace(strText, "D:\", "")
Set objFile = objFSO.OpenTextFile(strFile, ForWriting)
objFile.WriteLine strNewText
objFile.Close
Next

VBEND

I attempts to prcess the file - but then I get this error:

---------------------------
Macro Scheduler
---------------------------
Microsoft VBScript runtime error :62

Input past end of file

Line 74, Column 2
---------------------------
OK
---------------------------

I cant use MSChed EOF code because VBScript sees it as a syntax error - any ideas...???

User avatar
Bob Hansen
Automation Wizard
Posts: 2475
Joined: Tue Sep 24, 2002 3:47 am
Location: Salem, New Hampshire, US
Contact:

Post by Bob Hansen » Wed Dec 21, 2005 1:03 am

How about a simple VB Replace function in the middle of Macro Scheduler sript?

Code: Select all

//Next two lines only needed once at top of script
VBSTART
VBEND

//You could put the next VBEVAL line in a loop and parse every %string%.
VBEval>Replace("%string%","D:\",""),newstringname

//==========================================
Actually the Replace line does not require the result to have a new name.
VBEval>Replace("%string%","D:\",""),string is also acceptable.
Hope this was helpful..................good luck,
Bob
A humble man and PROUD of it!

Me_again
Automation Wizard
Posts: 1101
Joined: Fri Jan 07, 2005 5:55 pm
Location: Somewhere else on the planet

Post by Me_again » Wed Dec 21, 2005 1:41 am

Search and replace isn't going to do it because as I read the spec
how do I remove the firs three characters of each line regardless of what those characters are
it may be some other drive letter instead of D. So, as I suggested above:

Let>CF_OVERWRITE=1
DeleteFile>c:\temp\test2.txt
Let>k=1
Label>start
ReadLn>c:\temp\test.txt,k,line
If>line=##EOF##,finish
Length>line,len
Sub>len,3
MidStr>line,4,%len%,nuline
WriteLn>c:\temp\test2.txt,result,nuline
Let>k=k+1
Goto>start
Label>finish
CopyFile>c:\temp\test2.txt,c:\temp\test.txt

User avatar
Captive
Macro Veteran
Posts: 213
Joined: Sun Oct 20, 2002 8:37 pm
Location: Colorado, USA

Post by Captive » Mon Jan 02, 2006 9:10 pm

Effectively the same thing, but cut down to 1 line of VB...

VBEval>Right("%MyString%",Len("%MyString%") - 3),MyResult

(You could use MyString instead of MyResult too.)

Because we're using VBScript, don't forget to put
VBSTART
VBEND
at the top of your script if it's not already there.

User avatar
JRL
Automation Wizard
Posts: 3532
Joined: Mon Jan 10, 2005 6:22 pm
Location: Iowa

Post by JRL » Mon Jan 02, 2006 11:18 pm

If you're looking for a specific string to replace, rather than resorting to VB script, why not use the Macro Scheduler command "StringReplace".

ReadFile>%TEMP_DIR%\D_results.txt,file
StringReplace>%file%,D:\,,newfile
WriteLn>%TEMP_DIR%No_D_results.txt,wresult,%newfile%


If looking for any drive letter as Me_again has pointed out, one "midstr" could be added. Assuming the first line is a valid path beginning with a drive letter (as I think all of these examples would require), use midstr to identify the first three characters of the first line then replace all occurences of those three characters with "nul".

ReadFile>%TEMP_DIR%\D_results.txt,file
Midstr>%file%,1,3,drive
StringReplace>%file%,%drive%,,newfile
WriteLn>%TEMP_DIR%No_D_results.txt,wresult,%newfile%


Later,
Dick

User avatar
Marcus Tettmar
Site Admin
Posts: 7395
Joined: Thu Sep 19, 2002 3:00 pm
Location: Dorset, UK
Contact:

Post by Marcus Tettmar » Mon Jan 02, 2006 11:43 pm

Captive wrote:Effectively the same thing, but cut down to 1 line of VB...

VBEval>Right("%MyString%",Len("%MyString%") - 3),MyResult

(You could use MyString instead of MyResult too.)

Because we're using VBScript, don't forget to put
VBSTART
VBEND
at the top of your script if it's not already there.
Even shorter:

Let>MyString={copy(%MyString%,4,length(%MyString%))}
Marcus Tettmar
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar

Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?

Post Reply
Sign up to our newsletter for free automation tips, tricks & discounts