Macro to make director f folder does not exist

howard

Well-known Member
Joined
Jun 26, 2006
Messages
6,561
Office Version
  1. 2021
Platform
  1. Windows
I have the following macro below to move files in C:\temp to C:\old temp.

If C:\old temp does not exist, I need this folder created


It would be appreciated if someone could kindly amend my code to accommodate this

Code:
 Sub moveFiles()           


zFromPath = "C:\temp\" '<< specify source folder here
zToPath = "C:\old Temp\"                '<< specify destination folder here

zExt = Array("*.csv", "*.xls*")         '<< define file extensions for files to be moved

For Each x In zExt                      'process each specified file extension
zFile = Dir(zFromPath & x) 'get filename in source folder
Do While zFile <> "" And (zFile <> ThisWorkbook.Name) 'ignore THIS file
zFetch = zFromPath & zFile 'source file to be moved
On Error Resume Next 'skip if file is open or already there
Kill (zToPath & zFile) 'delete file (if exists) before moving
Name zFetch As (zToPath & zFile) 'move file to destination folder
On Error GoTo 0 'reset error trap
zFile = Dir 'fetch next matching file in source folder
Loop 'continue processing matching files
Next                                    'process next file extension type

'**************************************
'CHECK FOR ANY FILES NOT MOVED..
'**************************************
For Each x In zExt 'process each specified file extension
zFound = zCountFiles(zFromPath, x)
zCount = zCount + zFound
Next

If zCount > 0 Then
saywhat = saywhat & zCount
saywhat = saywhat & " file(s) were not moved because they are currently open" & vbCr
saywhat = saywhat & "or copies are already in the destination folder." & vbCr
saywhat = saywhat & vbCr & vbCr
End If
'**************************************
'DISPLAY COMPLETION MESSAGE..
'**************************************
saywhat = saywhat & "You can find the files from:" & vbCr
saywhat = saywhat & zFromPath & vbCr & vbCr
saywhat = saywhat & "in folder location:" & vbCr
saywhat = saywhat & zToPath & vbCr

MsgBox saywhat

End Sub

Function zCountFiles(zFolder, zExt)

zPath = zFolder
If Right(zPath, 1) <> "\" Then zPath = zPath & "\"

zFile = Dir(zPath & zExt)
Do While zFile <> "" And (zFile <> ThisWorkbook.Name) 'ignore THIS file
zCountFiles = zCountFiles + 1
zFile = Dir 'fetch next matching file in source folder
Loop                                    'continue processing matching files

End Function
 

Excel Facts

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.
The first few lines of your code amended
- path separator is added after existence of folder checked
VBA Code:
Dim zToPath As String, zFromPath As String
zFromPath = "C:\temp\"
zToPath = "C:\old Temp"
If Dir(zToPath, vbDirectory) = "" Then MkDir zToPath
zToPath = zToPath & "\"

NOTE
A sub-folder cannot be created before the folder above has been created
So in example below if FolderA does not exist .... create that folder first BEFORE trying to create myFolder
C:\TEST\FolderA\myFolder
 
Upvote 0

Forum statistics

Threads
1,214,411
Messages
6,119,360
Members
448,888
Latest member
Arle8907

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