1.

Solve : DOS reverse redirection?

Answer»

all,

I need some help on how to write a command to the DOS window using the reverse redirection symbol ( < ).

I have searched the internet for hours on trying to get some information on how to do this, but the only i've found is how to redirect OUTPUT to a text file. I need the opposite. I want to redirect the contents of a text file to the command line so I can execute it. Any ideas? Thanks!It is not clear what you want. The revers direction is for input. A text file does not ask for input. A program asks for input
It is used all the time. It is for programs where you do not want interaction, instead the commands come from some kind of text file.

Do you need some very simple examples?
I can only so simple examples. I have a bird brain.
This will take the first line of file x.txt and store it as a variable named y.

Set /p y=
Then after that, you can manipulate it as much as you want. Geek,

Here is a better explanation:


for example, I have a text file on the c drive that has this path: c:\reDir.txtIn that file I have one line of text: dirI want to execute that command in a DOS command prompt using the redirection. The end result should be just like it were if I typed 'D I R' into the prompt myself and hit enter. Does that make sense?Quote from: ajetrumpet on December 06, 2009, 01:12:37 PM

Geek,

Here is a better explanation:


for example, I have a text file on the c drive that has this path: c:\reDir.txtIn that file I have one line of text: dirI want to execute that command in a DOS command prompt using the redirection. The end result should be just like it were if I typed 'D I R' into the prompt myself and hit enter. Does that make sense?
is that in real time? can I do that from the prompt by typing it in?that can be does as a batch file. Go to start > run (or window key + r) and type in notepad (or open notepad any other way ). For your example enter this code:
Code: [Select]dir

Save it at the location you want using the .bat extension (I believe you need to have extension visible to do this successfully). Double clicking on the batch file will execute the commands there are in the file. If you want to do it for the command prompt, just write the name of the batch file.

Hope this helped

Two-Eyes %

EDIT:
Quote
can I do that from the prompt by typing it in?
Yes you can.i can type this in:Code: [Select]type finished.txtand what that does it PRINT the contents of the txt in the DOS window, but how do I get it to EXECUTE that content as a command?RENAME it to a .bat or .cmd file and then simply use:

Code: [Select]finished.bat
all,

i have gotten it down, but I think the problem is still present. I have this code for shelling an ftp text file:Code: [Select]Dim fso
Dim oFile
'Dim varitem As Variant
Dim CurDir As String
Dim CurRemoteDir As String
Dim vPath As String
Dim vFile As String
Dim vFTPServ As String
Dim fNum As Long

Screen.MousePointer = 11

Set fso = CreateObject("Scripting.FileSystemObject")
Set oFile = fso.createtextfile("c:\inProgress.txt", True)
oFile.writeline "contents"
oFile.Close

vPath = "C:"
vFTPServ = Me.Combo1
CurDir = Me.Text26
CurRemoteDir = Me.Text1
vFile = Me.List27
fNum = FreeFile()

Open vPath & "\FtpComm.txt" For Output As #fNum
Print #1, "USER " & sFtpUserName ' your login
Print #1, sFtpPassword ' your password
'Print #1, "dir > c:\inProgress.txt"

If sFtpHostName <> Me.Combo1 Then
Print #1, "cd " & Left( _
Right(CurRemoteDir, Len(CurRemoteDir) - InStr(CurRemoteDir, "/")), _
(Len(CurRemoteDir) - 1))
End If

Print #1, "put " & """" & CurDir & vFile & """"
'Print #1, "!"
'Print #1, "del c:\inProgress.txt"
'Print #1, "exit"
Print #1, "close" ' close connection
Print #1, "quit" ' Quit ftp program
Close

Shell "ftp -n -i -g -s:" & vPath & "\FtpComm.txt " & vFTPServ, vbMaximizedFocus
what I am trying to accomplish is to get an indication of when the ftp session is over and the DOS window has been closed, thus telling me that all of the commands have been executed.

This is in VBA, so maybe a different forum would be the best now?
C:\batch>type reverse.bat

Quote
@echo off

REM Put 500 in money.txt file

if not exist money.txt echo 500 > money.txt

Rem Put 500 in money variable

set /p money=< money.txt

echo Money = %money%

Output:

C:\batch> reverse.bat
Money = 500

C:\batch>Quote from: ajetrumpet on December 06, 2009, 01:49:41 PM

This is in VBA, so maybe a different forum would be the best now?

well, no reason to move it, I don't think.

I encountered this problem some time ago, and already have something that should help.

I made this module, "modexec" quite recently:

Code: [Select]Option Explicit

Private Type SECURITY_ATTRIBUTES
nLength As Long
lpSecurityDescriptor As Long
bInheritHandle As Long
End Type

Private Type PROCESS_INFORMATION
hProcess As Long
hThread As Long
dwProcessId As Long
dwThreadId 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 Byte
hStdInput As Long
hStdOutput As Long
hStdError As Long
End Type

Private Type OVERLAPPED
internal As Long
internalHigh As Long
offset As Long
OffsetHigh As Long
hEvent As Long
End Type

Private Const STARTF_USESHOWWINDOW = &H1
Private Const STARTF_USESTDHANDLES = &H100
Private Const SW_HIDE = 0
Private Const SW_SHOWDEFAULT As Long = 10

Private Const EM_SETSEL = &HB1
Private Const EM_REPLACESEL = &HC2
Public Declare Function CreatePipe Lib "kernel32" (phReadPipe As Long, phWritePipe As Long, _
lpPipeAttributes As SECURITY_ATTRIBUTES, ByVal nSize As Long) As Long
Private Declare Sub GetStartupInfo Lib "kernel32" Alias "GetStartupInfoA" (lpStartupInfo As STARTUPINFO)
Private Declare Function CreateProcess Lib "kernel32" Alias "CreateProcessA" (ByVal lpApplicationName As String, _
ByVal lpCommandLine As String, lpProcessAttributes As Any, lpThreadAttributes As Any, _
ByVal bInheritHandles As Long, ByVal dwCreationFlags As Long, lpEnvironment As Any, _
ByVal lpCurrentDriectory As String, lpStartupInfo As _
STARTUPINFO, lpProcessInformation As PROCESS_INFORMATION) As Long
Private Declare Function SetWindowText Lib "user32" Alias "SetWindowTextA" (ByVal hwnd As Long, ByVal lpString As _
String) As Long
Private Declare Function ReadFile Lib "kernel32" (ByVal hFile As Long, lpBuffer As Any, ByVal nNumberOfBytesToRead As _
Long, lpNumberOfBytesRead As Long, lpOverlapped As Any) As Long
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal _
wParam As Long, lParam As Any) As Long
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
Private Declare Function GetThreadDesktop Lib "User32.dll" (ByVal dwThread As Long) As Long
Private Declare Function WriteFile Lib "kernel32.dll" (ByVal hFile As Long, ByRef lpBuffer As Any, _
ByVal nNumberOfBytesToWrite As Long, ByRef lpNumberOfBytesWritten As Long, ByRef lpOverlapped As Any) As Long

Private Declare Function WaitForSingleObject Lib "kernel32.dll" (ByVal hHandle As Long, ByVal _
dwMilliseconds As Long) As Long
Private Const WAIT_TIMEOUT As Long = 258&



Public Function Redirect(CMDLINE As String) As String
Dim i%, t$
Dim pa As SECURITY_ATTRIBUTES
Dim pra As SECURITY_ATTRIBUTES
Dim tra As SECURITY_ATTRIBUTES
Dim pi As PROCESS_INFORMATION
Dim sui As STARTUPINFO
Dim hRead As Long
Dim hWrite As Long
Dim bRead As Long
Dim hstdInPipeWrite As Long, hStdInPipeRead As Long
Dim lpBuffer As String, wholestr As String
pa.nLength = Len(pa)
pa.lpSecurityDescriptor = 0
pa.bInheritHandle = True

pra.nLength = Len(pra)
tra.nLength = Len(tra)
CreatePipe hStdInPipeRead, hstdInPipeWrite, pa, 0
WriteFile hstdInPipeWrite, ByVal Chr$(0), 1, 0, ByVal 0
If CreatePipe(hRead, hWrite, pa, 0) <> 0 Then
sui.cb = Len(sui)
GetStartupInfo sui
sui.hStdInput = hStdInPipeRead
sui.hStdOutput = hWrite
sui.hStdError = hWrite
sui.dwflags = STARTF_USESHOWWINDOW Or STARTF_USESTDHANDLES
sui.wShowWindow = SW_HIDE
If CreateProcess(vbNullString, cmdLine, pra, tra, 1, 0, ByVal 0&, vbNullString, sui, pi) <> 0 Then
'SetWindowText objTarget.hwnd, ""
'If Not GetThreadDesktop(pi.hThread) = 0 Then
'insert waitforsingleobject loop here...
Dim rtret As Long
Do
rtret = WaitForSingleObject(pi.hProcess, 100)
If rtret <> WAIT_TIMEOUT Then
Exit Do
End If
DoEvents

Loop
Do
lpBuffer = Space(1024)
If ReadFile(hRead, ByVal lpBuffer, 1023, bRead, ByVal 0&) Then
' SendMessage objTarget.hwnd, EM_SETSEL, -1, 0
' SendMessage objTarget.hwnd, EM_REPLACESEL, False, lpBuffer(0)
wholestr = wholestr & Replace$(Trim$(lpBuffer), vbNullChar, "")
If bRead < 1023 Then
Exit Do
End If
DoEvents
Else
Exit Do
End If

Loop
' Else
' wholestr = "ERROR executing """ & cmdLine & """."
' End If
CloseHandle hWrite
CloseHandle hRead
CloseHandle hStdInPipeRead
CloseHandle hstdInPipeWrite
End If
End If
Redirect = wholestr
End Function

Public Sub ExecWait(cmdLine As String, Optional ByVal WindowShowMode As Long = 10)
Dim i%, t$
Dim pa As SECURITY_ATTRIBUTES
Dim pra As SECURITY_ATTRIBUTES
Dim tra As SECURITY_ATTRIBUTES
Dim pi As PROCESS_INFORMATION
Dim sui As STARTUPINFO
Dim hRead As Long
Dim hWrite As Long
Dim bRead As Long

Dim lpBuffer As String, wholestr As String



sui.cb = Len(sui)
GetStartupInfo sui
sui.dwflags = STARTF_USESHOWWINDOW Or STARTF_USESTDHANDLES
sui.wShowWindow = WindowShowMode
If CreateProcess(vbNullString, cmdLine, pra, tra, 1, 0, ByVal 0&, vbNullString, sui, pi) <> 0 Then
'SetWindowText objTarget.hwnd, ""
'If Not GetThreadDesktop(pi.hThread) = 0 Then
'insert waitforsingleobject loop here...
Dim rtret As Long
Do
rtret = WaitForSingleObject(pi.hProcess, 100)
If rtret <> WAIT_TIMEOUT Then
Exit Do
End If
DoEvents

Loop

End If

End Sub




you should be able to paste it into a new module in your VBA project, and then use the "execwait" function. For example:

Code: [Select]Execwait "notepad C:\newfile.txt",10

the second argument is the "show" mode; you can find these in the MSDN library, things like SW_HIDE, SW_SHOWDEFAULT (which I used here) SW_MAXIMIZED, etc.

the "exec" function is kind of buggy; the original purpose was to redirect all output from the command line program to a pipe and then read that pipe into a string variable... it doesn't seem to work really, I have to terminate the program manually before it works. Oh well. (Execwait seems to work fine, that's the function you need for this purpose).

EDIT: added line continuation characters.BC,

that worked a treat. thank you so much. what a job you did on that module. that's incredible. =)Quote from: ajetrumpet on December 06, 2009, 04:19:55 PM
BC,

that worked a treat. thank you so much. what a job you did on that module. that's incredible. =)

your very welcome, always nice to share some of my code. Sometimes I'll read about something, waiting for a process to finish, sorting algorithms, etc- and just feel compelled to write a module, class module, or a group of modules/class modules to do the job; however far I get working on it, it stays in my module folders, in case I need the functionality in a program later


Discussion

No Comment Found