Click here to Skip to main content
15,886,578 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am a newbie to coding.

I needed a macro to move a bunch of files based on their modified date. I pieced the code below together and made it work. However it resides as VBA code in an Excel file. I would like help in two areas. First, how exactly does this code work? I don't understand all I see, (e.g. dum_var1, dum_var2 and where does f2 come from)?

Secondly what would be the best way to convert this to a VB project? Make a one button application? Preferably, is there a way to make this launch automatically on the first of each month?

VB
Dim my_row, cur_path

Sub update_locations()
folderspec = "C:\test_start"     '"P:\QUALITY\3dGroup\Measurements"
dum_var = get_folders(folderspec)
MsgBox "Done."
End Sub


Function get_folders(in_folder)
full_ret_val = full_ret_val & get_files(in_folder)
Dim fs, f, f1, fc, s
Set fs = CreateObject("Scripting.FileSystemObject")
Set f = fs.GetFolder(in_folder)
dum_var1 = get_files(in_folder)
Set fc = f.SubFolders
For Each f2 In f
    DoEvents
    dum_var2 = get_folders(f2.Path)
Next
End Function

Function get_files(in_folder)
Dim fs, f, f1, fc, pathIn, pathOut, newPathStart, fileName, folderSave, NewName, s
Set fs = CreateObject("Scripting.FileSystemObject")
Set f = fs.GetFolder(in_folder)
Set fc = f.Files
For Each f1 In fc
    On Error Resume Next
    folderSave = Right(in_folder, Len(in_folder) - 13)
    
    If DateDiff("d", f1.datelastmodified, Now) > 5 Then

        newPathStart = "C:\test_end\"      ' "P:\QUALITY\3dGroup\ToBeArchived\"
        pathOut = newPathStart & folderSave
        'MsgBox pathOut
        On Error Resume Next
        MkDir pathOut
        'MsgBox f1
        fileName = f1.Name
        'MsgBox fileName
Posted
Updated 10-Apr-13 5:38am
v2

The path of least resistence to get this out of excel would be to copy and paste it into notepad then save its [something].vba. Then all you would need to do is just double click the vba file and your process would run. Outside of that you are going to have to pick a language, most likely VB.NET, and read up on Windows Forms and the System.IO namespace.

As for what the code is actually doing, that's a big long discussion with lots of links. If you really want to learn exactly what the script is doing, the meat and potatoes of the application is really the Scripting.FileSystemObject[^] which is what is allowing you to traverse the directory.
 
Share this answer
 
Comments
Scott Leedy 6-Dec-12 16:29pm    
Cool I like the idea of just making a "something.vba" file. Something I didn't ask in the first post; what code would be needed in the end to make is delete the folder if it ended up being empty (note in the final version I copy the current file to a new location and then "kill" it)? I didn't realize I posted an unfinished version.
Scott Leedy 6-Dec-12 16:44pm    
Hmm, I saved the code in a txt file and renamed it archive.vba, my system didn't know what software to use to run it?
Adam R Harris 6-Dec-12 16:49pm    
Sorry, it should be .vbs not .vba that's my bad. stupid typo.
Adam R Harris 6-Dec-12 16:47pm    
Yeah, i noticed you didn't post the full code.

If you want to delete the folder if the folder doesnt have any files in you should add something similar to this to your get_Folders function *before* you call get_Files, something like this

Function get_folders(in_folder)
full_ret_val = full_ret_val & get_files(in_folder)
Dim fs, f, f1, fc, s
Set fs = CreateObject("Scripting.FileSystemObject")
Set f = fs.GetFolder(in_folder)

' check if the folder, f, has any files
if f.Files.length = 0 then
' check if the folder has any sub folders, you dont want to delete a folder with only folders in it
if f.SubFolder.length = 0 then
' delete the folder because there are no files in it or sub folders
fs.DeleteFolder(f.Path)
end if
else
' continue processing as normal
dum_var1 = get_files(in_folder)
end if

Set fc = f.SubFolders
For Each f2 In f
DoEvents
dum_var2 = get_folders(f2.Path)
Next
End Function
Don't convert above code from VBA to VB.NET!

Based on this article: http://msdn.microsoft.com/en-us/library/dd997370.aspx[^] change the code to your needs to list all directories and files.

You can use List(of T)[^] generic class to store listed folders and files.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900