Solved

VBS reference .txt file to copy files

Posted on 2009-07-14
5
596 Views
Last Modified: 2012-08-13
Hi Experts
I am looking to schedule a script that can reference a .txt file with a list of files & locations then copy them to a backup location.
Can this be done in VBS as new to scripting and really don't know where to start!

thanks



0
Comment
Question by:Ninjaguy900
  • 3
  • 2
5 Comments
 
LVL 28

Expert Comment

by:Bill Bach
ID: 24852993
Yes.  Define a File System Object:
   Public fso
   set fso = CreateObject("Scripting.FileSystemObject")

Read the file list in from the text file, then use the simple command:
    fso.CopyFile fso.BuildPath(OriginalDir,FileName), fso.BuildPath(BackupDir,FileName), True

Obviously, you need to define the variables:
OriginalDir: The original directory (which you can read from the text file)
BackupDir: The location for the backups
FileName: The file name to copy

The code for reading the files wiull vary depending on the format of your text file, of course, so I don't have sample code for that.
0
 
LVL 28

Expert Comment

by:Bill Bach
ID: 24853039
Something like this might work, if your source list is a simple list of filenames with full paths:

set Cfg = fso.OpenTextFile(ConfigFile)
do while not Cfg.AtEndOfStream
    str = RTrim(Cfg.ReadLine)
    fso.CopyFile str, fso.BuildPath(BackupDir,fso.GetFileName(str)), True
loop
0
 

Author Comment

by:Ninjaguy900
ID: 24857814
Many thanks
it's a start, but I'm still having trouble with the code to access the file

for example
If the ref file is c:\backups\ref_file.txt

looks like this
C:\GA\report\New Text Document.txt
C:\GA\report\New Text Document (2).txt
C:\GA\report\New Text Document (3).txt

how can i copy the file listed to a alternative location??

thanks
0
 
LVL 28

Accepted Solution

by:
Bill Bach earned 250 total points
ID: 24859548
You need to put in your own values into the script:

Public fso
set fso = CreateObject("Scripting.FileSystemObject")
set Cfg = fso.OpenTextFile("c:\backups\ref_file.txt")
do while not Cfg.AtEndOfStream
    str = RTrim(Cfg.ReadLine)
    fso.CopyFile str, fso.BuildPath("C:\alternativelocation",fso.GetFileName(str)), True
loop
0
 

Author Comment

by:Ninjaguy900
ID: 24859734
Thanks
spelling error on my part ! doh!
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Not long ago I saw a question in the VB Script forum that I thought would not take much time. You can read that question (Question ID  (http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/Q_28455246.html)28455246) Here (http…
This article will show, step by step, how to integrate R code into a R Sweave document
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now