Dos commands in Access vba

alexlds

Registered User.
Local time
Today, 21:42
Joined
May 28, 2012
Messages
71
Several years ago I was quite familiar with DOS commands and was able to create access DBs and write the VBA button code. However that was quite a few years ago and Ive forgotten it all.

I wonder if some kind person might help me and provide the button code that I need ? ? This is what Im trying to do

For every image file in DIR A or subdirectory
If that image file exists in DIR B, then delete the image in DIR B


Years ago I could have managed this without any great difficulty but since then Ive lost the plot . . .

Thanks !
 
Search for FileSystemObject in this forum for help, there are many posts on the subject.

When using FileSystemObject, you are actually using VBA to call VBScript code.

Start with:

Dim fso 'I don't declare the type, this may not be the best practice.

Set fso = VBA.CreateObject("Scripting.FileSystemObject")

I think your plan of action should be to retrieve a list of files in the directory A and dump that into an array. Then get the list of files in Directory B and run your comparisons. Use an if statement to determine if the file in Directory B should be deleted by comparing it to the list of files in Directory A


Get Folder File List:
http://www.access-programmers.co.uk/forums/showthread.php?t=233567&highlight=filesystemobject

Assuming you use fso to represent FileSystemObject this command will allow you to delete the file as needed.

fso.DeleteFile [filename]

You may need to include the path in the filename depending on whether you include it in the variable to resolve the names or not.

MS KB to help start you off:
http://support.microsoft.com/kb/186118

Hey Scripting Guy archives for deeper reference and examples:
http://blogs.technet.com/b/heyscriptingguy/archive/tags/scripting+guy_2100_/default.aspx
 
Last edited:
You can use the Scripting (tools, references, microsoft scripting runtime reference) to loop through the folder A. For each one there, evaluate if the filename contains (let's say) .jpg in it. If so, add it to an array, then delete all files in array.

Something like:

dim fso as scripting.filesystemobject
dim fsofile as scripting.file
dim x as long
dim fsofolder as scripting.folder
dim arrFiles() as String
set fso=new scripting.filesystemobject
set fsofolder=fso.getfolder("path to folder where files will be deleted")
for each fsofile in fsofolder.files
if dir("path to other folder\" & fsofile.name)<>"" and instr(1,lcase(fsofile.name),".jpg)>0 Then

Redim Preserve arrfiles(0 to x)
arrfiles(x)=fsofile.path
x=x+1
end if

next fsofile

for x=0 to ubound(arrFiles)-1
Kill arrFiles(x)
next x

Obviously, aircode untested - but it should be about 99% accurate.
Always declare AND TYPE all variables in VBA.
 
Many thanks to both of you - your help is very much appreciated. ! !
 

Users who are viewing this thread

Back
Top Bottom