Create new folder

JonRowland

Active Member
Joined
May 9, 2003
Messages
417
Office Version
  1. 365
Platform
  1. Windows
Hi guys,

I'm pulling my hair out as to why the following isn't working. Basically, I am looking to move CSV files from one folder to a another one which will be create if doesn't exist.

However, when the below is run FSO.CreateFolder (ToPath) gives me an Error 76 - PATH NOT FOUND, rather than create within H:\StartFolder the new subfolders and subfolder within "\MoveItHere\BaseFiles".

What am I missing?


<code>
Sub Move_Certain_Files_To_New_Folder()

' This example move all Excel files from FromPath to ToPath.
' Note: It will create the folder ToPath
' Sourced from https://www.rondebruin.nl/win/s3/win026.htm

Dim FSO As Object
Dim FromPath As String
Dim ToPath As String
Dim FileExt As String
Dim FNames As String
Dim strFromDir As String
Dim strSavedFilePath As String

strFromDir = "H:\StartFolder"
strSavedFilePath = strFromDir + "" + "MoveItHere\BaseFiles"

Err = 0 ' For debugging

' Set Paths. Variables used as will be set Public prior to reaching this code.
FromPath = strFromDir
ToPath = strSavedFilePath

FileExt = "*.csv" '

If Right(FromPath, 1) <> "" Then
FromPath = FromPath & ""
End If

FNames = Dir(FromPath & FileExt)
If Len(FNames) = 0 Then
MsgBox "No files in " & FromPath
Exit Sub
End If

On Error Resume Next
Set FSO = CreateObject("scripting.filesystemobject")

' Create New Folder
FSO.CreateFolder (ToPath) ' Error 76 PATH NOT FOUND

FSO.MoveFile Source:=FromPath & FileExt, Destination:=ToPath

End Sub
</code>
 

Excel Facts

Select a hidden cell
Somehide hide payroll data in column G? Press F5. Type G1. Enter. Look in formula bar while you arrow down through G.
.
First thing missing is placing the code between the HASH marks so it displays correctly on screen here. It makes it so much easier to view.

You can go back in to edit your post, highlight all of the code, then look above on the small menu bar and find the 'pound symbol' # ... click that and your code will
be properly formatted for the FORUM.

To your question ... I haven't tested this here ... but I think it might be one of the following :

Code:
strFromDir = "H:\StartFolder\"     '<--- place the backslash after StartFolder
strSavedFilePath = strFromDir + "" + "MoveItHere\BaseFiles"


'OR


strFromDir = "H:\StartFolder"
strSavedFilePath = strFromDir + "\" + "MoveItHere\BaseFiles"  '<--- place the backslash between the quotes
 
Upvote 0
Logit apologies for the formatting error. Couldn't edit original post.

Thanks for tip but didn't fix my problem although it did let me see I had an error in the original code which should have added the \

It would appear that the issue is because I was trying to create a Subfolder1 & Subfolder2 in one go. I managed to counter this by creating each subfolder at a time.

My interim code prior to adding the necessary variables

Code:
Sub Move_Certain_Files_To_New_Folder()
' This example move all Excel files from FromPath to ToPath.
' Note: It will create the folder ToPath
' Sourced from https://www.rondebruin.nl/win/s3/win026.htm

    Dim FSO As Object
    Dim FromPath As String
    Dim ToPath As String
    Dim FileExt As String
    Dim FNames As String
    Dim strFromDir As String
    Dim strSavedFilePath As String

    Err = 0    ' For debugging

    FromPath = "H:\StartFolder"
    <font color = "Green">ToPath = FromPath & "\" & "MoveItHere" & "\"</font>
    <font color = "Red">ToPath2 = ToPath + "BaseFiles" & "\"</font>

    FileExt = "*.csv"    '

    If Right(FromPath, 1) <> "\" Then
        FromPath = FromPath & "\"
    End If

    FNames = Dir(FromPath & FileExt)
    If Len(FNames) = 0 Then
        MsgBox "No files in " & FromPath
        Exit Sub
    End If

    On Error Resume Next
    Set FSO = CreateObject("scripting.filesystemobject")

    ' Create New Folder
<font color = "Green">    FSO.CreateFolder (ToPath)</font>
<font color = "red">    FSO.CreateFolder (ToPath2)</font> 

    FSO.MoveFile Source:=FromPath & FileExt, Destination:=ToPath2

End Sub
 
Upvote 0
.
So .... you have it working now ? Or no ?

You can also place OPTION EXPLICIT at the top of your macro (above : Sub Move_Certain_Files_To_New_Folder()) and this
will assist you in finding coding errors. If you are not familiar with the OPTION statement it will require you
to DIM your variables but also points out coding errors.
 
Upvote 0
Yes it is working. I am very familiar with Option Explicit just didn't include it in my example, not that it would have picked up my typo error.
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,333
Members
452,636
Latest member
laura12345

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top