Solved Folder Creation (1 Viewer)

Cliff67

Registered User.
Local time
Yesterday, 20:56
Joined
Oct 16, 2018
Messages
175
Hi All

So not really related to Access but I thought It would be quicker to do this programmatically then by long hand

So what I'm doing is kind or related to a Database. We have board that we test and results are recorded in excel spreadsheets like shown in the Excel results image.

What I need to do now is basically for every result make a folder based on the name of the spread sheet so S-M0471_007.xlsx is now a folder called S-M0470_007

see the Folder Names image.

If I start from a form with windows dialog boxes that that select the file location to change and another one that selects the desired location,

has anyone got any idea how to do this? The excel sheets will be moved across into the folders once created

Thanks in advance

Cliff
 

Attachments

  • Excel results.JPG
    Excel results.JPG
    44.8 KB · Views: 259
  • Folder Names.JPG
    Folder Names.JPG
    36.4 KB · Views: 266

theDBguy

I’m here to help
Staff member
Local time
Yesterday, 20:56
Joined
Oct 29, 2018
Messages
21,467
Have you looked into the MKDIR statement?
 

Cliff67

Registered User.
Local time
Yesterday, 20:56
Joined
Oct 16, 2018
Messages
175
Hi BDG

no I haven't. I just wonder how to parse the names of the files to create the folders
 

bastanu

AWF VIP
Local time
Yesterday, 20:56
Joined
Apr 13, 2010
Messages
1,402

Pat Hartman

Super Moderator
Staff member
Local time
Yesterday, 23:56
Joined
Feb 19, 2002
Messages
43,263
You can use InStrR() to find the last instance of a character

NameLen = InstrR(Me.SomeFileName, ".") - 1
NewDirName = Left(Me.SomeFileName, NameLen)
 

moke123

AWF VIP
Local time
Yesterday, 23:56
Joined
Jan 11, 2013
Messages
3,916
FileSystemObject has a few methods that would make this easy.

you could try something along the lines of

Code:
Sub YourSubName(SourceFile As String, DestinationFolder As String)
'DestinationFolder is the parent folder to put folder being created

    Dim fso As New FileSystemObject

    Dim FolderName As String
    Dim DestinationFile As String

    FolderName = Replace(fso.GetFileName(SourceFile), "." & fso.GetExtensionName(SourceFile), "") ' remove extension from filename

    DestinationFolder = DestinationFolder & "\" & FolderName   'construct full path of new folder
    DestinationFile = DestinationFolder & "\" & fso.GetFileName(SourceFile) 'construct full path of file in new folder

    If fso.FolderExists(DestinationFolder) = False Then
        fso.CreateFolder DestinationFolder          'create the new folder
    End If

    fso.CopyFile SourceFile, DestinationFile      'copy file to new folder
    'fso.MoveFile SourceFile, DestinationFile     'move file to new folder

End Sub

edit: Dont forget to set a reference to Microsoft Scripting Runtime
 
Last edited:

Cliff67

Registered User.
Local time
Yesterday, 20:56
Joined
Oct 16, 2018
Messages
175
Also you might want to read about the FSO object (https://docs.microsoft.com/en-us/of...e/user-interface-help/filesystemobject-object). Are there any subfolders in the original spreadsheet repository? If yes you would need to loop recursively, get the spreadsheet name, create the new folder (sFolderName=Replace(FSOFile.Name,"xlsx","")), move the spreadsheet into it and move next.

Cheers,
there are no subfolders. It is structure as Name of PCB, then the serial number of the PCB that has been tested
 

Cliff67

Registered User.
Local time
Yesterday, 20:56
Joined
Oct 16, 2018
Messages
175
FileSystemObject has a few methods that would make this easy.

you could try something along the lines of

Code:
Sub YourSubName(SourceFile As String, DestinationFolder As String)
'DestinationFolder is the parent folder to put folder being created

    Dim fso As New FileSystemObject

    Dim FolderName As String
    Dim DestinationFile As String

    FolderName = Replace(fso.GetFileName(SourceFile), "." & fso.GetExtensionName(SourceFile), "") ' remove extension from filename

    DestinationFolder = DestinationFolder & "\" & FolderName   'construct full path of new folder
    DestinationFile = DestinationFolder & "\" & fso.GetFileName(SourceFile) 'construct full path of file in new folder

    If fso.FolderExists(DestinationFolder) = False Then
        fso.CreateFolder DestinationFolder          'create the new folder
    End If

    fso.CopyFile SourceFile, DestinationFile      'copy file to new folder
    'fso.MoveFile SourceFile, DestinationFile     'move file to new folder

End Sub

edit: Dont forget to set a reference to Microsoft Scripting Runtime
Hi Moke, how would I cycle through the source files? there are multiple excel spreadsheets in the folder.

I thought I could use the common dialog box to select the folder name and pass it through, but Access 13 does not support the dialog box anymore.

I've found by stepping through my old code for this Access goes out to lunch so I'm looking at ways to populate a text box for the selection

Cliff

Update - I've got the text box working
 
Last edited:

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 11:56
Joined
May 7, 2009
Messages
19,233
see this sample.
 

Attachments

  • pcb.accdb
    416 KB · Views: 286

Cliff67

Registered User.
Local time
Yesterday, 20:56
Joined
Oct 16, 2018
Messages
175
arnelgp you absolute hero, that works like a dream, that has saved me days and days of work, I can't thank you enough
Update to this, I opened up the source folder and found some early .doc test results,

do I add
cn.Add Replace$(s, ".doc", "")
to the code?

many thanks

Cliff
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 11:56
Joined
May 7, 2009
Messages
19,233
so it is not .xlsx, but "all" the files on that folder?
i made changes for you to test.
 

Attachments

  • pcb.accdb
    696 KB · Views: 285

Cliff67

Registered User.
Local time
Yesterday, 20:56
Joined
Oct 16, 2018
Messages
175
so it is not .xlsx, but "all" the files on that folder?
i made changes for you to test.
That works a treat, testing it through and I've almost finished the first product range. Many many thank
 

Cliff67

Registered User.
Local time
Yesterday, 20:56
Joined
Oct 16, 2018
Messages
175
slight problem, It creates all the folders but then copies all the files to the target folder only
 

moke123

AWF VIP
Local time
Yesterday, 23:56
Joined
Jan 11, 2013
Messages
3,916
Opps. I'm late to the game.

I threw this together so might as well upload it.

BTW, if you didn't notice , in my version you can either move or copy the file to the new folder. Just comment/uncomment the line.
Its currently set to just copy them.
It also is not file type specific. It will move or copy any files in the source folder.
 

Attachments

  • MoveXls.accdb
    452 KB · Views: 194
Last edited:

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 11:56
Joined
May 7, 2009
Messages
19,233
sorry about that, i must have deleted most of the code.
here test it again.
 

Attachments

  • pcb.accdb
    696 KB · Views: 281

Cliff67

Registered User.
Local time
Yesterday, 20:56
Joined
Oct 16, 2018
Messages
175
sorry about that, i must have deleted most of the code.
here test it again.
that's Ok I do my testing with local copies of the spreadsheets from our repository, if it messes up I delete and re-download them again
 

Cliff67

Registered User.
Local time
Yesterday, 20:56
Joined
Oct 16, 2018
Messages
175
that seems to have done the trick, many thanks it is much appreciated
 

Users who are viewing this thread

Top Bottom