[RESOLVED] Import a file with code-VBForums
Results 1 to 22 of 22

Thread: [RESOLVED] Import a file with code

  1. #1

    Thread Starter
    Member
    Join Date
    Feb 2012
    Posts
    41

    Resolved [RESOLVED] Import a file with code

    Hi
    I was wondering if someone could help me

    I have a vb system and I wish to execute some code depending on the result of a text box. The text box could contain two strings, depending on the result I want to execute a set of rules to search for specific files e.g. if one option the code will search for certain files defined for example request.doc and if other option then it will search for other files, which are my rules for the searches. I want these rules to be external to the system and for them to be loaded in so that its easier for the user of the system to change these rules if needed so they dont have to go into the programming of the code. Is there a specific file I can use such as .xml or a just a basic .txt file and what would the syntax be for these?

    Any help would be welcome!

  2. #2
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    5,006

    Re: Import a file with code

    Object serialization would be my first instinct. XML my second.

  3. #3

    Thread Starter
    Member
    Join Date
    Feb 2012
    Posts
    41

    Re: Import a file with code

    Thanks, what would be the syntax for XML? Also can I put code into a xml file?

  4. #4
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    5,006

    Re: Import a file with code

    You can put anything you want into an XML file. You dont know the structure of an XML file ? What do you mean its syntax ?

    Could you provide in explicit details what exactly it is you're looking for. I dont fully understand what it is you want. Take your time and write out a proper, clear explanation so that we can help you.

  5. #5
    Loquacious User Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    24,755

    Re: Import a file with code

    It sounds like the biggest challenge is going to be creating a syntax. If you want non-programmers to be able to edit the file and change the search criteria, then you will need a text-based file such as XML, or even just .TXT, but when people can change a text file, they have every opportunity to turn it into garbage. This is probably worse with XML than with .TXT, because a clueless editor could corrupt the XML itself, making it unreadable by a normal XML parser. It gets even worse if those files hold code. A non-coder can't really be expected to edit code correctly. Heck, even coders tend to create plenty of bugs without testing.

    I'd like to see a more complete description of the problem, as well, but I'd also lean towards an app that manages the files rather than just letting anybody go in and tinker at will.
    My usual boring signature: Nothing

  6. #6

    Thread Starter
    Member
    Join Date
    Feb 2012
    Posts
    41

    Re: Import a file with code

    I meant how would i enter the search criteria into the xml file like:
    <Rules>
    <rule1>
    <description>The code for checking if a file exists</description>
    </rule1>
    </Rules>

    At the moment the full usability wont be too much of a problem as I would be the only one using it so I would be changing the files.

    I need to have a system that can check a record and its 'proof', but depending what the record is classified as there will be different searching criteria. The first classification would need to check whether a request document is present in a folder and a closing confirmation document in the folder. The other classification will need these checks but also other checks such as a file which includes the time and date information. The system is to basically automate this process which was once done manually (ie searching for files). The searching criteria for better usage with a user should be external for them to change the searches it if need be if they are changed/updated. The main issue I have at the moment is coding the xml file itself and how to import it along with the other code for it to work

    Any help would be brill

  7. #7
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    5,006

    Re: Import a file with code

    Would every single record in the database have a corresponding entry in the Xml/.TXT file ? Would you like the entries to refer to a specific record by its primary key ? Are they bound by the classification you spoke of ? How is this related to the textbox with the strings you mentioned earlier ?

    You are giving us bits and pieces of information and im having a difficult time pinning down what it is you really need. I think you should just tell take a step back and describe the overall system that you're dealing with, like what type of software is it and what does your problem have to do with it.

  8. #8

    Thread Starter
    Member
    Join Date
    Feb 2012
    Posts
    41

    Re: Import a file with code

    Sorry its just im not sure how to explain it fully. To take a step back what I would want it to do basically is if a file is present a check box will be checked, so if a request document is present the box will tick. (also is there a way for the user to specify a folder to search for the file?.....getting off topic :S) However I think that I will need various if statements to say if textbox1.text = one then check for these files and if textbox1.text=two it will search for different files. I wanted the checks to be in a seperate file but am happy to have them in the system at this point as im on meltdown with this system so I appriciate you trying to help me . The system is in vb2010 and i think its mainly getting the logic right

  9. #9
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    5,006

    Re: Import a file with code

    Ok now we're getting somewhere. Are the checkboxes fixed in the software or do you want them to be generated dynamically based on settings in a file ?

  10. #10

    Thread Starter
    Member
    Join Date
    Feb 2012
    Posts
    41

    Re: Import a file with code

    The checkboxes change depending if the file is there or not, so will turn green and tick if the file is present and red if it isnt there

  11. #11
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    5,006

    Re: Import a file with code

    So what you really want is a way to check to see if a file exists ?
    You can check for a file's existence like:-
    vbnet Code:
    1. Dim b As Boolean = My.Computer.FileSystem.FileExists("C:\MyFile.Txt")

  12. #12

    Thread Starter
    Member
    Join Date
    Feb 2012
    Posts
    41

    Re: Import a file with code

    Thank you If I have a FolderBrowserDialog Class on the form for the user is there any way this can determine what folder the search is looking in?

    Thanks for your help, so if I wanted to do a file existence on a If statement so
    If textbox1.text = 'New' then Dim b As Boolean = My.Computer.FileSystem.FileExists("C:\MyFile.Txt")
    End If

  13. #13
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    5,006

    Re: Import a file with code

    The FolderBrowserDialog class has a SelectedPath property you can use to determine the path selected.

    As for that If statement you got there....it isnt quite obvious what that is meant to do there.

  14. #14

    Thread Starter
    Member
    Join Date
    Feb 2012
    Posts
    41

    Re: Import a file with code

    So if im searching for a specific file coulld I get the user to chose the folder path (as there is more than once evidence with the same file name but a different document) ie requestcheck.doc can i put it instead of the c: in Dim b As Boolean = My.Computer.FileSystem.FileExists("C:\MyFile.Txt") as the "c:\myfile.text is fixed I just want the c: to be not fixed within the search......just thought about this maybe error Thanks for still helping me

  15. #15
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    5,006

    Re: Import a file with code

    vbnet Code:
    1. Dim path As String = "c:\"
    2.         Dim f As ReadOnlyCollection(Of String) = My.Computer.FileSystem.GetFiles(path)
    The above code returns all the files present at the root folder of the C drive. You can use the path returned by your FolderBrowserDialog instead and then enumerate the collection returned by GetFiles to find the file or files that you want.

  16. #16

    Thread Starter
    Member
    Join Date
    Feb 2012
    Posts
    41

    Re: Import a file with code

    Thanks so i have
    Dim path As String = "folderchoicetxt"
    Dim f As ReadOnlyCollection(Of String) = My.Computer.FileSystem.GetFiles(path)

    But it says 'Read Only Collection' is not defined. Sorry if im being a pain and i appriciate the help

  17. #17
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    5,006

    Re: Import a file with code

    vbnet Code:
    1. Imports System.Collections.ObjectModel

  18. #18

    Thread Starter
    Member
    Join Date
    Feb 2012
    Posts
    41

    Re: Import a file with code

    Thanks

    At the moment my code is
    If checktextbox.text = "Correct" Then
    If My.Computer.FileSystem.FileExists("C:\Users\Documents\evidence.txt") Then
    evidenceCheckBox.Checked() = True
    evidenceCheckBox.ForeColor = Color.Green
    Else
    evidenceCheckBox.Checked = False
    evidenceCheckBox.ForeColor = Color.Red
    End If

    Is this the same if I want to include the other code to make the file path more dynamic rather than just the ("C:\Users\Documents\evidence.txt")
    Dim path As String = "name of the dialog box text box"
    Dim f As ReadOnlyCollection(Of String) = My.Computer.FileSystem.GetFiles(evidence.txt)

    Thanks for the help

  19. #19
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    8,594

    Re: Import a file with code

    You need to pass a path to the GetFiles() Method
    You also need quotes around any literal you try to put in there.

  20. #20

    Thread Starter
    Member
    Join Date
    Feb 2012
    Posts
    41

    Re: Import a file with code

    i am getting this error:
    Could not find a part of the path 'C:\Users\Admin\Documents\VBProject\System\bin\Debug\file.xlsx\'.

    Even when the dialogue box is changed to go to just my documents
    Last edited by Emma11263; Feb 25th, 2012 at 11:32 AM.

  21. #21
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    8,594

    Re: Import a file with code

    Did you notice the \ after your filename? That should not be there.

  22. #22

    Thread Starter
    Member
    Join Date
    Feb 2012
    Posts
    41

    Re: Import a file with code

    I finally got it working.......thank you so much!!

    However I used the combine path instead of the GetFile function


Posting Permissions

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



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.