473,836 Members | 1,586 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Check if a file is open in VB Script

283 Contributor
Hello,

I have been searching and searching to try and find a way to check and see if a file is open or not but I have had no luck so far.

The over all bigger picture is that I wrote a vb script to run as an update, but before running the update I want to check if the program is open or not and if it is to prompt the user that the program is open and that they need to close it before the update will run.

Any help would be much appreciated!

Thanks,

Slen

Here is what I have so far. Just trying to figure out how I can add to this to check if the file is open...

Expand|Select|Wrap|Line Numbers
  1. Dim x  
  2. x=MsgBox("Do you wish to install the Program?",1,"File Install")
  3.  
  4. If x = vbOK then 
  5.  
  6. Const FULL_PATH = "C:\DatabaseTestFolder"
  7. Set fso = CreateObject("Scripting.FileSystemObject")
  8.  
  9. BuildPath FULL_PATH
  10. CreateFile 
  11.  
  12. End if
  13.  
  14. Sub CreateFile()
  15. dim filesys
  16. dim strFile
  17. dim strDest
  18.  
  19. strFile = "C:\Documents and Settings\Desktop\Test.accde" 
  20. strDest = "C:\DatabaseTestFolder\Test.accde"
  21.  
  22.  
  23. set filesys=CreateObject("Scripting.FileSystemObject")
  24. If filesys.FileExists(strFile) Then
  25.    filesys.CopyFile strFile, strDest
  26.    MsgBox("The file has been saved to your C:\Drive as " & strDest)
  27.  
  28.    End If
  29.  
  30. If err.Number <> 0 then
  31.     msgbox(err.Description &  " " & err.Number)
  32. Else
  33.     msgbox("Testing")
  34.  
  35. End if
  36.  
  37. End Sub
  38.  
  39. Sub BuildPath(ByVal Path)
  40. If Not fso.FolderExists(Path) Then
  41. BuildPath fso.GetParentFolderName(Path)
  42. fso.CreateFolder Path
  43. End If
  44. End Sub
  45.  
Feb 14 '12 #1
4 15811
slenish
283 Contributor
Well I guess I will answer this one myself since no one else replied and I figured it out.

If you should have the same problem as me and you are trying to check if a file is open this is what i did...

Expand|Select|Wrap|Line Numbers
  1. Set objFSO = CreateObject("Scripting.FileSystemObject")
  2.  
  3. File1 = "C:File location\file name.Open file type"
  4. 'example since i was looking for a database that was open I did this
  5. 'File1 = "C:\TestFolder\Database1.laccdb" 
  6.  
  7. If objFSO.FileExists (FILE1) then
  8.  
  9. MsgBox "The Program is Currently Open." 0 "Warning Program Open"
  10.  
  11. Else
  12.  
  13. 'Some other Code
  14.  
  15. End if
  16.  
Hope this helps anyone else out that has the same problem.

Take care
Feb 17 '12 #2
Rabbit
12,516 Recognized Expert Moderator MVP
Just so you're aware, that doesn't do what you think it does. It only checks if a file exists, not if it's open. In this case, I think your true purpose is to check whether or not someone had a .mdb file open. So you're checking whether or not the lock file exists. However, you need to be aware that Access is bad at cleaning up the lock file so it may exist even if the file is not open.

I am also moving the thread to the Access forum where you may get more help.
Feb 17 '12 #3
ADezii
8,834 Recognized Expert Expert
The following In-Line Code will check and see if a File, (in this case an Access Database), is Open or not:
Expand|Select|Wrap|Line Numbers
  1. On Error Resume Next
  2. Dim strFileName As String
  3.  
  4. 'Absolute PATH to File
  5. strFileName = "C:\Test\Test.mdb"
  6.  
  7. If Dir$(strFileName) = "" Then
  8.   MsgBox strFileName & " is not in the specified PATH!"
  9.     Exit Sub
  10. End If
  11.  
  12. Open strFileName For Binary Access Read Write Lock Read Write As #1
  13. Close #1
  14.  
  15. 'If an error occurs, the File/Document is Open
  16. If Err.Number <> 0 Then
  17.   MsgBox "Error# " & CStr(Err.Number) & " - " & Err.Description
  18.     Err.Clear
  19. End If
Feb 17 '12 #4
NeoPa
32,584 Recognized Expert Moderator MVP
I've reset the Best Answer as we don't encourage members to set their own posts as Best Answer anyway, but particularly when it doesn't even answer the question adequately (or at all).
Feb 20 '12 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

0
1231
by: Giraud, Sebastien | last post by:
I should work cause it's made for it :) I had same problem checking socket bind and try did help me well :) -----Message d'origine----- De : python-list-bounces+sebastien.giraud=eds.com@python.org De la part de Maxim Khesin Envoyé : mercredi 17 décembre 2003 23:41 À : python-list@python.org Objet : is file open?
1
1639
by: Neil MacGaffey | last post by:
Hi - I'm new to python and am stymied by something that should be simple. The file open command below works fine on my desktop machine, but I cant get it to work on my laptop. Since using Python on my laptop is realistically the only way I'm going to find the time to learn Python, this is a big problem for me. Here's what I'm up against: PythonWin 2.3.3 (#51, Dec 18 2003, 20:22:39) on win32. Portions Copyright 1994-2001 Mark...
1
1435
by: Dfenestr8 | last post by:
Earlier I posted asking for a file browsing script, for a GUI app I'd written. Basically, I was after a graphical tool with which I could search for files on my disk, and would return the path of a file through the command line. I got no reply, either because I didn't describe the problem correctly, or because nobody could point me to such a script. So I wrote my own. http://hairy.asscrack.net/~hairyman/file_selector.py
3
3202
by: Farshid Lashkari | last post by:
Hi, My goal is to detect all (or most) file dependencies of a script (i.e. modules, dlls, data files). Currently, after a script is finished executing I use sys.modules to determine all module dependencies, and I use win32process.EnumProcessModules to determine DLL dependencies. This works reasonably well, however I would still like to detect dependencies from regular file open calls. I did a little test by modifying __builtins__.open...
1
1843
by: fatjoez | last post by:
Hey there. I've been trying to modify my file upload script so that it handles 10 files instead of one. i was thinking the most straightforward way would be to add a FOR LOOP? placed strategically somewhere like just before the my variables get declared??? the POST input name is "fileup" so maybe i could call them fileup1, fileup2 etc. This is the upld.pl script itself.
1
4278
by: jtertin | last post by:
I am currently using the following code to make sure a file is writable (i.e. is not in use) by using the CanWrite method of the FileStream object. Using the code below, the TextWriter is still trying to write to the file and is throwing an exception (File... in use by another process) whenever the output file (strFileName) is open. I am trying to prevent it from writing to it at all if this is the case... Any insight? Public...
5
5940
by: bvidinli | last post by:
is there a way to find out if file open in system ? - please write if you know a way other than lsof. because lsof if slow for me. i need a faster way. i deal with thousands of files... so, i need a faster / python way for this. thanks. -- Ý.Bahattin Vidinli Elk-Elektronik Müh.
5
1446
by: =?Utf-8?B?UGF1bA==?= | last post by:
Hi I am able to upload files to a server with a .net web application. The problem is I am providing a hyperlink so the user can open the file after it is placed on the server. For the URL I am using the absolute path to the file and setting this dynamically after the file is uploaded. For the hyperlink I have the target set to _blank. When clicking on the hyperlink nothing happens, wondering if I need to use a file open control and if...
1
5907
by: apurvaG | last post by:
Hi All, I wanted to know, can I check in action script whether the browser window which loads my swf file has been closed? Basically I wanted to call a action script function which will be triggered when the browser window is closed. Can I do this check in action script of the flash file which is loaded in the browser? Thanks in advance, Apurva G
2
4914
by: kumarboston | last post by:
Hi All, How do we open certain number of files in for loop. In my case I have couple of files with name scd_1_2.dat here, the number 1 goes till 5 and 2 goes till 14, I am trying to open recursively but getting some error. #!/usr/bin/perl use strict; use warnings; for (my $i=1;$i<=5;$i++) {
0
9668
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10843
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10589
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9371
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7790
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6978
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5648
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4448
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4015
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.