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

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

Vb script - copy files

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
rakkad
Asked:
rakkad
  • 6
  • 5
  • 2
1 Solution
 
Dany BalianCTOCommented:
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
 
jkaiosCommented:
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
 
rakkadAuthor Commented:
Instead of creating a msgbox,  I would like the contents to be written to a log file

Thanks
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
Dany BalianCTOCommented:
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
 
rakkadAuthor Commented:
I ran the script above, but it is saying 'not all files exist'  I have specified three different files..

Thanks
0
 
Dany BalianCTOCommented:
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
 
rakkadAuthor Commented:
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
 
Dany BalianCTOCommented:
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
 
rakkadAuthor Commented:
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
 
Dany BalianCTOCommented:
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
 
rakkadAuthor Commented:
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
 
Dany BalianCTOCommented:
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
 
jkaiosCommented:
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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.

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