Link to home
Start Free TrialLog in
Avatar of matthewsampson
matthewsampson

asked on

take the contents of a textbox and enter a character 4 positions from the end

Hi Guys,

I've got a filepath which is in a textbox - G:\database.mdb
I am doing a 'compact database' function which takes the database path name from the contents of the textbox, compacts the database and then renames it.

I want it to rename it to the same as the original file, but with the addition of 'TC' at the end of the filename.
-----
jro.CompactDatabase("Provider=Microsoft.Jet.OLEDB.4.0;Data Source= """ & textbox1.Text & """ ", _
        "Provider=Microsoft.Jet.OLEDB.4.0;Data Source= """ & textbox1.Text & "TC" & """ ;Jet OLEDB:Engine Type=5")
-----
The code above works fine, except that it adds the 'TC' after the extention.
I.e: textbox1.text = G:\database.mdb   and the new file is called  G:\database.mdbTC
What I want is the file to be called  D:\databaseTC.mdb

I think it needs to count from the end backwards and then insert the 'TC' just before the 'dot'. (.mdb). This is because there will be different length filenames but they will always end in .mdb.

Any help would be great.

Many Thanks

Matt
Avatar of Mike McCracken
Mike McCracken

Try something like

jro.CompactDatabase("Provider=Microsoft.Jet.OLEDB.4.0;Data Source= """ & textbox1.Text & """ ", _
        "Provider=Microsoft.Jet.OLEDB.4.0;Data Source= """ & left(textbox1.Text,length(textbox1.Text)-4) & "TC.mdb" & """ ;Jet OLEDB:Engine Type=5")

mlmcc
ASKER CERTIFIED SOLUTION
Avatar of Joe_Griffith
Joe_Griffith

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hi,

if you want to split the name, use something like this:

strName = "test.mdb"
strNewName = strName.substring(0,strName.LastIndexOf(".") - 1) + "TC" + ".mdb"

Also, you can use the FileInfo object: it has a .name method which gives you the filename without extension. Get this name,
add "TC.mdb" and you're done.
Don't know if the syntax is exact, put a new comment if you can't get it right.

greetings
I'd strongly suggest using Joe_Griffith's suggestion, since it's a lot safer (if for some reason there's a platform change or whatever in the future), and also a lot easier to figure out just what's going on.
Avatar of matthewsampson

ASKER

Hi Joe_Griffith,

I've been following your idea.
When using this bit of the code:
----
        FileName = Path.GetFileNameWithoutExtension(temp)
        FileExt = Path.GetExtension(temp)
----
It says that Path is not defined.
Did I miss something?
Add

Imports System.IO

or qualify Path as System.IO.Path...