Lets Code
Lets Code

Reputation: 42

FileSystemObject.CopyFile is not able to copy file from one folder to another

I have a piece of VBS code where a file(C:\test.txt) should be copied to a newly created temporary folder. But it fails to copy the file. Weird thing is that the same function works fine when I have wild characters in the source parameter (C:\ *est.txt). Any advice would be really helpful.

set fso = createobject("Scripting.FileSystemObject")
if fso.FileExists(src_temp) then
    'src_temp contains the file path.

    'Path of the temporary folder
    Set tmp_fld = fso.GetSpecialFolder(TemporaryFolder)
    tmp_fld = tmp_fld & "\OldFiles_tmp"

    'Create the temporary folder if does not exist
    If Not fso.FolderExists(tmp_fld) Then 
        fso.CreateFolder(tmp_fld)   
    End If

'Copy the files to temporary path
On Error Resume Next
fso.CopyFile src_temp, tmp_fld, True 'last parameter is set as true for overwriting the existing
On Error Goto 0

End If

I have verified if destination temporary folder is created and also the path and other stuffs. How can a wild character in the path makes the CopyFile work and same does not work for complete file name. Also how to solve this issue?

Upvotes: 1

Views: 4824

Answers (1)

Eduardo Poço
Eduardo Poço

Reputation: 3079

Put a backslash (\) at the end of the destiny folder:

tmp_fld = tmp_fld & "\OldFiles_tmp\" 'note the \

I tested and it worked for me. But, strangely, the wildcard thing was not working for me in the original source code.

Upvotes: 2

Related Questions