Results 1 to 8 of 8

Thread: 2 simple questions, please help!

  1. #1

    Thread Starter
    Member hooya81's Avatar
    Join Date
    May 2003
    Location
    Sunny Scotland!
    Posts
    60

    2 simple questions, please help!

    hi, not been programming for long and could do with a little help.

    EncryptFile(Text1.Text, Text1.Text & ".fxt")

    the first text1.text is the input file and the 2nd is the output file, so C:\example.txt gets encrypted and then saved as C:\example.txt.fxt that part works fine however its when i want to decrypt!

    DecryptFile(text1.text, text1.text)

    again the first text1.text is input the second output, what i want to know is how do i now remove that ".fxt" i added earlier.

    assuming its something like

    DecryptFile (text1.text, text1.text - ".fxt")

    please help!

    The second thing wos i want to detect a file extension.

    an example would be once i open the file path to a text box using commondialog i want to be able to detect the extension of that file so i can display something in a label caption. eg

    if text2.text = "*.fxt" then
    label1.caption = "Encryption method used etc etc"

    i know the above isn't how you do it, but hopefully someone can tell me how!

    sorry i know these are dumb questions, still learning.

    thanx in advance

  2. #2
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106
    If you can always be sure that the file extension is there, then it will always be the right 4 characters: The "." and the three characters of the extension. That would be easy:

    If st1 is the file name, then

    st2 = Right(st1,4)

    would give you the last four characters.

    Alternatively, you can search for the "." character, using InStr(), but this has the potential problem that there can be more than one period in file names these days. Therefore, you would need to look back from the end of the string, though once you either find a period, or have searched back four characters, you would know whether or not the extension exists.

  3. #3

    Thread Starter
    Member hooya81's Avatar
    Join Date
    May 2003
    Location
    Sunny Scotland!
    Posts
    60
    thanx alot 'Shaggy Hiker' tried what you said and it worked perfectly!, You the man.

    if someone else could just help me with the first problem i'll be sorted!

    thanx again really appreciate that, cheers

  4. #4
    Fanatic Member Armbruster's Avatar
    Join Date
    Sep 2002
    Location
    Maryland Heights, MO
    Posts
    857
    VB Code:
    1. 'Assuming there is only one period in the filename
    2.     ' contained in text1.text . . .
    3.    
    4.     Dim strFileName As String
    5.     'create array to hold parsed filename
    6.     Dim strFile() As String
    7.    
    8.     'parse the filename by the period
    9.     strFile = Split(Text1.Text, ".")
    10.    
    11.     'set the filename to the first element of the array
    12.     strFileName = strFile(0)
    13.    
    14.     DecryptFile Text1.Text, strFileName
    "Look! Up in the sky! It's a bird! It's a plane! It's Diaper-Head Boy! (there by my name!) Yes, Diaper-Head Boy, who disguised as my son, Seth, fights a never-ending battle for truth, justice and terrorizing my house!

    Resistance is futile, you will be compiled . . . Please!

  5. #5
    Addicted Member Mars-Martian's Avatar
    Join Date
    May 2002
    Location
    Canada
    Posts
    185
    Hmmm armbuster that's not that good 'cause if the extension if something other then *.txt.fxt then he has to figure that out..
    The best way (In my opinion) is..
    [Highlight=VB]
    St1 = Left(St2, 0, Len(St2) - 4)
    First person to be able to get what song is currently playing in Winamp 3.

  6. #6
    Fanatic Member Armbruster's Avatar
    Join Date
    Sep 2002
    Location
    Maryland Heights, MO
    Posts
    857
    Originally posted by Mars-Martian
    Hmmm armbuster that's not that good 'cause if the extension if something other then *.txt.fxt then he has to figure that out..
    The best way (In my opinion) is..
    VB Code:
    1. St1 = Left(St2, 0, Len(St2) - 4)
    In my example I DID say . . .

    'Assuming there is only one period in the filename
    ' contained in text1.text . . .
    My code code be easily modified to remove the extension, no matter how many periods are in the filename.

    VB Code:
    1. Dim strFileName As String
    2.     Dim intUbound As Integer
    3.     Dim i As Integer
    4.    
    5.     'create array to hold parsed filename
    6.     Dim strFile() As String
    7.    
    8.     'parse the filename by the period
    9.     strFile = Split(Text1.Text, ".")
    10.    
    11.     'find the upper bound of the arrary
    12.     intUbound = UBound(strFile)
    13.    
    14.     'add the array back together but do
    15.     '  not add the file extension '
    16.     '   (last element of the array)
    17.     For i = 0 To intUbound - 2
    18.         If i = 0 Then
    19.             strFileName = strFile(i)
    20.         Else
    21.             'add any period(s) contained in the filename back in
    22.             strFileName = strFileName & "." & strFile(i)
    23.         End If
    24.     Next i
    25.        
    26.     DecryptFile Text1.Text, strFileName

    Technically, your code is also not good.

    1) It assumes a three-digit file extension. What about .html??? That is a four-digit extension, your code would not work.

    2) You used left function instead of left$ function so vb has to convert the variable twice

    3) the left function takes two arguments, not three - your code would never work

    Sorry, I don't mean to come off as an a$$, but it kinda rubbed me wrong when the first thing I read was . . .

    armbuster that's not that good
    Last edited by Armbruster; May 5th, 2003 at 08:15 PM.
    "Look! Up in the sky! It's a bird! It's a plane! It's Diaper-Head Boy! (there by my name!) Yes, Diaper-Head Boy, who disguised as my son, Seth, fights a never-ending battle for truth, justice and terrorizing my house!

    Resistance is futile, you will be compiled . . . Please!

  7. #7

    Thread Starter
    Member hooya81's Avatar
    Join Date
    May 2003
    Location
    Sunny Scotland!
    Posts
    60
    Thanx alot 'Armbruster' and 'Mars Martian'

    Got it working great thanx to you 2!

    Can't thank you's enough.

    Cheers!

  8. #8
    Junior Member
    Join Date
    May 2003
    Posts
    17
    Hi,

    I'm glad your thread is solved, but could you PLEASE add something like 'solved' in the title of this thread?


    Thanks,

    Teddo and a lot of other thankful users
    [vbcode]
    Private Sub AskAQuestion(Me As Question, Others As Answer)
    MsgBox "Answer: " & Others
    End Sub

    Private Sub HelpOthers()
    If Answer = Known Then
    Give Answer
    Else
    MakeUpAnswerAndLookSmart
    End If
    End Sub
    [/vbcode]
    [vbcode]
    Private Sub Teddo_WakeUp()
    If HelpOther = Possible Then
    HelpOthers
    Else
    AskAQuestion
    End If
    End Sub
    [/vbcode]

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width