Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 606
  • Last Modified:

VBS reference .txt file to copy files

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
Ninjaguy900
Asked:
Ninjaguy900
  • 3
  • 2
1 Solution
 
Bill BachPresidentCommented:
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
 
Bill BachPresidentCommented:
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
 
Ninjaguy900Author Commented:
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
 
Bill BachPresidentCommented:
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
 
Ninjaguy900Author Commented:
Thanks
spelling error on my part ! doh!
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now