View Single Post
  #5   Report Post  
Posted to microsoft.public.excel.programming
Ivan F Moala[_21_] Ivan F Moala[_21_] is offline
external usenet poster
 
Posts: 1
Default Is it possible to run DOS commands in Excel?


Ah, yes you are correct :)

You can use the FileSystemObject

No need to shell out to Dos

'// You can either
'// 1) Set a reference to the [Microsoft Scripting Runtime]
FileSystemObject.CopyFile "c:\mydocuments\My.txt"
"c:\mydocuments\MyNew.txt"

'// OR 2) create the reference via Late Binding
Dim Fso As Object
Set Fso = CreateObject("Scripting.FileSystemObject")
Fso.CopyFile "c:\mydocuments\My.txt", "c:\mydocuments\MyNew.txt"


'// OR 3) Use just use Native VBA to copy
FileCopy "c:\mydocuments\My.txt", "c:\mydocuments\MyNew.txt"

If you really need to Shell out to Dos using core Dos commands such a
Copy then you need to use the c switch together
with the command.com eg
Shell "command.com /c MD C:\Amydir C:\Amydir", vbNormalFocus

If running it asynchronously is a problem then use the Window
Scripting Host object model.

A method exposed by this library lets you to run a file,
and specify whether you want to wait for its termination.
To use the following code snippet you must add a reference
to Windows Scripting Host Object model in the "References " dialo
window.
The method you're looking for is the Run method of the IWshShell_Clas
object.
Its first argument is the file to run, the second specifies the style
of the
application's window and the last is a Boolean value that tells whethe
you
want to wait for the program termination. Here's a function tha
contains all
the necessary code:

Sub RunAndWait(ByVal sFile As String)
Dim WSHShell As New IWshShell_Class
WSHShell.Run sFile, , True
End Sub

'To test this function, write the following code in your form
'(it assumes that Notepad is in your system path):

Sub Tester()
RunAndWait "notepad.exe"
MsgBox "Back to your application!"
End Sub



Rob van Gelder Wrote:[color=blue]
I thought OP was after Copy, not Rename.

--
Rob van Gelder - http://www.vangelder.co.nz/excel


"Ivan F Moala" wrote i
message
...

Hi hce

Look up help for [Name Statement]

From help

Name Statement Example
This example uses the Name statement to rename a file. For purpose

of
this example, assume that the directories or folders that ar

specified
already exist. On the Macintosh, “HD:” is the default drive name and
portions of the pathname are separated by colons instead of
backslashes.

Dim OldName, NewName
OldName = "OLDFILE": NewName = "NEWFILE" ' Define file names.
Name OldName As NewName ' Rename file.

OldName = "C:\MYDIR\OLDFILE": NewName = "C:\YOURDIR\NEWFILE"
Name OldName As NewName ' Move and rename file.



--
Ivan F Moal

-----------------------------------------------------------------------
Ivan F Moala's Profile: http://www.excelforum.com/member.php...nfo&userid=195
View this thread: http://www.excelforum.com/showthread.php?threadid=26838