Getting A File Name And Adding It To A File
Getting A File Name And Adding It To A File
Getting A File Name And Adding It To A File
Getting A File Name And Adding It To A File
Getting A File Name And Adding It To A File
Getting A File Name And Adding It To A File Getting A File Name And Adding It To A File Getting A File Name And Adding It To A File Getting A File Name And Adding It To A File Getting A File Name And Adding It To A File Getting A File Name And Adding It To A File Getting A File Name And Adding It To A File Getting A File Name And Adding It To A File
Getting A File Name And Adding It To A File Getting A File Name And Adding It To A File
Getting A File Name And Adding It To A File
Go Back  Xtreme Visual Basic Talk > > > Getting A File Name And Adding It To A File


Reply
 
Thread Tools Display Modes
  #1  
Old 08-17-2011, 11:28 PM
Shockey Shockey is offline
Newcomer
 
Join Date: Aug 2011
Posts: 5
Default Getting A File Name And Adding It To A File


Hello guys, this is my first post and my first problem with this program that i have been creating. If you take a look at my code

Code:
   Private Sub CompileToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CompileToolStripMenuItem.Click
        Dim SaveFile As New SaveFileDialog
        SaveFile.FileName = ""
        SaveFile.Filter = "Pawno File (*.pwn)|*.pwn"
        SaveFile.ShowDialog()
        Try
            Dim Write As New System.IO.StreamWriter(SaveFile.FileName)
            Write.Write(TextBox1.Text)
            Write.Close()
            Shell("pawncc.exe", vbNormalFocus)
        Catch ex As Exception

        End Try
        Dim OpenFile As New OpenFileDialog
        SaveFile.FileName = "compiled"
        SaveFile.Filter = "Pawno File (*.bat)|*.bat"
        SaveFile.ShowDialog()
        Try
            Dim Write As New System.IO.StreamWriter(SaveFile.FileName)
            Write.Write("pawncc.exe" & OpenFile.FileName)
            Write.Write(TextBox2.Text)
            Write.Close()
            Shell("pawncc.exe", vbNormalFocus)
        Catch ex As Exception

        End Try
    End Sub
I want the .pwn file name to show in the .bat file.

So the one line will look like

pawncc.exe "[Filename].pwn"

Also, i want on the next line will say pause.

so in the very end.

pawncc.exe "[Filename].pwn"
pause

Thank you!
Reply With Quote
  #2  
Old 08-18-2011, 08:32 AM
NFITC1 NFITC1 is offline
Centurion
 
Join Date: Apr 2006
Posts: 102
Default

There are several issues with your code that you should address. I don't know if you know what they are so I'm just going to show you.

First off, you have two try blocks with empty catch blocks. That's not a good practice. You're probably trying to ignore the error of "what if there is no selected file". You can get around that by adding this instead of a try-catch block:

Code:
        If SaveFile.ShowDialog() Then
            'CommonDialog.ShowDialog returns a boolean that will indicate if it was "Canceled" or not. If it is canceled it returns "False"

            ...

            Dim OpenFile As New OpenFileDialog
            SaveFile.FileName = "compiled"
            SaveFile.Filter = "Pawno File (*.bat)|*.bat"

            If SaveFile.ShowDialog() Then
                'Same thing here.
                ... 
            End If
        End If
    End Sub
That's not what you asked for, so I'll answer that now. You're changing your SaveFile SaveFileDialog Box after you declare the OpenFile OpenFileDialog. So, of course, OpenFile.FileName will be null because it never opened. I don't presume to know how your program should function, but based on what you said you want this is how to do it:

Code:
            Dim OpenFile As New OpenFileDialog
            OpenFile.FileName = "compiled"
            OpenFile.Filter = "Pawno File (*.bat)|*.bat"

            If OpenFile.ShowDialog() Then
                Dim Write As New System.IO.StreamWriter(OpenFile.FileName) 'OpenFile is your .bat file
                Write.Write("pawncc.exe" & SaveFile.FileName) 'SaveFile is your .pwn file

You'll also want some delimiter between the .bat file and the file name if you plan on running it:

Code:
            Write.Write("pawncc.exe " & OpenFile.FileName)
Also, if I'm reading the function correctly, you're running the pawncc.exe twice. Again, I don't presume to know what your program should do, but just from looking at the code not knowing what the pawncc.exe is or does I'd think you'd want to run the batch file instead:

Code:
                Shell("OpenFile.FileName", vbNormalFocus)
Feel free to ignore this if that's not what you want to happen.
Here's a nicer copy/paste form:

Code:
   Private Sub CompileToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CompileToolStripMenuItem.Click
        Dim SaveFile As New SaveFileDialog
        SaveFile.FileName = ""
        SaveFile.Filter = "Pawno File (*.pwn)|*.pwn"
        If SaveFile.ShowDialog() Then
            Dim Write As New System.IO.StreamWriter(SaveFile.FileName)
            Write.Write(TextBox1.Text)
            Write.Close()
            Shell("pawncc.exe", vbNormalFocus)

            Dim OpenFile As New OpenFileDialog
            OpenFile.FileName = "compiled"
            OpenFile.Filter = "Pawno File (*.bat)|*.bat"

            If OpenFile.ShowDialog() Then
                Dim Write As New System.IO.StreamWriter(OpenFile.FileName) 
                Write.Write("pawncc.exe " & OpenFile.FileName)
                Write.Write(TextBox2.Text)
                Write.Close()
                Shell("OpenFile.FileName", vbNormalFocus)
            End If
        End If
    End Sub
Reply With Quote
  #3  
Old 08-18-2011, 12:10 PM
Shockey Shockey is offline
Newcomer
 
Join Date: Aug 2011
Posts: 5
Default

Thank you but im currently getting

Dim Write As New System.IO.StreamWriter(OpenFile.FileName)

Write Hides an variable in a enclosing block

Also, it wasn't writing the file correctly.

pawncc.exe C:\Documents and Settings\Chris\Desktop\Testing\compiledpause

Also, it didnt create the file as .bat

Last edited by Shockey; 08-18-2011 at 12:22 PM.
Reply With Quote
  #4  
Old 08-18-2011, 12:45 PM
NFITC1 NFITC1 is offline
Centurion
 
Join Date: Apr 2006
Posts: 102
Default

Quote:
Originally Posted by Shockey View Post
Thank you but im currently getting

Dim Write As New System.IO.StreamWriter(OpenFile.FileName)

Write Hides an variable in a enclosing block
Oversight on my part. The variable "Write" already exists in the If block above it. You can't create it with the same name. Rename the second Write variable to something like WriteBat and change the following three lines accordingly.

Quote:
Originally Posted by Shockey View Post
Also, it wasn't writing the file correctly.

pawncc.exe C:\Documents and Settings\Chris\Desktop\Testing\compiledpause

Also, it didnt create the file as .bat
I didn't make the "friendly copy/paste" perfect unfortunately. I mentioned that the line "Write.Write("pawncc.exe " & OpenFile.FileName)" needed to change and didn't change it. It should read:
Code:
WriteBat.Write("pawncc.exe " & SaveFile.FileName)
As to why it's not parsing correctly, that's probably from the way you're writing TextBox2.Text. I claimed to not know what the goal was and this is why I said that. If TextBox2.Text contains the "pause" command then you might want to write a CrLf to the end of the line with the pawncc.exe in it:

Code:
WriteBat.Write("pawncc.exe " & SaveFile.FileName & vbCrLf)
Also, since you set a default FileName, it won't add the extension to it. You need to check for the appropriate extension to the file before you actually write or change your default FileName:

Code:
OpenFile.FileName = "compiled.bat"
or
Code:
Dim WriteBat As New System.IO.StreamWriter(OpenFile.FileName & iif(OpenFile.FileName.EndsWith(".bat"), "", ".bat") )  'sneaky way to do this in one line without creating a new variable
Reply With Quote
  #5  
Old 08-18-2011, 03:10 PM
Shockey Shockey is offline
Newcomer
 
Join Date: Aug 2011
Posts: 5
Default

Okay, Thanks! Everything works good. One more thing. Is there a way that i can save the .bat file wherever the .pwn file is created? Then load the .bat where ever the .pwn file is?

Thanks!

Also, TextBox2.Text is pause

And its still writing the file like this.

pawncc.exe C:\Documents and Settings\Chris\Desktop\Testing\Test.pwn
pause

It should just be

pawncc.exe "Test.pwn"
pause

Heres the whole code.


Code:
    Private Sub CompileToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CompileToolStripMenuItem.Click
        Dim SaveFile As New SaveFileDialog
        SaveFile.FileName = ""
        SaveFile.Filter = "Pawno File (*.pwn)|*.pwn"
        If SaveFile.ShowDialog() Then
            Dim Write As New System.IO.StreamWriter(SaveFile.FileName)
            Write.Write(TextBox1.Text)
            Write.Close()
            Shell("pawncc.exe", vbNormalFocus)

            Dim OpenFile As New OpenFileDialog
            OpenFile.FileName = "compiled"
            OpenFile.Filter = "Pawno File (*.bat)|*.bat"

            If OpenFile.ShowDialog() Then
                Dim WriteBat As New System.IO.StreamWriter(OpenFile.FileName)
                WriteBat.Write("pawncc.exe " & SaveFile.FileName & vbCrLf)
                WriteBat.Write(TextBox2.Text)
                WriteBat.Close()
                Shell("OpenFile.FileName", vbNormalFocus)
            End If
        End If
    End Sub
Im sorry if im annoying, im just kinda new
Reply With Quote
  #6  
Old 08-18-2011, 03:18 PM
Shockey Shockey is offline
Newcomer
 
Join Date: Aug 2011
Posts: 5
Default

It works, except its still writing the file like this.


pawncc.exe C:\Documents and Settings\Chris\Desktop\Testing\test1.pwn
pause

And, Can i create the .bat when i save the .pwn and automatically load it?

Also,

Shell("OpenFile.FileName", vbNormalFocus)

That cant find my file, What do i need to change that to?
Reply With Quote
  #7  
Old 08-19-2011, 08:51 AM
NFITC1 NFITC1 is offline
Centurion
 
Join Date: Apr 2006
Posts: 102
Default

Quote:
Originally Posted by Shockey View Post
It works, except its still writing the file like this.


pawncc.exe C:\Documents and Settings\Chris\Desktop\Testing\test1.pwn
pause
I thought that was the format you wanted. What's not right? if you want quote marks around it you'll have to append four quote marks:

Code:
WriteBat.Write("pawncc.exe " & """" & SaveFile.FileName & """" & vbCrLf)
Quote:
Originally Posted by Shockey View Post
And, Can i create the .bat when i save the .pwn and automatically load it?
Isn't that what you're doing?

Quote:
Originally Posted by Shockey View Post
Also,

Shell("OpenFile.FileName", vbNormalFocus)

That cant find my file, What do i need to change that to?
Remove the quote marks. That's another mistake.
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
Getting A File Name And Adding It To A File
Getting A File Name And Adding It To A File
Getting A File Name And Adding It To A File Getting A File Name And Adding It To A File
Getting A File Name And Adding It To A File
Getting A File Name And Adding It To A File
Getting A File Name And Adding It To A File Getting A File Name And Adding It To A File Getting A File Name And Adding It To A File Getting A File Name And Adding It To A File Getting A File Name And Adding It To A File Getting A File Name And Adding It To A File Getting A File Name And Adding It To A File
Getting A File Name And Adding It To A File
Getting A File Name And Adding It To A File
 
Getting A File Name And Adding It To A File
Getting A File Name And Adding It To A File
 
-->