VBScript - Check file usage

Discussion in 'Software' started by CobolExpert, Jul 8, 2004.

  1. CobolExpert

    CobolExpert Private E-2

    I am a noob at this VB stuff. I have been searching online and in my books but I can't seem to figure out how to check to see if a file is in use.

    I want to copy a file set from one directory to another (overwriting it). Before I do that I want to make sure that no one is using either of the directories. If someone is using one or both of them, I want to let the user running the script know that they are in use and by whom.

    I assumed that the 'InUseCount' in Win32_Directory might help... but that field never seems to get populated.

    Any ideas???

    Thanks,
    JB
     
  2. Kodo

    Kodo SNATCHSQUATCH

    off the top of my head. Use the FILESYSTEMOBJECT (FSO) to move the file to itselft for ex:

    move C:\file.txt to C:\file.txt

    essentially overwriting the file. If the file is in use it will pop an error. Trap the error and you're all set.
    I can't think of or find anything that specifically detects if a file is in use in VBScript
     
  3. CobolExpert

    CobolExpert Private E-2

    Will the error tell me who is using the file?

    Thanks,
    JB
     
  4. Kodo

    Kodo SNATCHSQUATCH

    not that I'm aware of. And in retrospect, I don't think the error will pop on files that don't have any lock state, like a text file or something. The only thing I can think of is to build some sort of file monitoring database where you can "check-in" "check-out" files.. I'm sure there is some software that does that already and it would save you the time of having to write one.
     
  5. CobolExpert

    CobolExpert Private E-2

    I'm not sure this is the right spot for a WMI question but...

    Win32_Serverconnection almost gets what I am looking for. It shows all the connections to the file directory on the server I am looking at. It even shows how many files are open ('NumberofFiles' under Win32_ConnectionShare.Depenent). Unfortunately, it falls just short of telling me exactly what files are open. Sure everyone in the company has a drive open to the public directory and some of them may be using files, but who is using files in the directory I want to overwrite?? I don't know... I am going to keep digging in this one and see if I can get it to work.

    -JB
     
  6. CobolExpert

    CobolExpert Private E-2

    For the record, in case anyone is looking for this in the future...

    The script below does almost exactly what I want... It shows all users connected to files on the server. Now I just have to parse it out and have it show only the directories I care about.

    On a seperate but similiar note, here is a article about it -
    http://www.winnetmag.com/Windows/Article/ArticleID/24503/24503.html

    ----

    Dim oWinNTObj
    Dim sFileServer
    Dim sMsg

    ' Not sure why but seems like last Path value is always null. Checking for Null
    ' doesn't work. Only works by adding the following statement in order to ignore
    ' the error.
    On Error Resume Next

    sMsg = "USER PATH" & vbCrLf
    sMsg = sMsg & "-----------------------------------------------" & vbCrLf

    sFileServer = "yourserver"

    Set oWinNTObj = GetObject("WinNT://" & sFileServer & "/LanManServer")

    For Each oResource In oWinNTObj.Resources
    ' If Not(IsNull(CStr(oResource.Path))) Then

    ' Skip entries that have no file suffix (eg ".doc") and assume
    ' they are folder or pipe entries. List only file entries.
    If Left(Right(CStr(oResource.Path),4),1) = "." Then
    sMsg = sMsg & CStr(oResource.User) & " "
    sMsg = sMsg & CStr(oResource.Path) & vbCrLf
    End If

    ' End If
    Next

    WScript.Echo sMsg

    Set oWinNTObj = Nothing

    ----
     
  7. Kodo

    Kodo SNATCHSQUATCH

    very cool.
    Sorry I couldn't be of more assistance to you.
     

MajorGeeks.Com Menu

Downloads All In One Tweaks \ Android \ Anti-Malware \ Anti-Virus \ Appearance \ Backup \ Browsers \ CD\DVD\Blu-Ray \ Covert Ops \ Drive Utilities \ Drivers \ Graphics \ Internet Tools \ Multimedia \ Networking \ Office Tools \ PC Games \ System Tools \ Mac/Apple/Ipad Downloads

Other News: Top Downloads \ News (Tech) \ Off Base (Other Websites News) \ Way Off Base (Offbeat Stories and Pics)

Social: Facebook \ YouTube \ Twitter \ Tumblr \ Pintrest \ RSS Feeds