Solved

Vb script - copy files

Posted on 2014-11-03
13
182 Views
Last Modified: 2014-11-05
Hi

Can someone help me, I am trying to write a vb script that will do the following

1. Check certain files e.g cars.txt, bacs1.txt, maint.txt exist in a file location \\cslwinapp08\bacs, check that three files exist if not report, log error 'files don't exist'
2. if these three files exist in \\cslwinapp08\bacs copy these files to \\cslwinapp10\bacs
3. Check that these files have been copied to \\cslwinapp10\bacs and report a log to confirm 'files copied'

Thanks
0
Comment
Question by:rakkad
  • 6
  • 5
  • 2
13 Comments
 
LVL 11

Expert Comment

by:Dany Balian
ID: 40420580
something like this perhaps?

dim mainfolder1, mainfolder2, file1, file2, file3
mainfolder1 = "\\cslwinapp08\bacs\"
mainfolder2= "\\cslwinapp10\bacs\"
file1="cars.txt"
file2="bacs1.txt"
file3="maint.txt"
Dim oFSO
Set oFSO = CreateObject("Scripting.FileSystemObject")
If oFSO.FileExists(mainfolder1 & file1) and oFSO.FileExists(mainfolder1 & file2) and oFSO.FileExists(mainfolder1 & file3) Then
  'all files exist
  oFSO.CopyFile mainfolder1 & file1, mainfolder2
  oFSO.CopyFile mainfolder1 & file2, mainfolder2
  oFSO.CopyFile mainfolder1 & file3, mainfolder2
  if oFSO.FileExists(mainfolder2 & file1) and oFSO.FileExists(mainfolder2 & file2) and oFSO.FileExists(mainfolder2 & file3) then
    msgbox "Copy Successful"
  else
    msgbox "Copy Failed"
  end if
else
  msgbox "not all files exist" 'if you want the msgbox to specify which files are missing then we have to store the results of the fileexists checkups in a variable
end if

Open in new window


EDIT: if you want to replace the files in the destination directory in case they existed, then just specify true as a third parameter to the copyfile command

ex: oFSO.CopyFile mainfolder1 & file1, mainfolder2, true
0
 
LVL 12

Expert Comment

by:jkaios
ID: 40420677
Just to add some clarification that the "default value" of the third param (OverWriteFiles) of the CopyFile method of the FSO is True, so no need to specify or include it.  Other than that, Dany's code should do the job.
0
 

Author Comment

by:rakkad
ID: 40421296
Instead of creating a msgbox,  I would like the contents to be written to a log file

Thanks
0
Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

 
LVL 11

Expert Comment

by:Dany Balian
ID: 40421347
try this

dim mainfolder1, mainfolder2, file1, file2, file3
mainfolder1 = "\\cslwinapp08\bacs\"
mainfolder2= "\\cslwinapp10\bacs\"
file1="cars.txt"
file2="bacs1.txt"
file3="maint.txt"
Dim oFSO, myLog
Set oFSO = CreateObject("Scripting.FileSystemObject")
Set myLog = oFSO.OpenTextFile("C:\mylog.log", 2, True) '2 is forwriting, if you want to append to the log then use 8 instead
If oFSO.FileExists(mainfolder1 & file1) and oFSO.FileExists(mainfolder1 & file2) and oFSO.FileExists(mainfolder1 & file3) Then
  'all files exist
  oFSO.CopyFile mainfolder1 & file1, mainfolder2
  oFSO.CopyFile mainfolder1 & file2, mainfolder2
  oFSO.CopyFile mainfolder1 & file3, mainfolder2
  if oFSO.FileExists(mainfolder2 & file1) and oFSO.FileExists(mainfolder2 & file2) and oFSO.FileExists(mainfolder2 & file3) then
    mylog.WriteLine "Copy Successful"
  else
    mylog.WriteLine "Copy Failed"
  end if
else
  mylog.WriteLine "not all files exist" 'if you want the msgbox to specify which files are missing then we have to store the results of the fileexists checkups in a variable
end if
mylog.Close

Open in new window

0
 

Author Comment

by:rakkad
ID: 40421924
I ran the script above, but it is saying 'not all files exist'  I have specified three different files..

Thanks
0
 
LVL 11

Expert Comment

by:Dany Balian
ID: 40422099
try to access the files using explorer: (windows, run)
and type: \\cslwinapp08\bacs\
if it requires authentication enter your username and password (and tick save password)
when the explorer opens, are your 3 files there?
0
 

Author Comment

by:rakkad
ID: 40422158
I saved the file to a .vbs extension and renamed the file paths as a test e.g.  to see if the copy process worked:-

dim mainfolder1, mainfolder2, file1, file2, file3
mainfolder1 = "c:\Temp\test1"
mainfolder2 = "c:\Temp\test2"
file1 = "A.txt"
file2 = "B.txt"
file3 = "C.txt"
Dim oFSO, myLog
Set oFSO = CreateObject("Scripting.FileSystemObject")
Set myLog = oFSO.OpenTextFile("C:\temp\test1\mylog.txt", 8, True)
If oFSO.FileExists(mainfolder1 & file1) and oFSO.FileExists(mainfolder1 & file2) and oFSO.FileExists(mainfolder1 & file3) Then
  oFSO.CopyFile mainfolder1 & file1, mainfolder2
  oFSO.CopyFile mainfolder1 & file2, mainfolder2
  oFSO.CopyFile mainfolder1 & file3, mainfolder2
  if oFSO.FileExists(mainfolder2 & file1) and oFSO.FileExists(mainfolder2 & file2) and oFSO.FileExists(mainfolder2 & file3) then
    mylog.WriteLine "Copy Successful"
  else
    mylog.WriteLine "Copy Failed"
  end if
else
  mylog.WriteLine "not all files exist"
end if
mylog.Close

The files exist see attachment...  Re-ran the .vbs file and files are not being copied.  Thanks
U--UploadFiles.PNG
0
 
LVL 11

Expert Comment

by:Dany Balian
ID: 40422344
mainfolder1 should be: mainfolder1 = "c:\Temp\test1\" (the backslash at the end is crucial)
same for mainfolder2: mainfolder2 = "c:\Temp\test2\" <<notice the backslash at the end

final code is:
dim mainfolder1, mainfolder2, file1, file2, file3
mainfolder1 = "c:\Temp\test1\"
mainfolder2 = "c:\Temp\test2\"
file1 = "A.txt"
file2 = "B.txt"
file3 = "C.txt"
Dim oFSO, myLog
Set oFSO = CreateObject("Scripting.FileSystemObject")
Set myLog = oFSO.OpenTextFile("C:\temp\test1\mylog.txt", 8, True)
If oFSO.FileExists(mainfolder1 & file1) and oFSO.FileExists(mainfolder1 & file2) and oFSO.FileExists(mainfolder1 & file3) Then
  oFSO.CopyFile mainfolder1 & file1, mainfolder2
  oFSO.CopyFile mainfolder1 & file2, mainfolder2
  oFSO.CopyFile mainfolder1 & file3, mainfolder2
  if oFSO.FileExists(mainfolder2 & file1) and oFSO.FileExists(mainfolder2 & file2) and oFSO.FileExists(mainfolder2 & file3) then
    mylog.WriteLine "Copy Successful"
  else
    mylog.WriteLine "Copy Failed"
  end if
else
  mylog.WriteLine "not all files exist"
end if
mylog.Close

Open in new window

0
 

Author Comment

by:rakkad
ID: 40423483
Thanks for the update.

I changed the path and it works.. Great !!

I would like an additional development on the script, can you include a variable to state what files are missing i.e. if they do not exist ?

Thanks
0
 
LVL 11

Accepted Solution

by:
Dany Balian earned 500 total points
ID: 40423584
dim mainfolder1, mainfolder2, file1, file2, file3
dim file1exists, file2exists, file3exists
mainfolder1 = "c:\Temp\test1\"
mainfolder2 = "c:\Temp\test2\"
file1 = "A.txt"
file2 = "B.txt"
file3 = "C.txt"
Dim oFSO, myLog
Set oFSO = CreateObject("Scripting.FileSystemObject")
Set myLog = oFSO.OpenTextFile("C:\temp\test1\mylog.txt", 8, True)
file1exists=oFSO.FileExists(mainfolder1 & file1)
file2exists=oFSO.FileExists(mainfolder1 & file2)
file3exists=oFSO.FileExists(mainfolder1 & file3)
If file1exists and file2exists and file3exists Then
  oFSO.CopyFile mainfolder1 & file1, mainfolder2
  oFSO.CopyFile mainfolder1 & file2, mainfolder2
  oFSO.CopyFile mainfolder1 & file3, mainfolder2
  if oFSO.FileExists(mainfolder2 & file1) and oFSO.FileExists(mainfolder2 & file2) and oFSO.FileExists(mainfolder2 & file3) then
    mylog.WriteLine "Copy Successful"
  else
    mylog.WriteLine "Copy Failed"
  end if
else
  if not file1exists then 
    mylog.WriteLine mainfolder1 & file1 & " does not exist."
  end if
  if not file2exists then 
    mylog.WriteLine mainfolder1 & file2 & " does not exist."
  end if
  if not file3exists then 
    mylog.WriteLine mainfolder1 & file3 & " does not exist."
  end if
end if
mylog.Close

Open in new window

0
 

Author Comment

by:rakkad
ID: 40423708
The three different files need to have wildcards e.g.

Y01A.CARS*.*
GPL.MT940*.*
GPL.CRCSV*.*

As these files change on a daily basis, so... like below:-


dim mainfolder1, mainfolder2, file1, file2, file3
mainfolder1 = "c:\Temp\test1\"
mainfolder2 = "c:\Temp\test2\"
file1 = "Y01A.CARS*.*"
file2 = "GPL.MT040*.*"
file3 = "GPL.CRCSV*.*"
Dim oFSO, myLog
Set oFSO = CreateObject("Scripting.FileSystemObject")
Set myLog = oFSO.OpenTextFile("C:\temp\test1\mylog.txt", 8, True)
If oFSO.FileExists(mainfolder1 & file1) and oFSO.FileExists(mainfolder1 & file2) and oFSO.FileExists(mainfolder1 & file3) Then
  oFSO.CopyFile mainfolder1 & file1, mainfolder2
  oFSO.CopyFile mainfolder1 & file2, mainfolder2
  oFSO.CopyFile mainfolder1 & file3, mainfolder2
  if oFSO.FileExists(mainfolder2 & file1 ) and oFSO.FileExists(mainfolder2 & file2) and oFSO.FileExists(mainfolder2 & file3) then
    mylog.WriteLine "Copy Successful"
  else
    mylog.WriteLine "Copy Failed"
  end if
else
  mylog.WriteLine "not all files exist"
end if
mylog.Close

but the log file states not all files exist

Can you help thanks again ?
0
 
LVL 11

Expert Comment

by:Dany Balian
ID: 40423771
ok, if you copy "c:\Temp\test1\Y01A.CARS*.*" to another folder, it works.. however you can't check if "c:\Temp\test1\Y01A.CARS*.*" exists.. because the function expects a file name.. so you have to remove your checkups and just make the script copy files for you.. or you would have to loop through the folder to see if any file matching that criteria exists and then proceed.. which is a totally new project and question! you can post another question and see if other experts can help you with that part!
good luck
dan
0
 
LVL 12

Expert Comment

by:jkaios
ID: 40425212
If I were the Asker, I would accept Dany's solution right away!  He has tirelessly provided a very useful and thorough solution.  The original questions have already been answered, so why not accept his work now and close the question?
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

In this article we want to have a look at the directory attributes which are used by Microsoft to store the so called Security Identifiers (SID). These SIDs plays an important role in delegating and granting permissions and in authentication of trus…
Introduction During my participation as a VBScript contributor at Experts Exchange, one of the most common questions I come across is this: "I have a script that runs against only one computer. How can I make it run against a list of computers in …
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

831 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