Trying to create a batch file from VB 2010 Express - vb.net

Why does a batch file run when I create it in Notepad, but not when I create it in my VB code?
Dim strStartFile As String = "C:\Documents and Settings\All Users\StartMenu\Programs\Startup\Starter.bat"
If Not File.Exists(strStartFile) Then
Dim strBatLine1 As String = "cd C:\Progra~1\Applic~1 && start Application.exe"
My.Computer.FileSystem.WriteAllText(strStartFile, strBatLine1, False)
SetAttr(strStartFile, FileAttribute.Normal)
End If
It creates the file just fine. It looks exactly the same as the handmade version, it just won't launch the exe when double clicked. I've tried appending CR+LF, vbCrLf, but no go.
There is an inherent problem when trying to launch the exe directly from Startup, it runs it from that directory and can't find the related files (in the Application directory) so the cd is necessary.
Using VB 2010 Express. Thanks in advance for your help!

You probably need to pass in the Systems ANSI CodePage, because you are executing the file from cmd.exe
My.Computer.FileSystem.WriteAllText(strStartFile, strBatLine1, False, System.Text.Encoding.Default);

Related

Call .reg File from Excel-VBA script

I want to call a .reg File out of my VBA script. I'm using Office/Excel 2013.
I know Excel can't run these files by itself, so i need to call the file via shell. The code i wrote doesn't work:
Sub deactivateHyperlinkWarnings()
Dim x
x = Shell("cmd /C C:\TEMP\DisableHyperlinkWarnings.reg")
End Sub
I found this piece of code somewhere on the web, but its not working. I don't even get an error message. The .reg File is located in C:\TEMP
What do i need to write to make it work?
Plus: Is it possible to suppress the MessageBoxes that are displayed when i run the .reg-File? When i start the file manually, i need to Hit "OK" like 3 Times. The people who are working with the Excelsheet later on shouldn't be seeing these things.
Instead of running cmd try to run reg. So in your case it should be x = Shell("reg import C:\TEMP\DisableHyperlinkWarnings.reg")
More info here

Shell() website url

Is it possible to run website url using Shell() command? I saw someone post
Shell() can only read the executable path
But regarding to this site http://www.vb6.us/forums/general-questions/attaching-website-links-your-command-button Shell() can used to run the website url.
I have some website url inside my XML file and I tried to run them using Shell() command as my XML file also containing .exe file path. So I am running those .exe file and website url like this
Dim i As Integer, j As Integer
For i = 0 To 9
For j = 0 To 9
If MenuListBox.SelectedItem = MenuListBox(i, j, 0) Then
Shell(MenuListBox(i, j, 1))
End If
Next
Next
I am using array to store each of elements inside my XML file.
So the problem here is, I can only run my .exe files and when running website url it said that
File not found
Even though my path is correct. I did used the Process.Start() also but it only working for the website url, not the .exe file. It returns me this error.
The system cannot find the file specified
Kindly to help me. Thanks in advance.
Process.Start() can be used for url and executables and other files. If you pass a path to a file, like a doc file, it is open with default application. In your case if you pass a url like "http://www.google.com" it will be opened with your default browser.
According to MSDN:
Starting a process by specifying its file name is similar to typing
the information in the Run dialog box of the Windows Start menu.
Therefore, the file name does not need to represent an executable
file. It can be of any file type for which the extension has been
associated with an application installed on the system. For example
the file name can have a .txt extension if you have associated text
files with an editor, such as Notepad, or it can have a .doc if you
have associated.doc files with a word processing tool, such as
Microsoft Word. Similarly, in the same way that the Run dialog box can
accept an executable file name with or without the .exe extension, the
.exe extension is optional in the fileName parameter. For example, you
can set the fileName parameter to either "Notepad.exe" or "Notepad".
opening a url here and here
Quick solution, to get time to check the real solution:
If path.toupper like "*.EXE"
shell path
Else
process.start (path)
End if
But if you have a Win32Exception (show us the full message) ... they used to appear on 32/64 bits issue. etc. But, as Shell is working, I think you have a Credentials issue= permissions of that folder/exe.
Place that exe on another granted location to test.
Thanks to #Capitán Cavernícola for your suggestion.
If path.toupper like "*.EXE"
shell path
Else
process.start (path)
End if
I took your code and change the path.toupper like "*.EXE" to Path.Contains(".exe") Then
Here is my coding that working all fine now.
Dim Path As String = MenuListBox(i, j, 1)
If Path.Contains(".exe") Then
Shell(Path)
Else
Process.Start(Path)
End If
Thank you all :)

multiple programming try to access same text file

I had created and text file using (AFL SCRIPTING LANGUAGE) this script will update (write) to a text file every 5 seconds. I will try to read file using vb.net, when I run the vb.net code form visual studio, everything works fine but (AFL script not able to update the text file), here is my vb.net code:
Dim FILE_NAME As New FileStream("C:\myreport\myfile.TXT", FileMode.Open, FileAccess.Read, FileShare.Read)
REM Dim FILE_NAME As String = "C:\myreport\myfile.TXT"
REM Dim TextLine As String
REM If System.IO.File.Exists(FILE_NAME) = True Then
Dim objReader As New System.IO.StreamReader(FILE_NAME)
Do While objReader.Peek() <> -1
MYSTRING(I) = objReader.ReadLine()
I = I + 1
Loop
REM End If
When I run the code above, ( AFL script not able to update the text file),
Put simply:
When I run the vb.net code (for accessing the text file), AFL script not able to update.
I had share read/write the folder (where the text file exists), no effect, same problem I am facing.
Unchecked "Enable visual studio hosting process", still problem not solved.
In .NET, the FileSystemWatcher class can help you with this problem. You can read the file each time the file watcher says that the files has changed. Here is the reference documentation for it.
You cannot read and write to a file from two processes at the same time. Well, technically you can, but it can lead to a race condition which is bad.
You need to implement some kind of shared locking mechanism around the file to prevent your two programs from fighting over it. Or, if you can guarantee that the consumer VB.NET program will have the file open for less than 5 seconds, you can go with MrAxel's solution and simply have the VB.NET program read the file every time it is updated.

How to use Process.Start

I'm using process.Start to run Convert.exe. This program's purpose is to convert all files which are in the exe's folder. So when I normally use it, I copy paste a file into the same folder as Convert.exe and then run Convert.exe. Convert.exe will create a new "converted" file in the same folder.
I'm trying to automate this tedious process. A User selects a file which needs to be converted from FolderA, I copy it to the same folder where Convert.exe is and I'm using process.start(Convert.exe) to run it.
Just to be clear, this "Convert.exe" accepts NO arguments.
The problem: "Convert.exe" is not converting the files in its folder. Instead it's converting all the files in FolderA for some weird reason. I don't know why it picked that folder, I never even try to send it as an argument or nothing.
Here's the code I have:
Dim techInfo As New System.IO.FileInfo(itm.strFilePath)
techInfo.CopyTo(ConverterPath & techInfo.Name)
Dim procInfoConvert As New ProcessStartInfo
procInfoConvert.CreateNoWindow = False
procInfoConvert.Arguments = ""
procInfoConvert.FileName = ConverterPath & "Convert.exe"
Dim procConvert As Process = Process.Start(procInfoConvert)
I did a test where I copy pasted a file into the "Convert.exe" folder and then just run this code:
process.start(ConverterPath & "Convert.exe")
The exe returns nothing, same as if there was no files in the folder.
The only thing I can think of is that when process.Start is run, it copies the file to another location and runs it from there.
Any ideas anyone?
Try this:
procInfoConvert.WorkingDirectory = ConverterPath
That'll set the process up to start in the directory it's contained in, instead of the current directory.

How can I use VB.NET to execute a batch file on another computer?

In vb.net 2008 I want to execute a batch file that resides on another computer.
There is no error, but nothing happens.
Here is the code:
Dim pStart As New System.Diagnostics.Process
Dim startInfo As New System.Diagnostics.ProcessStartInfo(serverpath & "\file.bat")
startInfo.RedirectStandardOutput = True
startInfo.WindowStyle = ProcessWindowStyle.Hidden
startInfo.UseShellExecute = False
pStart = System.Diagnostics.Process.Start(startInfo)
pStart.WaitForExit()
pStart.Close()
To run a process on a remote computer you can use Sysinternals free psexec.
You can call it with the proper parameters and having the required permissions like you are doing in your sample code.
I've never tried to create a Process using a batch file as the executable. I've always had to use cmd.exe as the program. This has worked for me in the past:
Dim startInfo As New System.Diagnostics.ProcessStartInfo("cmd.exe", "/c " & serverpath & "\file.bat")
The "/c" as part of the argument list tells cmd.exe to exit after the batch file has completed.
If you are going to use RedirectStandardOutput, you really do want to use RedirectStandardError, and then also subscribe to the events of the Process class for catching data on those streams (OutputDataReceived and ErrorDataReceived). Otherwise you will have no way to debug your batch script.
This reads like a permissions problem. I would troubleshoot it that way if you haven't ruled it out yet.
Have you tried running the same batch file from the local computer?
If it is a permissions issue you can solve it either copying the file locally before executing it or map a drive to the remote computer where the file is and then execute the batch file from the new path.
Additionally, we don't really know what's in the batch file that could be causing an issue. I would either post the batch file or if you can't post the batch file use a batch file that you can post. Example a batch file would write the current date time to a file.