I want to insert a batch file into my vb project. So I thought a resource file would be easiest. The thing is I dont know the code for a .bat resource file. Please enlighten me on how to insert a .bat resource file.
Printable View
I want to insert a batch file into my vb project. So I thought a resource file would be easiest. The thing is I dont know the code for a .bat resource file. Please enlighten me on how to insert a .bat resource file.
Something a bit like this :
1. Create a text file containing the script for your batch file.
2. Put your text file in the resource file under the Custom directory
3. In your program retrieve the file from the resource file using LoadTextResource or just SaveResItemtoDisc and the following functions.
VB Code:
Public Function LoadTextResource(ByVal ResourceID As String, ByVal sResourceType As String, Optional TempFile) As String 'Returns a text file from a resource file. 'EXAMPLE CALL: 'Text1 = LoadTextResource(101, "Custom", "C:\temp\temp.tmp") Dim sFilename As String Dim iFileNum As Integer Dim sText As String 'Check if the TempFile Name has been specified If IsMissing(TempFile) Then 'Create a temp file name such as "~res1234.tmp" GetTempFile "", "~rs", 0, sFilename Else 'Use the specified temp file name sFilename = TempFile End If 'Save the resource item to disk If SaveResItemToDisk(ResourceID, sResourceType, sFilename) = 0 Then iFileNum = FreeFile 'Get free file handle Open sFilename For Input As iFileNum 'Open the file LoadTextResource = Input(LOF(iFileNum), iFileNum) 'Read all of the text from the file Close iFileNum Kill sFilename 'Delete the temp file End If End Function Public Function SaveResItemToDisk(ByVal iResourceNum As String, ByVal sResourceType As String, ByVal sDestFileName As String) As Long 'Saves a resource item to disk 'Returns 0 on success, error number on failure 'Example Call: 'iRetVal = SaveResItemToDisk(101, "CUSTOM", "C:\myImage.gif") Dim bytResourceData() As Byte Dim iFileNumOut As Integer On Error GoTo SaveResItemToDisk_err 'Retrieve the resource contents (data) into a byte array bytResourceData = LoadResData(iResourceNum, sResourceType) 'Get Free File Handle iFileNumOut = FreeFile 'Open the output file Open sDestFileName For Binary Access Write As #iFileNumOut 'Write the resource to the file Put #iFileNumOut, , bytResourceData 'Close the file Close #iFileNumOut 'Return 0 for success SaveResItemToDisk = 0 Exit Function SaveResItemToDisk_err: 'Return error number SaveResItemToDisk = Err.Number End Function Public Function GetTempFile(ByVal strDestPath As String, ByVal lpPrefixString As String, ByVal wUnique As Integer, lpTempFilename As String) As Boolean ' Get a temporary filename for a specified drive and filename prefix ' PARAMETERS: ' strDestPath - Location where temporary file will be created. If this ' is an empty string, then the location specified by the ' tmp or temp environment variable is used. ' lpPrefixString - First three characters of this string will be part of ' temporary file name returned. ' wUnique - Set to 0 to create unique filename. Can also set to integer, ' in which case temp file name is returned with that integer ' as part of the name. ' lpTempFilename - Temporary file name is returned as this variable. ' RETURN: ' True if function succeeds; false otherwise If strDestPath = "" Then ' No destination was specified, use the temp directory. strDestPath = String(255, vbNullChar) If GetTempPath(255, strDestPath) = 0 Then GetTempFile = False Exit Function End If End If lpTempFilename = String(255, vbNullChar) GetTempFile = GetTempFilename(strDestPath, lpPrefixString, wUnique, lpTempFilename) > 0 lpTempFilename = StripTerminator(lpTempFilename) End Function Public Function StripTerminator(ByVal strString As String) As String ' Returns a string without any zero terminator. Typically, ' this was a string returned by a Windows API call. ' ' IN: [strString] - String to remove terminator from ' ' Returns: The value of the string passed in minus any ' terminating zero. Dim intZeroPos As Integer intZeroPos = InStr(strString, Chr$(0)) If intZeroPos > 0 Then StripTerminator = Left$(strString, intZeroPos - 1) Else StripTerminator = strString End If End Function
Working with Resource Files
A resource file allows you to collect all of the version-specific text and bitmaps for an application in one place. This can include icons, screen text, and other material that may change between localized versions or between revisions or specific configurations.
Adding Resources to a Project
You can create a resource file using the Resource Editor add-in. The compiled resource file will have a .res file name extension. Each project can contain only one resource file.
The actual file consists of a series of individual strings, bitmaps, or other items, each of which has a unique identifier. The identifier is either a Long or a String, depending on the type of data represented by the resource. Strings, for example, have a Long identifier, while bitmaps have a Long or String identifier. To retrieve resources in your code, learn the identifier for each resource. The function parameters referring to the resources can use the Variant data type.
To add a new resource file to your project
Choose Resource Editor from the Tools menu. An empty resource file will be opened in the Resource Editor window.
Note The Resource Editor add-in must be installed. For information on installing add-ins, see "Using Wizards and Add-Ins" in "Managing Projects".
Select the Save button on the Resource Editor toolbar to save the resource file. The file will be added to the Project Explorer under the Related Documents section.
To add an existing resource file to your project
Choose Add New Resource File from the Project menu. Any existing resource file in your project will be replaced.
Caution If you make any modifications to an existing resource file it could affect other projects that use that resource file. Make sure that you save the file under a new filename.
Note The Resource Editor add-in must be installed. For information on installing add-ins, see "Using Wizards and Add-Ins" in "Managing Projects."
For More Information For more information on resource files, see "Using Resource Files for Localization" in "International Issues."
Note Windows resource files are specific to 16-bit or 32-bit applications. Visual Basic will generate an error message if you try to add a 16-bit resource file to a project.
Using Resources in Code
Visual Basic provides three functions for retrieving data from the resource file for use in code.
Function Description
LoadResString Returns a text string.
LoadResPicture Returns a Picture object, such as a bitmap, icon, or cursor.
LoadResData Returns a Byte array. This is used for .wav files, for example.
For More Information See the appropriate function topic.
From help files
Seahag
Do you think you could give me an attachment with an example for me to go off of so I know what Im doing?
Have the batch file do this:
@echo off
echo VB
pause > nul Press any key to succeed with this batch file...
cls
Here you go.
http://www.vbforums.com/attachment.p...postid=1282010
Seahag