Automatic Folder Creation

grovelli
4StarLounger
Posts: 528
Joined: 26 Jan 2010, 15:14

Automatic Folder Creation

Post by grovelli »

Suppose you have a folder on your hard drive c:\test, how can one automate creating a subfolder of the test folder at the start of every new year and giving it the year name? E.g. when 2016 chimes in, a new folder is automatically created c:\test\2016.

User avatar
viking33
PlatinumLounger
Posts: 5685
Joined: 24 Jan 2010, 19:16
Location: Cape Cod, Massachusetts,USA

Re: Automatic Folder Creation

Post by viking33 »

grovelli wrote:Suppose you have a folder on your hard drive c:\test, how can one automate creating a subfolder of the test folder at the start of every new year and giving it the year name? E.g. when 2016 chimes in, a new folder is automatically created c:\test\2016.
I don't know of an automatic way to accomplish this.
However, it doesn't seem like too much of a chore, once a year, to go into Explorer and scroll down to the Test folder and right click it to create a new folder for that year? :thumbup: :yawn:
Don't mean to be sarcastic but......
BOB
:massachusetts: :usa:
______________________________________

If I agreed with you we'd both be wrong.

User avatar
Rudi
gamma jay
Posts: 25455
Joined: 17 Mar 2010, 17:33
Location: Cape Town

Re: Automatic Folder Creation

Post by Rudi »

John might need to help fine tune the .bat file code, but as mentioned, you could have Windows Task Scheduler run a .bat file on a specific date (recurring) to create the new folder.

A batch file code something like this:

Code: Select all

@echo off
cd C:\Test
setlocal enableextensions
set name=%DATE:/=_%
mkdir %name%
echo %folder% created
This code puts the whole date in yyyy-mm-dd according to your regional settings.

To run the code, set a scheduled task following this guideline...
Regards,
Rudi

If your absence does not affect them, your presence didn't matter.

User avatar
HansV
Administrator
Posts: 78545
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Automatic Folder Creation

Post by HansV »

How much work is it to create a folder once a year?

It should be possible, however to write a batch or VBScript file that checks whether a folder corresponding to the current year exists, and if not, create it.
Create a scheduled job in Windows to run that file once a year, on the 1st of January.

Here is the content of a VBScript file that you could use:

p = "C:\Test\" & Year(Date)
Set objFSO = CreateObject("Scripting.FileSystemObject")
If objFSO.FolderExists(p) = False Then objFSO.CreateFolder p
Set objFSO = Nothing

BATcher may tell you what a batch file for this purpose could look like.
Best wishes,
Hans

grovelli
4StarLounger
Posts: 528
Joined: 26 Jan 2010, 15:14

Re: Automatic Folder Creation

Post by grovelli »

I had the same reaction as yourself when I was asked whether it was possible but then it was pointed out to me you might not rely on people being around on the first of January to create such folder for whatever reason :bananas: :groovin: (or just simply forget to)and since that folder is supposed to host mail messages, it could happen that when messages start to get sent on the first(or second, or third) of January, there's no proper folder to collect them.

User avatar
StuartR
Administrator
Posts: 12615
Joined: 16 Jan 2010, 15:49
Location: London, Europe

Re: Automatic Folder Creation

Post by StuartR »

Ideally you should put code to check whether the folder exists and create it if necessary into the existing code that moves mail messages to that folder. That way you can be sure that the folder exists when you need it.
StuartR


grovelli
4StarLounger
Posts: 528
Joined: 26 Jan 2010, 15:14

Re: Automatic Folder Creation

Post by grovelli »

Good idea Stuart, I'm going to try Hans' VBScript since it's possible to reference Microsoft Scripting Runtime from within Outlook.

grovelli
4StarLounger
Posts: 528
Joined: 26 Jan 2010, 15:14

Re: Automatic Folder Creation

Post by grovelli »

And as a matter of fact it works :cheers:

Code: Select all

Option Explicit

Public WithEvents oSentItems As Items

Private Sub Application_Startup()
    Dim oSentItemsFolder As MAPIFolder
    Set oSentItemsFolder = Application.Session.GetDefaultFolder(olFolderSentMail)
    Set oSentItems = oSentItemsFolder.Items
End Sub

Private Sub oSentItems_ItemAdd(ByVal item As Object)
    Dim oRecipient As Recipient, p As String, objFSO As Object
    Dim strSubject As String
    p = "C:\Test\" & Year(Date) & "\"
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    If objFSO.FolderExists(p) = False Then objFSO.CreateFolder p
    Set objFSO = Nothing
    If item.Class = olMail Then
        For Each oRecipient In item.Recipients
            If InStr(1, oRecipient.Address, "ROVELLI", vbTextCompare) Then
                strSubject = CorrectName(item.Subject)
                item.SaveAs p & strSubject & "_" & _
                    Format(Now, "yyyymmddhhnnss") & ".msg", olMSGUnicode
                Exit For
            End If
        Next oRecipient
    End If
End Sub

Function CorrectName(ByVal s As String) As String
    Dim strName As String
    Dim varInvalids As Variant
    Dim i As Long
    strName = s
    varInvalids = Array("?", "*", "|", "<", ">", "\", ":", "/", """")
    For i = LBound(varInvalids) To UBound(varInvalids)
        strName = Replace(strName, varInvalids(i), "_")
    Next i
    CorrectName = strName
End Function

User avatar
HansV
Administrator
Posts: 78545
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Automatic Folder Creation

Post by HansV »

Great - thanks for the feedback.
Best wishes,
Hans

Max
2StarLounger
Posts: 113
Joined: 23 Mar 2015, 22:28

Re: Automatic Folder Creation

Post by Max »

grovelli wrote:Suppose you have a folder on your hard drive c:\test, how can one automate creating a subfolder of the test folder at the start of every new year and giving it the year name? E.g. when 2016 chimes in, a new folder is automatically created c:\test\2016.
This is what I do within Access. You could adapt this if you wished. It doesn't matter how many times it is called.

Code: Select all

Public Function pfCreateFolder(sFolder As String) As Boolean
' I use this call from my login code.
' If an error occurs when the specified folder already exists, it simple ignores it.
'Call pfCreateFolder(strStem & "\Exported")

' If I wanted folders and sub-folders I would do something like this to call it twice, first o create the
' first folder and then again for the sub-folder to that.
' Call pfCreateFolder(strStem & "\ObjectsBackup\Export")
' Call pfCreateFolder(strStem & "\ObjectsBackup\Export\Data")

' The 'strStem' is simple a global variable which holds a system path to where the Front-End is located.

    Dim fs
    On Error GoTo errhandler
    Set fs = CreateObject("Scripting.FileSystemObject")    '' requires a reference to Microsoft Scripting Runtime")
    fs.CreateFolder sFolder
    pfCreateFolder = True
exithere:
    Exit Function
errhandler:
    pfCreateFolder = False
    Select Case Err.Number
    Case 58  ' Folder already exists
        pfCreateFolder = True
        GoTo exithere
    Case 76  '
        MsgBox "path not found, create intermediate folders (" & sFolder & ")"
        GoTo exithere
    Case Else
        Dim lngErr As Long, strErr As String    ' Max Sherman - Dec 2010
        lngErr = Err.Number: strErr = Err.Description
        Call pfNotifyDeveloper(True, lngErr, strErr, "Function", "_modInit", "pfCreateFolder")
        MsgBox "Error " & lngErr & " (" & strErr & ") in procedure pfCreateFolder of Module _modInit"
        Resume exithere
    End Select
    GoTo exithere
End Function
Max
Max

User avatar
HansV
Administrator
Posts: 78545
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Automatic Folder Creation

Post by HansV »

Welcome to Eileen's Lounge, and thanks!
Best wishes,
Hans