You are here:

VB Script/VBScript to move a file using a wildcard

Advertisement


Question
QUESTION: Hello:

I am trying to write a VBScript to move a file from a specific folder to a specific destination but the catch is that the file name changes everyday: TestFile###.txt where ### is a random number sequence. I have tried CopyFile and FileMove and both state “Path Not Found” due to the wild card. Is there a way to accomplish this task is VBScript. It is important to know that there will never be two files in this folder with the naming convention of TestText.

Here is the syntax that I have tried:

    Dim fso
    Set fso = CreateObject("Scripting.FileSystemObject")
    fso.MoveFile "C:\Directory\TestFile*.txt", "C:\Directory\NewLocation\TestFile.txt", True
    Set fso = Nothing


Thank you very much for your help in advance.

Regards,

Steve

ANSWER: Steve,

Try this:

dim objshell
Set objshell = CreateObject("Wscript.shell")
objshell.Run "cmd /c C:\Directory\TestFile*.txt, C:\Directory\NewLocation\TestFile.txt"
set objshell = nothing


Let me know if any issue.

Subbu.


---------- FOLLOW-UP ----------

QUESTION: Thank you very much for getting back to me so quickly. I ran this however it is not doing anything; it does not copy or move the file. Any thoughts?

Answer
Steve,

I am really sorry, I forgot Copy command.  Please try this and let me know.

dim objshell
Set objshell = CreateObject("Wscript.shell")
objshell.Run "cmd /c copy C:\Directory\TestFile*.txt C:\Directory\NewLocation\TestFile.txt"
set objshell = nothing


You can also use Move command in place of copy.

Subbu.

VB Script

All Answers


Answers by Expert:


Ask Experts

Volunteer


Subbu

Expertise

I can answer VB Script questions which are related to Visual Basic / VBA / QTP Scripts.

Experience

8 Years

Education/Credentials
Master of Computer Applications

Awards and Honors
Established Member from QA Forums and Brainbench certification

©2012 About.com, a part of The New York Times Company. All rights reserved.