Copy/Rename Files from within Access

C

Clint Marshall

I am looking for a way in Access 2000 to click a Command Button that will
copy and rename several files elsewhere in the same directory that the
Access database resides in. This could be done by having Access directly
rename and copy the files or by having Access run a .bat file that does the
work for us.

How do I do this? I am reasonably comfortable with Access Macros and VB
programming.


Perhaps another way of looking at this question is to think about what I'm
trying to achieve...

The end result I need is to change the icon for the directory in which the
file resides. We use the directory icon and icon color to track what
actions are needed for each directory (red icon needs immediate attention,
orange needs attention soon, green means the ball is in someone else's
court). I currently do this by using a set of batch files to copy in
different icons depending on the color needed. It works, but is primitive
and we'd like control from within Access.

Thanks!

-Clint Marshall
 
A

Andi Mayer

I am looking for a way in Access 2000 to click a Command Button that will
copy and rename several files elsewhere in the same directory that the
Access database resides in. This could be done by having Access directly
rename and copy the files or by having Access run a .bat file that does the
work for us.

How do I do this? I am reasonably comfortable with Access Macros and VB
programming.

you mean this functions?

currentproject.path
name "oldfile" as "newfile"
filecopy "oldfile", "newfile"
kill "oldfle"
 
T

Troy

Yes. Use the Name Statement.

Renames a disk file, directory, or folder.

Dim OldName As Variant
Dim NewName As Variant

OldName = "C:\Test.txt"
NewName = "C:\YourNewName.txt"
Name OldName As NewName

From the Access Help file:
Remarks

The Name statement renames a file and moves it to a different directory or
folder, if necessary. Name can move a file across drives, but it can only
rename an existing directory or folder when both newpathname and oldpathname
are located on the same drive. Name cannot create a new file, directory, or
folder.

Using Name on an open file produces an error. You must close an open file
before renaming it. Name arguments cannot include multiple-character (*) and
single-character (?) wildcards.

HTH


--
Troy

Troy Munford
Development Operations Manager
FMS, Inc.
www.fmsinc.com


I am looking for a way in Access 2000 to click a Command Button that will
copy and rename several files elsewhere in the same directory that the
Access database resides in. This could be done by having Access directly
rename and copy the files or by having Access run a .bat file that does the
work for us.

How do I do this? I am reasonably comfortable with Access Macros and VB
programming.


Perhaps another way of looking at this question is to think about what I'm
trying to achieve...

The end result I need is to change the icon for the directory in which the
file resides. We use the directory icon and icon color to track what
actions are needed for each directory (red icon needs immediate attention,
orange needs attention soon, green means the ball is in someone else's
court). I currently do this by using a set of batch files to copy in
different icons depending on the color needed. It works, but is primitive
and we'd like control from within Access.

Thanks!

-Clint Marshall
 
C

Clint Marshall

Great! Thanks!

I'm having to unhide (turn off the "hidden" attribute) the files I'm working
with so that I can Kill and copy them. Is there any way to use Access to
either change the attributes or to run a batch file that changes the
attributes?
 
C

Clint Marshall

Great clear answer! Thanks!

I'm having to unhide (turn off the "hidden" attribute) the files I'm working
with so that I can Kill and copy them. Is there any way to use Access to
either change the attributes or to run a batch file that changes the
attributes?
 
T

Troy

Try the SetAttr() function before the Name statement is executed.

--
Troy

Troy Munford
Development Operations Manager
FMS, Inc.
www.fmsinc.com


Great clear answer! Thanks!

I'm having to unhide (turn off the "hidden" attribute) the files I'm working
with so that I can Kill and copy them. Is there any way to use Access to
either change the attributes or to run a batch file that changes the
attributes?
 
C

Clint Marshall

Troy -
You're awesome! That works beautifully!
One last question: When the file that I'm changing the attribute of ,
and deleting (desktop.ini) is missing, I get a "file not found" error and
the code stops. In this case, how can get the program to skip that step and
pick up at the FileCopy step?

Here's the section of my code. When the desktop.ini file is missing, I
get a "file not found" error and the code stops (either at SetAttr or at
Kill, not sure which).

FoundSlash_cmdRed_Click:
stUpOnePath = Left(stFullPath, n - 1)
stIconDirectory = stUpOnePath & "\icons"
stNewIniFile = stIconDirectory & "\red.ini"
stCurrentIniFile = stFullPath & "\desktop.ini"
SetAttr stCurrentIniFile, vbNormal
Kill stCurrentIniFile
FileCopy stNewIniFile, stCurrentIniFile
SetAttr stCurrentIniFile, vbHidden
MsgBox "Icon color successfully changed to red", vbOKOnly, "Icon
changed"

Thanks again!

-Clint
 
T

Troy

Prior to doing any of the code that uses the file in any way, check for the
file existence first.

Dim fExists As Boolean
fExists = Len(Dir("C:\YourFileName.txt")) > 0

You *may* need to wrap that in On error resume next

On Error Resume Next
Dim fExists As Boolean
fExists = Len(Dir("C:\YourFileName.txt")) <> 0
On Error Goto 0 ' or Proc Err Handler you may have
If Err Then 'Something bad happened
fExists = False
Else
fExists = True
End If

--
Troy

Troy Munford
Development Operations Manager
FMS, Inc.
www.fmsinc.com


Troy -
You're awesome! That works beautifully!
One last question: When the file that I'm changing the attribute of ,
and deleting (desktop.ini) is missing, I get a "file not found" error and
the code stops. In this case, how can get the program to skip that step and
pick up at the FileCopy step?

Here's the section of my code. When the desktop.ini file is missing, I
get a "file not found" error and the code stops (either at SetAttr or at
Kill, not sure which).

FoundSlash_cmdRed_Click:
stUpOnePath = Left(stFullPath, n - 1)
stIconDirectory = stUpOnePath & "\icons"
stNewIniFile = stIconDirectory & "\red.ini"
stCurrentIniFile = stFullPath & "\desktop.ini"
SetAttr stCurrentIniFile, vbNormal
Kill stCurrentIniFile
FileCopy stNewIniFile, stCurrentIniFile
SetAttr stCurrentIniFile, vbHidden
MsgBox "Icon color successfully changed to red", vbOKOnly, "Icon
changed"

Thanks again!

-Clint
 
C

Clint Marshall

Awesome again, Troy!

fExists kept coming up false, even when the file was there, though the Dir
and Len functions were working correctly. Not sure what was wrong, but I
ended up falling back to:
If Dir(stCurrentIniFile, vbHidden) <> "" Then
SetAttr stCurrentIniFile, vbNormal
Kill stCurrentIniFile
End If

I'm a hero (at least in my own mind!), thanks to you!

-Clint
 

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