VBScript deleting file after creation

Ernest Grogg
Ernest Grogg used Ask the Experts™
on
I have the following code that does seem to work but at the end it seems to delete the file.

It copies if needed but then deletes the *.accdb  so that when Access opens the file is missing.  I can see it copying the db but then it deletes it?  either way...even if the file is there it still removes it?

don't know where I am going wrong...

by the way I do plan on changing the links of the deployment version to a network unc path

' ----- ExeScript Options Begin -----
' ScriptType: window,activescript,invoker
' DestDirectory: current
' Icon: default
' deployment.vbs
' ----- ExeScript Options End -----
'declare an object to be a filesystem object
Public fs
'declare an object to be a MS Access application
Public acApp
 'declare a wscript shell
Public WshShell

Main()

'-------------------------------------------------------------------------------------------------------------------------------------------
Sub Main()
dim MyConn
dim MyConn1
Dim LocalSplit
Dim ServerSplit
Dim x
Dim LocalBad



OpenCmdWindow()

Set fs = CreateObject("Scripting.FileSystemObject")

CheckDeployment()

If fs.FileExists("C:\Deployment\DeploymentVersion.txt") then
    Set MyConn= fs.OpenTextFile("C:\Deployment\DeploymentVersion.txt",1, true)
    ServerSplit = Split(MyConn.readall, ".")
    MyConn.Close
Else
    ServerSplit =Split("0.0.0.0", ".")
end if

If fs.FileExists("C:\LocalVersion\Version.txt") then
    Set MyConn1= fs.OpenTextFile("C:\LocalVersion\Version.txt",1, true)
    LocalSplit = Split(MyConn1.readall, ".")
    MyConn1.Close
else
    LocalSplit =Split("0.0.0.0", ".")
end if

set myconn = nothing
set myconn1 = nothing

If UBound(LocalSplit) <> UBound(ServerSplit) Then
        Call CopyUpdate()
        FireDB()
        Exit Sub
End If

For x = 0 To UBound(LocalSplit)
    If cint(ServerSplit(x)) > cint(LocalSplit(x)) Then
        Call CopyUpdate()
        FireDB()
        Exit Sub
    End If
Next

FireDB()

end sub

'------------------------------------------------------------------------------------
private Sub CopyUpdate()
Dim BuiltPath
Dim userpath


userpath= "C:\Deployment\"
BuiltPath = "C:\LocalVersion\"
'DeploymentVersionBackup = "C:\DeploymentVersionBackup\"

TempPath = "c:\prod\temp"

If fs.FolderExists(BuiltPath) = False Then
    fs.CreateFolder BuiltPath

End If
If fs.FolderExists(userpath) = False Then
    fs.CreateFolder userpath

End If

fs.copyfile "C:\Deployment\test.accdb", "C:\LocalVersion\test.accdb", -1
'fs.copyfile "C:\Deployment\deployment.vbs","C:\LocalVersion\LocalVersion.vbs", -1
fs.copyfile "C:\Deployment\DeploymentVersion.txt","C:\LocalVersion\Version.txt",-1

End Sub
'-----------------------------------------------------------------------------------------

Function CheckDeployment() 
Dim BuiltPath
BuiltPath = "C:\LocalVersion\test.accdb"
If fs.FileExists(BuiltPath) = False Then
    Call CopyUpdate()
End If



End Function
'---------------------------------------------------------------------------------------
sub FireDB

CloseCmdWindow()
Dim WshShell, PathToMDE, accessPath
Set WshShell = WScript.CreateObject("WScript.Shell")
PathToMDE = "C:\LocalVersion\test.ACCDB"
accessPath = "C:\Program Files\Microsoft Office 15\root\office15\MSAccess.exe"

WshShell.run chr(34) & accessPath & chr(34) & " " & chr(34) & PathToMDE & chr(34)

If fs.FileExists("C:\LocalVersion\test.accdb") then
    fs.DeleteFile "C:\LocalVersion\test.accdb", True
End If
If fs.FileExists("C:\LocalVersion\test.laccdb") then
    fs.DeleteFile "C:\LocalVersion\test.laccdb", True
End If
end sub

'-------------------------------------------------------------
sub OpenCmdWindow()
set WshShell = WScript.CreateObject("WScript.Shell")
WshShell.Run "cmd.exe /q",0
WScript.Sleep 10
WshShell.AppActivate "c:\windows\system32\cmd.exe /q"
WScript.Sleep 100
WshShell.Popup "Starting...." & vbcrlf & vbcrlf &  "Checking Version" & vbcrlf & vbcrlf & "Downloading if necessary...." ,5 ,"ALERT! Please Wait! This will close Automatically",0+48
SET WshShell= Nothing
end sub

 '------------------------------------------------------------
sub CloseCmdWindow()
dim oshell
dim oWmg
dim strWndprs
dim objQResult

WScript.Sleep 100

Set oShell = CreateObject("WScript.Shell") 
Set oWmg = GetObject("winmgmts:") 

strWndprs = "select * from Win32_Process where name='cmd.exe'" 
Set objQResult = oWmg.Execquery(strWndprs) 

For Each objProcess In objQResult 
	intReturn = objProcess.Terminate(1) 
Next
End sub

'------------------------------------------------------------

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Bill PrewIT / Software Engineering Consultant
Top Expert 2016

Commented:
Won't this always delete the file, not sure what your intention was?

If fs.FileExists("C:\LocalVersion\test.accdb") then
    fs.DeleteFile "C:\LocalVersion\test.accdb", True
End If

Open in new window


»bp
Ernest GroggSecurity Management InfoSec

Author

Commented:
so I will have one deployment version on a network folder (for now I used the local machine) and then I will have the LocalVersion on the local machine.

First I was trying to test if the LocalVersion Folder was there, if not, create the folder.  That then should have then prompted to just copy the items to the LocalVersion folder and then open the db.  Else: if all is there then check the version #'s in each text file....if a version # was different in the .txt file, then remove the files then copy the new files
Bill PrewIT / Software Engineering Consultant
Top Expert 2016

Commented:
I think I would need to better understand what you are trying to do in the script, it's not clear from the code, or what you have described so far.


»bp
Introduction to Web Design

Develop a strong foundation and understanding of web design by learning HTML, CSS, and additional tools to help you develop your own website.

Ernest GroggSecurity Management InfoSec

Author

Commented:
so maybe here:

1. open the cmd window and use the message popup window.
2. set the FileSystemObject
3. run "CheckDeployment"--checks for the "buildpath"  which is the LocalVersion folder and db...if it does not exist then #4 else go to #5
4. run "CopyUpdate"--this creates the folders necessary and the files
5. next we compare the text files...if the local and server are not equal, run "CopyUpdate"--that will copy the files needed, if equal, go to #6
6. run FireDB--this starts the Access program  and the path to the file also closes the cmd window

If I comment out in FireDB, sure it does not delete, but I want to delete, not just overwrite or copy/paste if the text files are different

leaving that there deletes it no matter what.

does this help any?
Ernest GroggSecurity Management InfoSec

Author

Commented:
sorry, but have a meeting and will return I a couple of hours
Ernest GroggSecurity Management InfoSec

Author

Commented:
Ok.. Back now
Consulting
Distinguished Expert 2017
Commented:
IMO, the FireDB procedure does not do its intended job.

It launch MS Access, check for files existance and delete files.
Why that ?
It should just check for files existances, then if successfull launch MS Access.
So remove those DeleteFile statements, they have no business here.

On the other hand, the CopyUpdate procedure should copy files and overwrite no matter what, to ensure the most up to date files are available.
Ernest GroggSecurity Management InfoSec

Author

Commented:
changed to remove the delete in FireDB

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial