Vba rename query

Pobek

Board Regular
Joined
Jul 7, 2015
Messages
99
I have a two texts files say text1.txt and text2.txt, I am trying to copy them over to another folder and rename them to (rename1 and rename2). The new names are in a particular column. I have written the code below but it is giving me a runtime error saying the source file cannot be found even though the link in the macro actually does open the file. This is the code below, can someone help please?

VBA Code:
Sub CopyFolderContents()
Dim fso As Object
Dim sourceFolder1, sourceFolder2, sourceFile1, sourceFile2 As String
Dim destinationFolder1, destinationFolder2, destParentFolder, destinationFile1, destinationFile2 As String
Dim i, j As Variant


For j = 1 To 5
    ' Initialize the FileSystemObject
    Set fso = CreateObject("Scripting.FileSystemObject")
   
    'Set the source and destination folder paths (change as needed)
    destParentFolder = "C:\XTemp" & "\" & Range("C47").Value2
    sourceFile1 = Cells(52 + j, 3).Value2 'Range("ParentOUT").Value2 & "\" & 203 ' "C:\Path\To\Source\Folder\"
    sourceFile2 = Cells(57 + j, 3).Value2 'Range("ParentOUT").Value2 & "\" & 402 ' "C:\Path\To\Source\Folder\"


    destinationFolder1 = "C:\XTemp" & "\" & Range("C47").Value2 & "\" & 203
    destinationFolder2 = "C:\XTemp" & "\" & Range("C47").Value2 & "\" & 402
    destinationFile1 = "C:\XTemp" & "\" & Range("C47").Value2 & "\" & 203 & "\" & Cells(52 + j, 2).Value2 & "\" & ".txt"
    destinationFile2 = "C:\XTemp" & "\" & Range("C47").Value2 & "\" & 402 & "\" & Cells(57 + j, 2).Value2 & "\" & ".Am1"
  
    strFolderExists = Dir(destParentFolder, vbDirectory)
    If strFolderExists = "" Then MkDir Path:=destParentFolder
   
    strFolderExists = Dir(destinationFolder1, vbDirectory)
    If strFolderExists = "" Then MkDir Path:=destinationFolder1
   
    strFolderExists = Dir(destinationFolder2, vbDirectory)
    If strFolderExists = "" Then MkDir Path:=destinationFolder2

   fso.CopyFile sourceFile1, destinationFile1
    'fso.CopyFile Chr(34) & sourceFile1 & Chr(34), Chr(34) & destinationFile1 & Chr(34)
    fso.CopyFile sourceFile2, destinationFile2
    ' Clean up
    Set fso = Nothing
Next j

'MsgBox "All contents have been copied from " & sourceFolder & " to " & destinationFolder, vbInformation
End Sub
 

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand
You don't appear to be specifying a path for the source files.
 
Upvote 0
OK, so which line is actually giving the error?
 
Upvote 0
OK, so which line is actually giving the error?
the first line below (copied multiple lines to help you spot it)

fso.CopyFile sourceFile1, destinationFile1
'fso.CopyFile Chr(34) & sourceFile1 & Chr(34), Chr(34) & destinationFile1 & Chr(34)
fso.CopyFile sourceFile2, destinationFile2
' Clean up
Set fso = Nothing
Next j

I wonder if it's because there is space within the string of the path itself? :unsure:
 
Upvote 0
Shouldn't be an issue. How long are the filenames including paths?
 
Upvote 0
Just noticed that your destination file names seem to have backslashes just before the extensions...
 
Upvote 0

Forum statistics

Threads
1,223,885
Messages
6,175,180
Members
452,615
Latest member
bogeys2birdies

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