473,320 Members | 1,887 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Advanced: Shelling to DOS without using "Shell" command

Due to the way our IT group has set up our servers, we are not allowed
to use the Shell() command.

We are already doing a similar action with an FTP command. Our code
is thus:

sExe = Environ$("COMSPEC")
sExe = Left(sExe, Len(sExe) - Len(Dir(sExe)))
sExe2 = sExe & "ftp.exe -s:" & Q & sScrFile & Q

ShellWait sExe2, vbNormalFocus

In the above example, Q = """" (don't ask me why...) and sScrFile is
the name of a text file which contains a list of commands.

Once we do this, we need to move a file from one directory to
another. We can't use the DOS "Move" command while in the FTP shell,
so we have to open a new instance in the CMD shell.

My code is thus:

sExe3 = sExe & "cmd.exe -s:" & sScrFile2

ShellWait sExe3, vbNormalFocus

All it does is open a CMD window and sit there, it's not processing
the text file I'm sending it like the FTP shell does.

Anyone know why? Can you fix my code? Any help is appreciated.

Jun 15 '07 #1
3 4778
Usually when I need to move a file I use the kill and filecopy
commands.
So first make sure you have a local file to copy, then delete the
destination file, then copy the local file to the destination, then
verify the dest file, then delete the local file.
I hope this helps.

Here is a snippet from some of my code that does some of this:

Dim bCopyFile As Boolean
Dim sNewImagePath As String
bCopyFile = True
sNewImagePath = sDefaultImagePath & "\" & sFileName
If UncDir(sNewImagePath) <"" Then 'see if the file is
already there
bCopyFile = False
If MsgBox("The file " & sNewImagePath & " already
exists." & vbCrLf & _
"Would you like to overwrite it?", vbYesNo,
"Overwrite File") = vbYes Then
bCopyFile = DeleteFile(sNewImagePath)
End If
End If
If bCopyFile Then
FileCopy sImagePath, sNewImagePath
UpdateImage sNewImagePath
End If
There are two caveats.
1) the kill (or delete file) command will error if there is no file
present to delete
2) the dir command in vb doesn't seem to like network unc paths (\
\whatever\whereever) very much so I made my own
Function DeleteFile(ByVal strPathAndFile As String) As Boolean
'************************************************* **********************************
' Author Daniel Tweddell
' Revision Date 04/14/03
'
' Deletes a file
'************************************************* **********************************
On Error GoTo Err_Function
DeleteFile = True 'default to true
If UncDir(strPathAndFile) <"" Then 'make sure the file is
there
Kill strPathAndFile 'delete a file
End If
Exit Function
Err_Function:
ErrHandler Err.Number, Err.Description, "DeleteFile()", bSilent
DeleteFile = False 'if there is a problem, false
End Function
Function UncDir(ByVal strFilename As String) As String
'************************************************* ***************
' Author Daniel Tweddell
' Revision Date 12/13/03
'
' The Dir cmd Errors with unc network paths that do not exist
' so this seems to work.
'************************************************* ***************
On Error GoTo Err_Function
Dim FileData As WIN32_FIND_DATA, apiFileHandle As Long
Dim lngLastError As Long, FileDateTime As FILETIME, SystemDateTime
As SYSTEMTIME
apiFileHandle = FindFirstFile(strFilename, FileData)
If apiFileHandle <INVALID_HANDLE_VALUE Then UncDir = strFilename
Call FindClose(apiFileHandle)
Exit Function
Err_Function:
ErrHandler Err.Number, Err.Description, "UncDir()", bSilent
End Function

uncdir requires:
Private Type FILETIME ' 8 Bytes
dwLowDateTime As Long
dwHighDateTime As Long
End Type

Private Type SYSTEMTIME ' 16 Bytes
wYear As Integer
wMonth As Integer
wDayOfWeek As Integer
wDay As Integer
wHour As Integer
wMinute As Integer
wSecond As Integer
wMilliseconds As Integer
End Type

Private Type WIN32_FIND_DATA ' 318 Bytes
dwFileAttributes As Long
ftCreationTime As FILETIME
ftLastAccessTime As FILETIME
ftLastWriteTime As FILETIME
nFileSizeHigh As Long
nFileSizeLow As Long
dwReservedØ As Long
dwReserved1 As Long
cFileName As String * 260
cAlternate As String * 14
End Type

Declare Function FindFirstFile& Lib "kernel32" Alias
"FindFirstFileA" (ByVal _
lpFileName As String, lpFindFileData As WIN32_FIND_DATA)

Declare Function FindClose& Lib "kernel32" (ByVal hFindFile As Long)

Private Const ERROR_NO_MORE_FILES = 18&
Private Const INVALID_HANDLE_VALUE = -1

Jun 15 '07 #2
ManningFan wrote:
My code is thus:

sExe3 = sExe & "cmd.exe -s:" & sScrFile2

ShellWait sExe3, vbNormalFocus

All it does is open a CMD window and sit there, it's not processing
the text file I'm sending it like the FTP shell does.

Anyone know why? Can you fix my code? Any help is appreciated.
I think that this line:

sExe3 = sExe & "cmd.exe -s:" & sScrFile2

should be using sExe2 instead?

sExe3 = sExe2 & "cmd.exe -s:" & sScrFile2

--
'--------------------------
' John Mishefske
' UtterAccess Editor
' 2007 Microsoft Access MVP
'--------------------------
Jun 15 '07 #3
John Mishefske wrote:
ManningFan wrote:
>My code is thus:

sExe3 = sExe & "cmd.exe -s:" & sScrFile2

ShellWait sExe3, vbNormalFocus

All it does is open a CMD window and sit there, it's not processing
the text file I'm sending it like the FTP shell does.

Anyone know why? Can you fix my code? Any help is appreciated.

I think that this line:

sExe3 = sExe & "cmd.exe -s:" & sScrFile2

should be using sExe2 instead?

sExe3 = sExe2 & "cmd.exe -s:" & sScrFile2

I should have read your post more carefully but your move command is
probably expanding to something like this:
(assuming) ComSpec=C:\WINDOWS\system32\cmd.exe and "test.txt" as sScrFile2

The code line:

sExe3 = sExe & "cmd.exe -s:" & sScrFile2

expands to:

cmd.execmd.exe -s: filename

So... you can see there are several issues here:
- cmd.exe provided twice
- there is no -s: would be an invalid cmd option

You might want to try:
sExe & " move " & sScrFile2 & " " & sDestination

or better yet, use the Name statement in VBA to move your file.
--
'--------------------------
' John Mishefske
' UtterAccess Editor
' 2007 Microsoft Access MVP
'--------------------------
Jun 15 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Siemel Naran | last post by:
Hi. I'm writing a command shell that reads commands from standard input. At this point I have the command in a std::string. Now I want to execute this command in the shell. From the Borland...
2
by: senthil | last post by:
Hi My name is Senthil . I am trying to run bat file from Asp.net using shell command but it failed . The bat file is in server . The purpose of running this bat file is to do an ftp from server to...
3
by: RJN | last post by:
Hi I'm trying to create a windows task through code. I could do this through the shell command and the WMIService in .net exe. But the same doesn't work from asp.net. Any idea? This is the code...
9
by: Amjad | last post by:
I'm trying to create a folder, append text file to another, and delete files using the "Shell" command. But I always get the error "File Not Found". How do I use DOS commands from Visual Basic?...
2
by: ¹é¿ø¼® | last post by:
Hello, everybody. I want to call any shell command from php script using shell_exec(). The problem is that the next line of the php script would not be run until a few minutes after running the...
2
by: Hexman | last post by:
Hello All, I receive many files with similar content each day. All I want to do is copy them into one file, then I'll use that file for my processes. Using vb2005. I've tried: Shell("copy...
0
by: amar1223 | last post by:
hi all, I am stuck with a problem. it would be highly appreciated if anyone could help. i am executing a java program from my vb program using shell command. the java program outputs some text...
0
by: Chris | last post by:
Are there any arguments against using SHELL command for opening Access from VB6 ? I will define full path of access, system.mdw path, user name and password and database path to be opened. I...
2
by: mayo | last post by:
I had a procedure that calls some executable files. When I call .exe files compiled in VB, there's no error but when I try to call the help file which is created in Winhelp 2000, i got an error...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.