473,606 Members | 2,200 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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$("COMSP EC")
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 4802
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 = sDefaultImagePa th & "\" & sFileName
If UncDir(sNewImag ePath) <"" 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(sNew ImagePath)
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\where ever) very much so I made my own
Function DeleteFile(ByVa l 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(strPathA ndFile) <"" 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(s trFilename, FileData)
If apiFileHandle <INVALID_HANDLE _VALUE Then UncDir = strFilename
Call FindClose(apiFi leHandle)
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
dwFileAttribute s As Long
ftCreationTime As FILETIME
ftLastAccessTim e 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_F ILES = 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:\WIND OWS\system32\cm d.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
1333
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 newsgroups I learned that there is a function in stdlib.h called system. int system(const char *command); First question, is the system command ANSI compliant. Because I include <cstdlib> and write std::system(command.c_str()); it looks like an...
2
1498
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 mainframe . It runs good in vb.net . When I try to run or open this batch file from server it does the ftp but not from Asp.net application. I willl be thankful to people if my problem is solved. I will be happy to know if there is an other...
3
3057
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 --WMI strComputer = "." objWMIService = GetObject("winmgmts:" _ & "{impersonationLevel=impersonate}!\\" _
9
9318
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? My code is something like: Dim folderPath as String folderPath = "C:\MyFolder"
2
6940
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 shell command. I found the shell command couldn't be run at background mode. It seemed that the shell_exec() function waits a return from the command. Because the command doesn't return, the shell_exec() waits until timeout reached. Using the '&'...
2
4960
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 c:\CND\dr*.txt C:\CND\TestC.txx") That comes back with "File not found". There are files there! Tried:
0
1431
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 to the standard output. how can i get my vb program to read this out put?? can anyone help.
0
1276
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 was not able to find another way for defining all these parameters together. Thanks a lot in advance.
2
3330
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 message and it wont open. My code is this: Shell (strPath & "\Help\MANUAL.HLP"),vbNormalFocus error message is:"Invalid procedure call." What is in Winhelp file? can anybody have some solutions for this? thanks..
0
7951
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8439
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8430
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8094
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
5966
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5465
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
2448
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1553
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1296
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.