run cygwin terminal using vb - vb.net

i need a code to type and perform commands in cygwin terminal in hidden mode(background) using visual basic, i was using cmd but now i want to use a Linux source code so i must use linux.
i ran cmd in hidden mode successfully but it isn't working with cygwin, here is the cmd code:
Shell("cmd.exe /k tracert -h " & _h & " " & domain.Text & " > temp" & i + 1 & ".txt & exit", AppWinStyle.Hide, True)
so i have tried
Shell(""C:\cygwin\Cygwin.bat -k tracert -h " & _h & " " & domain.Text & " > temp" & i + 1 & ".txt & exit", AppWinStyle.Hide, True)
and
Shell("C:\cygwin\Cygwin.bat")
'SendKeys.Send("tracert -h " & _h & " " & domain.Text & " > temp" & i + 1 & ".txt"))
but this still didn't work where in the second code i still have to press enter in cygwin to process the traceroute and which should be automaticcaly processed, so i hope that i'll find help in here.

Shell("C:\cygwin\Cygwin.bat")
SendKeys.Send("tracert -h " & _h & " " & domain.Text & " > temp" & i + 1 & ".txt"))
SendKeys.Send("{ENTER}")
this is the answer, icase anyone needed this.

Related

Run a Powershell command (not script) from Excel VBA

I've searched SO, and I can find plenty of examples of running a PowerShell script from VBA, but I can't find any examples of just running a simple command.
For example, this works:
Dim retval As Variant
retval = Shell("PowerShell ""C:\MyScript.ps1""", vbNormalFocus)
But this does not:
Dim retval As Variant
Dim pscmd As String
pscmd = "PowerShell " & _
Chr(34) & "Get-ScheduledTask" & _
" -TaskName " & Chr(34) & "My Task" & Chr(34) & _
" -CimSession MYLAPTOP" & Chr(34)
retval = Shell(pscmd, vbNormalFocus)
Debug.Print pscmd
'pscmd = PowerShell "Get-ScheduledTask -TaskName "My Task" -CimSession MYLAPTOP"
I know I could write the PS command to a file, execute it as a script, and then delete the file, but that does not seem to be very elegant.
To run an inline Powershell command, you'll probably need to use the -Command param and surround your statement with quotes. It'll also be easier if you use single quotes within the command.
Try this out:
pscmd = "PowerShell -Command ""{Get-ScheduledTask -TaskName 'My Task' -CimSession MYLAPTOP}"""
Haven't tested but this but the execution policy may be a factor.
pscmd = "PowerShell -NoProfile -NoLogo -ExecutionPolicy Bypass -Command {" & _
Chr(34) & "Get-ScheduledTask" & _
" -TaskName " & Chr(34) & "My Task" & Chr(34) & _
" -CimSession MYLAPTOP" & Chr(34) & "}"

update sh file using VBA before perl script is run

I have a VBA that creates a new directory folder and creates a new text file in that directory. I am trying to run a perl script from the VBA and have created a batch file that gets called from the VBA. That bat file uses a shell file to run a script. The directory in the script is dynamic and changes each time based on user input. My question is can the .sh file be updated before it is run? I apologize for the long post, just wanted to be complete. Thank you :).
VBA
Private Sub CommandButton3_Click()
Dim MyBarCode As String ' Enter Barcode
Dim MyScan As String ' Enter ScanDate
Dim MyDirectory As String
MyBarCode = Application.InputBox("Please enter the barcode", "Bar Code", Type:=2)
If MyBarCode = "False" Then Exit Sub 'user canceled
Do
MyScan = Application.InputBox("Please enter scan date", "Scan Date", Date, Type:=2)
If MyScan = "False" Then Exit Sub 'user canceled
If IsDate(MyScan) Then Exit Do
MsgBox "Please enter a valid date format. ", vbExclamation, "Invalid Date Entry"
Loop
Range("B20").Value = MyBarCode
Range("B21").Value = CDate(MyScan)
MyDirectory = "N:\1_DATA\MicroArray\NexusData\" & MyBarCode & "_" & Format(CDate(MyScan), "m-d-yyyy") & "\"
' Create nexus directory and folder
If Dir(MyDirectory, vbDirectory) = "" Then MkDir MyDirectory
If MsgBox("The project file has been created. " & _
"Do you want to create a template for analysis now?", _
vbQuestion + vbYesNo) = vbYes Then
'Write to text file
Open MyDirectory & "sample_descriptor.txt" For Output As #1
Print #1, "Experiment Sample" & vbTab & "Control Sample" & vbTab & "Display Name" & vbTab & "Gender" & vbTab & "Control Gender" & vbTab & "SpikeIn Location"
Print #1, MyBarCode & "_532Block1.txt" & vbTab & MyBarCode & "_635Block1.txt" & vbTab & ActiveSheet.Range("B8").Value & " " & ActiveSheet.Range("B9").Value & vbTab & ActiveSheet.Range("B10").Value & vbTab & ActiveSheet.Range("B5").Value & vbTab & ActiveSheet.Range("B11").Value & vbTab & ActiveSheet.Range("B12").Value
Print #1, MyBarCode & "_532Block2.txt" & vbTab & MyBarCode & "_635Block2.txt" & vbTab & ActiveSheet.Range("C8").Value & " " & ActiveSheet.Range("C9").Value & vbTab & ActiveSheet.Range("C10").Value & vbTab & ActiveSheet.Range("C5").Value & vbTab & ActiveSheet.Range("C11").Value & vbTab & ActiveSheet.Range("C12").Value
Print #1, MyBarCode & "_532Block3.txt" & vbTab & MyBarCode & "_635Block3.txt" & vbTab & ActiveSheet.Range("D8").Value & " " & ActiveSheet.Range("D9").Value & vbTab & ActiveSheet.Range("D10").Value & vbTab & ActiveSheet.Range("D5").Value & vbTab & ActiveSheet.Range("D11").Value & vbTab & ActiveSheet.Range("D12").Value
Print #1, MyBarCode & "_532Block4.txt" & vbTab & MyBarCode & "_635Block4.txt" & vbTab & ActiveSheet.Range("E8").Value & " " & ActiveSheet.Range("E9").Value & vbTab & ActiveSheet.Range("E10").Value & vbTab & ActiveSheet.Range("E5").Value & vbTab & ActiveSheet.Range("E11").Value & vbTab & ActiveSheet.Range("E12").Value
Close #1
'Run ImaGene
If MsgBox("Please run the ImaGene analysis. " & _
"and click yes after it completes to verify the spike-ins.", _
vbQuestion + vbYesNo) = vbYes Then
**'Update folder structure and call .bat
Dim PathCrnt As String
Dim FN As Long
FN = FreeFile 'FreeFile gets an available file number'
Open "C:\cygwin\home\cmccabe\Run_probes.sh" For Output As FN
PathCrnt = ActiveWorkbook.Path
*system.diagnostics.process.Start ("C:\Users\cmccabe\Desktop\NxClinical.bat") & PathCrnt*
Close FN
End If**
End Sub
Else
MsgBox "Nothing has been done. ", vbExclamation, "Goodbye!"
End If
Application.DisplayAlerts = False
Application.Quit
End Sub
Bat with that calls perl script:
C:\cygwin\bin\bash --login -i ./Run_probes.sh
Run_probes.sh
perl "C:\cygwin\home\cmccabe\get_imagene_spikein_probe_values.pl" "N:\1_DATA\MicroArray\NexusData\*257168310045_8-18-2015*" "ImaGene EmArray- Template.txt" < test_probes8.txt > "N:\1_DATA\MicroArray\NexusData\*257168310045_8-18-2015*\output.txt"
Nice post! Very detailed. :)
Certainly the Perl file can be updated before it is run. Just write the update path into the file like you write the data into sample_descriptor.txt. eg:
Dim FN as Long
FN = FreeFile 'FreeFile gets an available file number'
Open PathToShFile For Output As FN
Print FN, "perl " & chr(34) & PathCrnt & _
& chr(34) & "\get_imagene_spikein_probe_values.pl" & chr(34) & _
& chr(34) & "N:\1_DATA\MicroArray\NexusData\*257168310045_8-18-2015*" & _
chr(34) & " " & chr(34) & _
" ImaGene EmArray- Template.txt" & chr(34) & _
" < test_probes8.txt > " & chr(34) & _
"N:\1_DATA\MicroArray\NexusData\*257168310045_8-18-2015*\output.txt" & chr(34)
Close FN
I'm not sure I got all the " marks replaced with chr(34) correctly so make sure to echo out that string before trying to execute any code. You might also make it more readable (and configurable) by using variables to store paths. eg:
strNPath = "N:\1_DATA\MicroArray\NexusData\*257168310045_8-18-2015*\"

Treenode not appearing when program restarts

the below code is run from a double click event on a listbox that copies the selected file to the selected node directory on a treeview then adds the selected text as a child node.
It appears to work fine however when the program is closed and then re-opened its not showing the child node.
Any pointers........
Dim Copy2 = aMailbox & tvProgress.SelectedNode.Text & "\" & lstRequired.Text
Dim Copy1 = rPath & "\" & lstRequired.Text
If File.Exists(Copy2) Then
MsgBox("File already added. Please edit from the view above", MsgBoxStyle.OkOnly, "Lynx Control Panel")
Exit Sub
End If
If File.Exists(Copy1) Then
File.Copy(Copy1, Copy2)
tvProgress.SelectedNode.Nodes.Add(lstRequired.Text)
tvProgress.ExpandAll()
Else
MsgBox("This file no longer exists in your Lynx Repository. Please select another", MsgBoxStyle.OkOnly, "Lynx Control Panel")
Exit Sub
End If
The below code is taken entirely from a doubleclick event of the 1st listbox
Dim n As Integer
Dim i As Integer = lstPlanned.SelectedIndex
If lstPlanned.SelectedItems.Count = 0 Then Exit Sub
For n = 0 To UBound(AllDetails)
If AllDetails(n).uName & " - " & AllDetails(n).uCode & " - " & AllDetails(n).uOps = lstPlanned.SelectedItem Then
If Not My.Computer.FileSystem.DirectoryExists(zMailbox & AllDetails(n).uFile) Then
MsgBox("No files located for " & vbNewLine & (AllDetails(n).uName & " (" & AllDetails(n).uCode) & ")" & vbNewLine & " " & vbNewLine & "Please try another...", MsgBoxStyle.OkOnly, "Lynx Control Panel")
Exit Sub
End If
System.IO.Directory.CreateDirectory(aMailbox & "\" & AllDetails(n).uName & " - " & AllDetails(n).uCode & " - " & AllDetails(n).uOps)
For Each f In Directory.GetFiles(zMailbox & AllDetails(n).uFile, "*.dbf")
If File.Exists(f) Then
File.Copy(f, Path.Combine(aMailbox & "\" & AllDetails(n).uName & " - " & AllDetails(n).uCode & " - " & AllDetails(n).uOps, Path.GetFileName(f)), True)
End If
Next
For Each f In Directory.GetFiles(zMailbox & AllDetails(n).uFile, "*.ini", SearchOption.AllDirectories)
If File.Exists(f) Then
File.Copy(f, Path.Combine(aMailbox & "\" & AllDetails(n).uName & " - " & AllDetails(n).uCode & " - " & AllDetails(n).uOps, Path.GetFileName(f)), True)
End If
Next
For Each f In Directory.GetFiles(zMailbox & AllDetails(n).uFile, "*.txt", SearchOption.AllDirectories)
If File.Exists(f) Then
File.Copy(f, Path.Combine(aMailbox & "\" & AllDetails(n).uName & " - " & AllDetails(n).uCode & " - " & AllDetails(n).uOps, Path.GetFileName(f)), True)
End If
Next
tvProgress.Nodes.Remove(rN)
tvProgress.Nodes.Insert(0, rN)
tvProgress.Nodes.Add(New TreeNode(AllDetails(n).uName & " - " & AllDetails(n).uCode & " - " & AllDetails(n).uOps))
If i >= 0 And i < lstPlanned.Items.Count Then
lstPlanned.Items.RemoveAt(i)
End If
Exit Sub
End If
Next
Think that's want you want..maybe ;)
...
System.IO.Directory.CreateDirectory(aMailbox & "\" & AllDetails(n).uName & " - " & AllDetails(n).uCode & " - " & AllDetails(n).uOps)
' After the Dir is created Node is added to the TreeView
tvProgress.Nodes.Remove(rN)
tvProgress.Nodes.Insert(0, rN)
tvProgress.Nodes.Add(New TreeNode(AllDetails(n).uName & " - " & AllDetails(n).uCode & " - " & AllDetails(n).uOps))
For Each f In Directory.GetFiles(zMailbox & AllDetails(n).uFile, "*.dbf")
If File.Exists(f) Then
File.Copy(f, Path.Combine(aMailbox & "\" & AllDetails(n).uName & " - " & AllDetails(n).uCode & " - " & AllDetails(n).uOps, Path.GetFileName(f)), True)
'As ParentNode already exists as Pos 0 you can add child nodes to it
tvProgress.Nodes(0).Nodes.Add(Path.GetFileName(f))
End If
Next
...

VB.NET Lambda expression instead of iterator

I am fairly certain that this could be rewritten as a Lambda expression, but every attempt fails miserably. I know, C# Lambda reads cleaner, but I'm stuck with VB.NET. Here is the code - could someone point me in the right direction? Thanks!
For Each e As EventToMonitor In Events
If e.TypeID = 1 Then
If ("," & e.Values).Contains("," & b.ChoiceID & ",") Then
Notify(cacheValues, e, "Event notification (button press)", "Button pressed: " & b.Text & " on screen: " & b.GroupBox.Text & Environment.NewLine &
"User: " & cacheValues.CurrentUserName & Environment.NewLine & _
"Pressed at: " & Date.Now.ToShortDateString & " " & Date.Now.ToShortTimeString)
End If
End If
Next
Something like this should work if you want to convert the whole thing to lambda:
Events.Where(Function(e) e.TypeID = 1 AndAlso ("," & e.Values).Contains("," & b.ChoiceID & ",")) _
.ToList() _
.ForEach(Sub(e) Notify(cacheValues, e, "Event notification (button press)", "Button pressed: " & b.Text & " on screen: " & b.GroupBox.Text & Environment.NewLine & _
"User: " & cacheValues.CurrentUserName & Environment.NewLine & _
"Pressed at: " & Date.Now.ToShortDateString & " " & Date.Now.ToShortTimeString))
Honestly, though, I usually prefer to just use them to filter down results or build a collection:
Dim eventsList = Events.Where(Function(e) e.TypeID = 1 AndAlso ("," & e.Values).Contains("," & b.ChoiceID & ","))
For Each e As EventToMonitor In eventsList
Notify(cacheValues, e, "Event notification (button press)", "Button pressed: " & b.Text & " on screen: " & b.GroupBox.Text & Environment.NewLine &
"User: " & cacheValues.CurrentUserName & Environment.NewLine & _
"Pressed at: " & Date.Now.ToShortDateString & " " & Date.Now.ToShortTimeString)
Next

My automatic phonegap installer needs jdk download with wget, I have it working in command prompt but not in .hta file

I have written a vbscript based .hta installer to install PhoneGap/Cordova automatically with all the pre-requisites(ant/jdk/bada sdk etc). Its almost done but I am stuck at the part where I need to download JDK directly.
If you copy paste this (long) line in commandprompt and it works
wget --header "Cookie: gpw_e24=http://www.oracle.com/technetwork/java/javase/downloads/jdk7-downloads-1880260.html;" ""http://download.oracle.com/otn-pub/java/jdk/7u17-b02/jdk-7u17-windows-i586.exe -O jdk.exe
It will download JDK directly , absolutely fine .
When you paste the code snippet in notepad e.g and save as .vbs and run it then it doesnt work
Set objShell = CreateObject("WScript.Shell")
objShell.CurrentDirectory = "c:\"
'inQuotes function just puts quotes around the command parameters
objShell.Run inQuotes(WorkingDir & "\wget.exe") & " " & " --header " & inQuotes("Cookie: gpw_e24=http://www.oracle.com/technetwork/java/javase/downloads/jdk7-downloads-1880260.html; ") & "http://download.oracle.com/otn-pub/java/jdk/7u17-b02/jdk-7u17-windows-i586.exe" & " -O" & " " & inQuotes("jdk.exe"),1,True
Function inQuotes(toQuote)
'return with quotes around the toQuote parameter
inQuotes = chr(34) & toQuote & chr(34)
End Function
Somewhere in teh objShell.Run line there is a problem and wget isn't downloading the jdk ...
Will really appreciate any help
thank you :)
Is your variable WorkingDir is defined? If I put one echo:
strCmd = inQuotes(WorkingDir & "\wget.exe") & " " & " --header " _
& inQuotes("Cookie: gpw_e24=http://www.oracle.com/technetwork/java/javase/downloads/jdk7-downloads-1880260.html; ") _
& "http://download.oracle.com/otn-pub/java/jdk/7u17-b02/jdk-7u17-windows-i586.exe" & " -O" & " " & inQuotes("jdk.exe")
WScript.Echo strCmd
I get this:
"\wget.exe" --header "Cookie: gpw_e24=http://www.oracle.com/technetwork/java/javase/downloads/jdk7-downloads-1880260.html; "http://download.oracle.com/otn-pub/java/jdk/7u17-b02/jdk-7u17-windows-i586.exe -O "jdk.exe"
And this not pass to the original command. Maybe it s'd be like this?
strCmd = "wget.exe --header " _
& inQuotes("Cookie: gpw_e24=http://www.oracle.com/technetwork/java/javase/downloads/jdk7-downloads-1880260.html;") _
& " " & chr(34) &chr(34) & "http://download.oracle.com/otn-pub/java/jdk/7u17-b02/jdk-7u17-windows-i586.exe" _
& " -O jdk.exe"
WScript.Echo strCmd