Member_2_636999
asked on
Text in Dos To windows
How do I transfer the text in dos such as the list of files after a dir command to an application created in visual basic
hi,
What is the question? Have you solved it?
Cheers.
What is the question? Have you solved it?
Cheers.
That code should solve the problem, but why you need to do that (i mean, read data from DOS window)?
ASKER
The code above works only when the information displayed in DOS is very small
I suppose the problem is with the buffer size.How do i do it for large amount of information
I suppose the problem is with the buffer size.How do i do it for large amount of information
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
the code above is wrong... Try this one:
' executes the command
Shell "command.com /c dir *.* > c:\file.txt", vbMinimizedNoFocus
'reads the file to a text box
Open "c:\file.txt" For Input As #1
While Not EOF(1)
Line Input #1, filetext
Text1.Text = Text1.Text & filetext & vbCrLf
Wend
' executes the command
Shell "command.com /c dir *.* > c:\file.txt", vbMinimizedNoFocus
'reads the file to a text box
Open "c:\file.txt" For Input As #1
While Not EOF(1)
Line Input #1, filetext
Text1.Text = Text1.Text & filetext & vbCrLf
Wend
why would you need to do that, that code doesnt make any sence,
if you are wanting just to read a file from the hard drive which you are defining as DOS the just do this :-
private command1_click()
on error goto errhandler:
open "c:\whateverfileis.txt" for input as#1
while not eof(1)
input #1,newtext
wend
close#1
errhandler:
close#1
end sub
If you are trying to read an .EXE file this code will not work. but then again neither will anybody elses who have recently submitted before mine.
o boy.
if you are wanting just to read a file from the hard drive which you are defining as DOS the just do this :-
private command1_click()
on error goto errhandler:
open "c:\whateverfileis.txt" for input as#1
while not eof(1)
input #1,newtext
wend
close#1
errhandler:
close#1
end sub
If you are trying to read an .EXE file this code will not work. but then again neither will anybody elses who have recently submitted before mine.
o boy.
ive had alook at what you require.
cut and paste this code this should do the trick for you. it does exactly what you require....
Dim fso, strPath, fsoFolder, fsoTextFile, strTemp, strData, fsoFile, strFile
Set fso = CreateObject("Scripting.Fi leSystemOb ject")
strPath = "?"
If WScript.Arguments.Count > 0 Then strPath = WScript.Arguments(0)
Do While Not fso.FolderExists(strPath)
strPath = InputBox ("Enter the directory to detail:","Directory Lister" )
If Len(strPath) = 0 Then WScript.Quit
Loop
Set fsoFolder = fso.GetFolder(strPath)
strFile = "dir" & fsoFolder.Name & ".txt"
strTemp = Left(WScript.ScriptFullNam e,Len(WScr ipt.Script FullName) - Len(WScript.ScriptName))
Set fsoTextFile = fso.openTextFile(strTemp & strFile, 2, True)
fsoTextFile.WriteLine "Directory information for: " & strPath
fsoTextFile.WriteBlankLine s(1)
strData = "File Name" & String(16," ")
strData = strData & "Size" & String(4," ")
strData = strData & "Date Modified "
strData = strData & "File Type"
fsoTextFile.WriteLine (strData)
fsoTextFile.WriteBlankLine s(1)
For Each fsoFile in fsoFolder.Files
If fsoFile.Name <> strFile AND fsoFile.Name <> WScript.ScriptName Then
strTemp = Left(fsoFile.Name,24)
strData = strTemp & String(25-Len(strTemp)," ")
strTemp = CLng(fsoFile.Size / 1024) & " KB"
strData = strData & strTemp & String(8-Len(strTemp)," ")
strTemp = Month(fsoFile.DateLastModi fied) & "/" & Day(fsoFile.DateLastModifi ed) & "/" & Year(fsoFile.DateLastModif ied)
strData = strData & strTemp & String(15-Len(strTemp)," ") & fsoFile.Type
fsoTextFile.WriteLine (strData)
End If
Next
WScript.Echo "Detailing for directory " & strPath & " is complete. The results are in the file named " & strFile
WScript.Quit
cut and paste this code this should do the trick for you. it does exactly what you require....
Dim fso, strPath, fsoFolder, fsoTextFile, strTemp, strData, fsoFile, strFile
Set fso = CreateObject("Scripting.Fi
strPath = "?"
If WScript.Arguments.Count > 0 Then strPath = WScript.Arguments(0)
Do While Not fso.FolderExists(strPath)
strPath = InputBox ("Enter the directory to detail:","Directory Lister" )
If Len(strPath) = 0 Then WScript.Quit
Loop
Set fsoFolder = fso.GetFolder(strPath)
strFile = "dir" & fsoFolder.Name & ".txt"
strTemp = Left(WScript.ScriptFullNam
Set fsoTextFile = fso.openTextFile(strTemp & strFile, 2, True)
fsoTextFile.WriteLine "Directory information for: " & strPath
fsoTextFile.WriteBlankLine
strData = "File Name" & String(16," ")
strData = strData & "Size" & String(4," ")
strData = strData & "Date Modified "
strData = strData & "File Type"
fsoTextFile.WriteLine (strData)
fsoTextFile.WriteBlankLine
For Each fsoFile in fsoFolder.Files
If fsoFile.Name <> strFile AND fsoFile.Name <> WScript.ScriptName Then
strTemp = Left(fsoFile.Name,24)
strData = strTemp & String(25-Len(strTemp)," ")
strTemp = CLng(fsoFile.Size / 1024) & " KB"
strData = strData & strTemp & String(8-Len(strTemp)," ")
strTemp = Month(fsoFile.DateLastModi
strData = strData & strTemp & String(15-Len(strTemp)," ") & fsoFile.Type
fsoTextFile.WriteLine (strData)
End If
Next
WScript.Echo "Detailing for directory " & strPath & " is complete. The results are in the file named " & strFile
WScript.Quit
Well, i put the upper buffer a little up and work for me.
Private Declare Function CreatePipe Lib "kernel32" ( _
phReadPipe As Long, _
phWritePipe As Long, _
lpPipeAttributes As Any, _
ByVal nSize As Long) As Long
Private Declare Function ReadFile Lib "kernel32" ( _
ByVal hFile As Long, _
ByVal lpBuffer As String, _
ByVal nNumberOfBytesToRead As Long, _
lpNumberOfBytesRead As Long, _
ByVal lpOverlapped As Any) As Long
Private Type SECURITY_ATTRIBUTES
nLength As Long
lpSecurityDescriptor As Long
bInheritHandle As Long
End Type
Private Type STARTUPINFO
cb As Long
lpReserved As Long
lpDesktop As Long
lpTitle As Long
dwX As Long
dwY As Long
dwXSize As Long
dwYSize As Long
dwXCountChars As Long
dwYCountChars As Long
dwFillAttribute As Long
dwFlags As Long
wShowWindow As Integer
cbReserved2 As Integer
lpReserved2 As Long
hStdInput As Long
hStdOutput As Long
hStdError As Long
End Type
Private Type PROCESS_INFORMATION
hProcess As Long
hThread As Long
dwProcessID As Long
dwThreadID As Long
End Type
Private Declare Function CreateProcessA Lib "kernel32" (ByVal _
lpApplicationName As Long, ByVal lpCommandLine As String, _
lpProcessAttributes As Any, lpThreadAttributes As Any, _
ByVal bInheritHandles As Long, ByVal dwCreationFlags As Long, _
ByVal lpEnvironment As Long, ByVal lpCurrentDirectory As Long, _
lpStartupInfo As Any, lpProcessInformation As Any) As Long
Private Declare Function CloseHandle Lib "kernel32" (ByVal _
hObject As Long) As Long
Private Const NORMAL_PRIORITY_CLASS = &H20&
Private Const STARTF_USESTDHANDLES = &H100&
Private Function ExecCmd(cmdline$) As String
On Error GoTo errortrap
Dim proc As PROCESS_INFORMATION, ret As Long, bSuccess As Long
Dim start As STARTUPINFO
Dim sa As SECURITY_ATTRIBUTES, hReadPipe As Long, hWritePipe _
As Long
Dim bytesread As Long, mybuff As String
Dim i As Integer
mybuff = String(32000, Chr$(0))
sa.nLength = Len(sa)
sa.bInheritHandle = 1&
sa.lpSecurityDescriptor = 0&
ret = CreatePipe(hReadPipe, hWritePipe, sa, 0)
If ret = 0 Then
MsgBox "CreatePipe failed. Error: " & Err.LastDllError
Exit Function
End If
start.cb = Len(start)
start.dwFlags = STARTF_USESTDHANDLES
start.hStdOutput = hWritePipe
' Start the shelled application:
ret& = CreateProcessA(0&, cmdline$, sa, sa, 1&, _
NORMAL_PRIORITY_CLASS, 0&, 0&, start, proc)
If ret <> 1 Then
MsgBox "CreateProcess failed. Error: " & Err.LastDllError
End If
bSuccess = ReadFile(hReadPipe, mybuff, 32200, bytesread, 0&)
If bSuccess = 1 Then
ExecCmd = Left$(mybuff, bytesread)
Else
MsgBox "ReadFile failed. Error: " & Err.LastDllError
End If
ret& = CloseHandle(proc.hProcess)
ret& = CloseHandle(proc.hThread)
ret& = CloseHandle(hReadPipe)
ret& = CloseHandle(hWritePipe)
Exit Function
errortrap:
ret& = CloseHandle(proc.hProcess)
ret& = CloseHandle(proc.hThread)
ret& = CloseHandle(hReadPipe)
ret& = CloseHandle(hWritePipe)
End Function
Private Sub Form_Load()
Text1.Text = ExecCmd("c:\test.bat")
End Sub
Private Declare Function CreatePipe Lib "kernel32" ( _
phReadPipe As Long, _
phWritePipe As Long, _
lpPipeAttributes As Any, _
ByVal nSize As Long) As Long
Private Declare Function ReadFile Lib "kernel32" ( _
ByVal hFile As Long, _
ByVal lpBuffer As String, _
ByVal nNumberOfBytesToRead As Long, _
lpNumberOfBytesRead As Long, _
ByVal lpOverlapped As Any) As Long
Private Type SECURITY_ATTRIBUTES
nLength As Long
lpSecurityDescriptor As Long
bInheritHandle As Long
End Type
Private Type STARTUPINFO
cb As Long
lpReserved As Long
lpDesktop As Long
lpTitle As Long
dwX As Long
dwY As Long
dwXSize As Long
dwYSize As Long
dwXCountChars As Long
dwYCountChars As Long
dwFillAttribute As Long
dwFlags As Long
wShowWindow As Integer
cbReserved2 As Integer
lpReserved2 As Long
hStdInput As Long
hStdOutput As Long
hStdError As Long
End Type
Private Type PROCESS_INFORMATION
hProcess As Long
hThread As Long
dwProcessID As Long
dwThreadID As Long
End Type
Private Declare Function CreateProcessA Lib "kernel32" (ByVal _
lpApplicationName As Long, ByVal lpCommandLine As String, _
lpProcessAttributes As Any, lpThreadAttributes As Any, _
ByVal bInheritHandles As Long, ByVal dwCreationFlags As Long, _
ByVal lpEnvironment As Long, ByVal lpCurrentDirectory As Long, _
lpStartupInfo As Any, lpProcessInformation As Any) As Long
Private Declare Function CloseHandle Lib "kernel32" (ByVal _
hObject As Long) As Long
Private Const NORMAL_PRIORITY_CLASS = &H20&
Private Const STARTF_USESTDHANDLES = &H100&
Private Function ExecCmd(cmdline$) As String
On Error GoTo errortrap
Dim proc As PROCESS_INFORMATION, ret As Long, bSuccess As Long
Dim start As STARTUPINFO
Dim sa As SECURITY_ATTRIBUTES, hReadPipe As Long, hWritePipe _
As Long
Dim bytesread As Long, mybuff As String
Dim i As Integer
mybuff = String(32000, Chr$(0))
sa.nLength = Len(sa)
sa.bInheritHandle = 1&
sa.lpSecurityDescriptor = 0&
ret = CreatePipe(hReadPipe, hWritePipe, sa, 0)
If ret = 0 Then
MsgBox "CreatePipe failed. Error: " & Err.LastDllError
Exit Function
End If
start.cb = Len(start)
start.dwFlags = STARTF_USESTDHANDLES
start.hStdOutput = hWritePipe
' Start the shelled application:
ret& = CreateProcessA(0&, cmdline$, sa, sa, 1&, _
NORMAL_PRIORITY_CLASS, 0&, 0&, start, proc)
If ret <> 1 Then
MsgBox "CreateProcess failed. Error: " & Err.LastDllError
End If
bSuccess = ReadFile(hReadPipe, mybuff, 32200, bytesread, 0&)
If bSuccess = 1 Then
ExecCmd = Left$(mybuff, bytesread)
Else
MsgBox "ReadFile failed. Error: " & Err.LastDllError
End If
ret& = CloseHandle(proc.hProcess)
ret& = CloseHandle(proc.hThread)
ret& = CloseHandle(hReadPipe)
ret& = CloseHandle(hWritePipe)
Exit Function
errortrap:
ret& = CloseHandle(proc.hProcess)
ret& = CloseHandle(proc.hThread)
ret& = CloseHandle(hReadPipe)
ret& = CloseHandle(hWritePipe)
End Function
Private Sub Form_Load()
Text1.Text = ExecCmd("c:\test.bat")
End Sub
ASKER
hello richie
your solution works but only for files and not for DOS Commands I want It for dos commands such as DIR
(whatever is written in the Dos console i want it transferred to a text box created in Vb)
your solution works but only for files and not for DOS Commands I want It for dos commands such as DIR
(whatever is written in the Dos console i want it transferred to a text box created in Vb)
Well, we must redefine the entire routine since we need to keep open that dos window and work with it interactively.
It is a more complicated matter, i think.
I have something like this on my HD, let me search...
It is a more complicated matter, i think.
I have something like this on my HD, let me search...
gauravdhup:
You have many open questions:
https://www.experts-exchange.com/jsp/qShow.jsp?ta=visualbasic&qid=20132599
https://www.experts-exchange.com/jsp/qShow.jsp?ta=visualbasic&qid=20133812
https://www.experts-exchange.com/jsp/qShow.jsp?ta=visualbasic&qid=20249739
https://www.experts-exchange.com/jsp/qShow.jsp?ta=cplusprog&qid=20133818
https://www.experts-exchange.com/jsp/qShow.jsp?ta=gamesgen&qid=20234794
https://www.experts-exchange.com/jsp/qShow.jsp?ta=linux&qid=20177490
https://www.experts-exchange.com/jsp/qShow.jsp?ta=win2k&qid=20240804
To assist you in your cleanup, I'm providing the following guidelines:
1. Stay active in your questions and provide feedback whenever possible. Likewise, when feedback has not been provided by the experts, commenting again makes them receive an email notification, and they may provide you with further information. Experts have no other method of searching for questions in which they have commented, except manually.
2. Award points by hitting the Accept Comment As Answer button located above and to the left of that expert's comment.
3. When grading, be sure to read:
https://www.experts-exchange.com/jsp/cmtyQuestAnswer.jsp#3
to ensure that you understand the grading system here at EE. If you grade less than an A, you must explain why.
4. Questions that were not helpful to you should be PAQ'd (stored in the database for their valuable content?even if not valuable to you) or deleted. To PAQ or delete a question, you must first post your intent in that question to make the experts aware. Then, if no experts object after three full days, you can post a zero-point question at community support to request deletion or PAQ. Please include the link(s) to the question(s).
CS: https://www.experts-exchange.com/jsp/qList.jsp?ta=commspt
At that point, a moderator can refund your points and PAQ or delete the question for you. The delete button does not work.
5. If you fail to respond to this cleanup request, I must report you to the Community Support Administrator for further action.
Our intent is to get the questions cleaned up, and not to embarrass or shame anyone. If you have any questions or need further assistance at all, feel free to ask me in this question or post a zero-point question at CS. We are very happy to help you in this task!
thanks!
amp
community support moderator
You have many open questions:
https://www.experts-exchange.com/jsp/qShow.jsp?ta=visualbasic&qid=20132599
https://www.experts-exchange.com/jsp/qShow.jsp?ta=visualbasic&qid=20133812
https://www.experts-exchange.com/jsp/qShow.jsp?ta=visualbasic&qid=20249739
https://www.experts-exchange.com/jsp/qShow.jsp?ta=cplusprog&qid=20133818
https://www.experts-exchange.com/jsp/qShow.jsp?ta=gamesgen&qid=20234794
https://www.experts-exchange.com/jsp/qShow.jsp?ta=linux&qid=20177490
https://www.experts-exchange.com/jsp/qShow.jsp?ta=win2k&qid=20240804
To assist you in your cleanup, I'm providing the following guidelines:
1. Stay active in your questions and provide feedback whenever possible. Likewise, when feedback has not been provided by the experts, commenting again makes them receive an email notification, and they may provide you with further information. Experts have no other method of searching for questions in which they have commented, except manually.
2. Award points by hitting the Accept Comment As Answer button located above and to the left of that expert's comment.
3. When grading, be sure to read:
https://www.experts-exchange.com/jsp/cmtyQuestAnswer.jsp#3
to ensure that you understand the grading system here at EE. If you grade less than an A, you must explain why.
4. Questions that were not helpful to you should be PAQ'd (stored in the database for their valuable content?even if not valuable to you) or deleted. To PAQ or delete a question, you must first post your intent in that question to make the experts aware. Then, if no experts object after three full days, you can post a zero-point question at community support to request deletion or PAQ. Please include the link(s) to the question(s).
CS: https://www.experts-exchange.com/jsp/qList.jsp?ta=commspt
At that point, a moderator can refund your points and PAQ or delete the question for you. The delete button does not work.
5. If you fail to respond to this cleanup request, I must report you to the Community Support Administrator for further action.
Our intent is to get the questions cleaned up, and not to embarrass or shame anyone. If you have any questions or need further assistance at all, feel free to ask me in this question or post a zero-point question at CS. We are very happy to help you in this task!
thanks!
amp
community support moderator
Hi gauravdhup,
It appears that you have forgotten this question. I will ask Community Support to close it unless you finalize it within 7 days. I will suggest to:
Split points between: Richie_Simonetti and nunorodrigo
gauravdhup, if you think your question was not answered at all or if you need help, you can simply post a new comment here. Community Support moderators will follow up.
EXPERTS: If you disagree with that recommendation, please post an explanatory comment.
==========
DanRollins -- EE database cleanup volunteer
It appears that you have forgotten this question. I will ask Community Support to close it unless you finalize it within 7 days. I will suggest to:
Split points between: Richie_Simonetti and nunorodrigo
gauravdhup, if you think your question was not answered at all or if you need help, you can simply post a new comment here. Community Support moderators will follow up.
EXPERTS: If you disagree with that recommendation, please post an explanatory comment.
==========
DanRollins -- EE database cleanup volunteer
Per recommendation, force-accepted.
Netminder
CS Moderator
Richie_Simonetti: points for you at https://www.experts-exchange.com/jsp/qShow.jsp?ta=visualbasic&qid=20328617
Netminder
CS Moderator
Richie_Simonetti: points for you at https://www.experts-exchange.com/jsp/qShow.jsp?ta=visualbasic&qid=20328617
ASKER
'Executes the command, and when it finish returns value to VB
Dim proc As PROCESS_INFORMATION, ret As Long, bSuccess As Long
Dim start As STARTUPINFO
Dim sa As SECURITY_ATTRIBUTES
Dim hReadPipe As Long, hWritePipe As Long
Dim bytesread As Long, mybuff As String
Dim i As Integer
Dim sReturnStr As String
' the lenght of the string must be 10 * 1024
mybuff = String(10 * 1024, Chr$(65))
sa.nLength = Len(sa)
sa.bInheritHandle = 1&
sa.lpSecurityDescriptor = 0&
ret = CreatePipe(hReadPipe, hWritePipe, sa, 0)
If ret = 0 Then
'===Error
ExecCmdPipe = "Error: CreatePipe failed. " & Err.LastDllError
Exit Function
End If
start.cb = Len(start)
start.hStdOutput = hWritePipe
start.dwFlags = STARTF_USESTDHANDLES + STARTF_USESHOWWINDOW
start.wShowWindow = SW_SHOWMINNOACTIVE
' Start the shelled application:
ret& = CreateProcessA(0&, CmdLine$, sa, sa, 1&, _
NORMAL_PRIORITY_CLASS, 0&, 0&, start, proc)
If ret <> 1 Then
'===Error
sReturnStr = "Error: CreateProcess failed. " & Err.LastDllError
End If
' Wait for the shelled application to finish:
ret = WaitForSingleObject(proc.h
bSuccess = ReadFile(hReadPipe, mybuff, Len(mybuff), bytesread, 0&)
If bSuccess = 1 Then
sReturnStr = Left(mybuff, bytesread)
Else
'===Error
sReturnStr = "Error: ReadFile failed. " & Err.LastDllError
End If
ret = CloseHandle(proc.hProcess)
ret = CloseHandle(proc.hThread)
ret = CloseHandle(hReadPipe)
ret = CloseHandle(hWritePipe)
'returns to VB
ExecCmdPipe = sReturnStr
End Function