Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: VB.NET VBA
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?
 
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 6-Dec-12 11:04am
Edited 10-Apr-13 6:38am
Maciej Los158.3K
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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.
  Permalink  
Comments
Scott Leedy at 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 at 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 at 6-Dec-12 16:49pm
   
Sorry, it should be .vbs not .vba that's my bad. stupid typo.
Adam R Harris at 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
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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.
  Permalink  

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

  Print Answers RSS
0 OriginalGriff 277
1 Maciej Los 210
2 BillWoodruff 205
3 DamithSL 165
4 Tomas Takac 144
0 OriginalGriff 5,130
1 DamithSL 4,157
2 Maciej Los 3,670
3 Kornfeld Eliyahu Peter 3,470
4 Sergey Alexandrovich Kryukov 2,821


Advertise | Privacy | Mobile
Web01 | 2.8.141216.1 | Last Updated 11 Apr 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100