Issuing DOS commands from VBA

J

Jonathan Blitz

How can I issue a DOS command from VBA.

I need to copy files from one directory to another.

--
Jonathan Blitz
AnyKey Limited
Israel

"When things seem bad
Don't worry and shout
Just count up the times
Things have worked themselves out."
 
A

Albert D. Kallal

You can use the Shell command. Check it out in the help.

However, ms-access has a fileCopy command built in. So, you don't need the
shell.


FileCopy fromfile, DestFile
 
B

Billy Yao [MSFT]

Hello Jonathan,

Thank you for posting in the community. My name is Billy and it's my pleasure to assist you
with this issue.

From your description, I understand that you would like to know how to issue the DOS
command from VBA to copy files. You can use the following two methods (Albert
mentioned) to copy a file in VBA. Here I provided you a detailed sample for your reference:


''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Copy with the SHELL function
' to issue the DOS command

Dim MyCommand As String
Dim TaskId As Integer
' Create command string to copy the file to another directory.
MyCommand = "CMD /C Copy c:\filecopy1.txt d:\filecopy2.txt"
TaskId = Shell(MyCommand, 1)
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''


''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Copy with the FileCopy function
' to copy the file directly

Dim SourceFile, DestinationFile
SourceFile = "c:\filecopy1.txt" ' Define source file name.
DestinationFile = "d:\filecopy2.txt" ' Define target file name.
FileCopy SourceFile, DestinationFile ' Copy source to target.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

For more information on how to use the Shell() function in Access Basic to run intrinsic MS-
DOS commands, please see the following KB:

116384 ACC: Using the Shell() Function to Run MS-DOS Commands
http://support.microsoft.com/?id=116384

Jonathan, does this answer your question? Please feel free to post in the group if this solves
your problem or if you would like further assistance. Thanks for posting in the community.

Best regards,

Billy Yao
Microsoft Online Support
 
J

Jonathan Blitz

Many thanks.

I did it using the Shell.
Should I change it?

--
Jonathan Blitz
AnyKey Limited
Israel

"When things seem bad
Don't worry and shout
Just count up the times
Things have worked themselves out."
 
B

Billy Yao [MSFT]

Hi Jonathan,

Thank you for your update. It all depends on your needs. If you would only copy files from
one directory to another, both are ok. If you'd like to issue more DOS commands from VBA, I
believe Shell function is a better choice.

Regards,

Billy Yao
Microsoft Online Support
 
J

Jonathan Blitz

Many thanks.

--
Jonathan Blitz
AnyKey Limited
Israel

"When things seem bad
Don't worry and shout
Just count up the times
Things have worked themselves out."
 
Joined
Sep 21, 2014
Messages
1
Reaction score
0
How is it possible to modify this code to rename the file before sending it to the new location?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top