1

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?

4
  • try adding the file name to the destination as well Commented Dec 16, 2015 at 13:01
  • 2
    It might have something to do with windows not properly detecting whether tmp_fld is supposed to be a file or a folder. In this case it would help if you replace tmp_fld = tmp_fld & "\OldFiles_tmp" with tmp_fld = tmp_fld & "\OldFiles_tmp\" (trailing backspace). This is just a wild guess though. Commented Dec 16, 2015 at 13:02
  • src_temp contains the file path... Does it include file name? Commented Dec 16, 2015 at 13:38
  • Yes. Adding a trailing backspace worked. Thank you. Commented Jan 11, 2016 at 10:38

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.