Jump to content

About file process, is this possible?


Recommended Posts

Let say I have a network file myservermyfoldermyfile.exe

mapped as R:myfoldermyfile.exe

This folder is shared and myfile.exe is accessed by many computers at the same time.

I would like to delete the file but cant do it when it's running at any computers.

How to know if that file is still opened on any computers on the network?

Impossible to know?

Link to comment
Share on other sites

_Net_Share_FileEnum() may come in handy.

edit: in conjunction with _Net_Share_FileGetInfo(), if you need specific details.

Edited by orbs

Signature - my forum contributions:

Spoiler

UDF:

LFN - support for long file names (over 260 characters)

InputImpose - impose valid characters in an input control

TimeConvert - convert UTC to/from local time and/or reformat the string representation

AMF - accept multiple files from Windows Explorer context menu

DateDuration -  literal description of the difference between given dates

Apps:

Touch - set the "modified" timestamp of a file to current time

Show For Files - tray menu to show/hide files extensions, hidden & system files, and selection checkboxes

SPDiff - Single-Pane Text Diff

 

Link to comment
Share on other sites

FileDelete will return 0. I would search the term 'shared file in use' on google or such-like.

UDF List:

 
_AdapterConnections()_AlwaysRun()_AppMon()_AppMonEx()_ArrayFilter/_ArrayReduce_BinaryBin()_CheckMsgBox()_CmdLineRaw()_ContextMenu()_ConvertLHWebColor()/_ConvertSHWebColor()_DesktopDimensions()_DisplayPassword()_DotNet_Load()/_DotNet_Unload()_Fibonacci()_FileCompare()_FileCompareContents()_FileNameByHandle()_FilePrefix/SRE()_FindInFile()_GetBackgroundColor()/_SetBackgroundColor()_GetConrolID()_GetCtrlClass()_GetDirectoryFormat()_GetDriveMediaType()_GetFilename()/_GetFilenameExt()_GetHardwareID()_GetIP()_GetIP_Country()_GetOSLanguage()_GetSavedSource()_GetStringSize()_GetSystemPaths()_GetURLImage()_GIFImage()_GoogleWeather()_GUICtrlCreateGroup()_GUICtrlListBox_CreateArray()_GUICtrlListView_CreateArray()_GUICtrlListView_SaveCSV()_GUICtrlListView_SaveHTML()_GUICtrlListView_SaveTxt()_GUICtrlListView_SaveXML()_GUICtrlMenu_Recent()_GUICtrlMenu_SetItemImage()_GUICtrlTreeView_CreateArray()_GUIDisable()_GUIImageList_SetIconFromHandle()_GUIRegisterMsg()_GUISetIcon()_Icon_Clear()/_Icon_Set()_IdleTime()_InetGet()_InetGetGUI()_InetGetProgress()_IPDetails()_IsFileOlder()_IsGUID()_IsHex()_IsPalindrome()_IsRegKey()_IsStringRegExp()_IsSystemDrive()_IsUPX()_IsValidType()_IsWebColor()_Language()_Log()_MicrosoftInternetConnectivity()_MSDNDataType()_PathFull/GetRelative/Split()_PathSplitEx()_PrintFromArray()_ProgressSetMarquee()_ReDim()_RockPaperScissors()/_RockPaperScissorsLizardSpock()_ScrollingCredits_SelfDelete()_SelfRename()_SelfUpdate()_SendTo()_ShellAll()_ShellFile()_ShellFolder()_SingletonHWID()_SingletonPID()_Startup()_StringCompact()_StringIsValid()_StringRegExpMetaCharacters()_StringReplaceWholeWord()_StringStripChars()_Temperature()_TrialPeriod()_UKToUSDate()/_USToUKDate()_WinAPI_Create_CTL_CODE()_WinAPI_CreateGUID()_WMIDateStringToDate()/_DateToWMIDateString()Au3 script parsingAutoIt SearchAutoIt3 PortableAutoIt3WrapperToPragmaAutoItWinGetTitle()/AutoItWinSetTitle()CodingDirToHTML5FileInstallrFileReadLastChars()GeoIP databaseGUI - Only Close ButtonGUI ExamplesGUICtrlDeleteImage()GUICtrlGetBkColor()GUICtrlGetStyle()GUIEventsGUIGetBkColor()Int_Parse() & Int_TryParse()IsISBN()LockFile()Mapping CtrlIDsOOP in AutoItParseHeadersToSciTE()PasswordValidPasteBinPosts Per DayPreExpandProtect GlobalsQueue()Resource UpdateResourcesExSciTE JumpSettings INISHELLHOOKShunting-YardSignature CreatorStack()Stopwatch()StringAddLF()/StringStripLF()StringEOLToCRLF()VSCROLLWM_COPYDATAMore Examples...

Updated: 22/04/2018

Link to comment
Share on other sites

  • Moderators

Here is a snippet I use, just to show you another way. Will show who is using the file:

 

Edit - forum is hating me this morning, won't let me post code for some reason. See below in plain text.

 

$aSessions = $server.Resources

   For $element in $aSessions
      If $element.Path <> "" Then MsgBox(0, "", "Path: " & $element.Path & @CRLF & "User: " & $element.User)
   Next

Edited by JLogan3o13

"Profanity is the last vestige of the feeble mind. For the man who cannot express himself forcibly through intellect must do so through shock and awe" - Spencer W. Kimball

How to get your question answered on this forum!

Link to comment
Share on other sites

I presumed you weren't using a server, seems I was mistaken.

UDF List:

 
_AdapterConnections()_AlwaysRun()_AppMon()_AppMonEx()_ArrayFilter/_ArrayReduce_BinaryBin()_CheckMsgBox()_CmdLineRaw()_ContextMenu()_ConvertLHWebColor()/_ConvertSHWebColor()_DesktopDimensions()_DisplayPassword()_DotNet_Load()/_DotNet_Unload()_Fibonacci()_FileCompare()_FileCompareContents()_FileNameByHandle()_FilePrefix/SRE()_FindInFile()_GetBackgroundColor()/_SetBackgroundColor()_GetConrolID()_GetCtrlClass()_GetDirectoryFormat()_GetDriveMediaType()_GetFilename()/_GetFilenameExt()_GetHardwareID()_GetIP()_GetIP_Country()_GetOSLanguage()_GetSavedSource()_GetStringSize()_GetSystemPaths()_GetURLImage()_GIFImage()_GoogleWeather()_GUICtrlCreateGroup()_GUICtrlListBox_CreateArray()_GUICtrlListView_CreateArray()_GUICtrlListView_SaveCSV()_GUICtrlListView_SaveHTML()_GUICtrlListView_SaveTxt()_GUICtrlListView_SaveXML()_GUICtrlMenu_Recent()_GUICtrlMenu_SetItemImage()_GUICtrlTreeView_CreateArray()_GUIDisable()_GUIImageList_SetIconFromHandle()_GUIRegisterMsg()_GUISetIcon()_Icon_Clear()/_Icon_Set()_IdleTime()_InetGet()_InetGetGUI()_InetGetProgress()_IPDetails()_IsFileOlder()_IsGUID()_IsHex()_IsPalindrome()_IsRegKey()_IsStringRegExp()_IsSystemDrive()_IsUPX()_IsValidType()_IsWebColor()_Language()_Log()_MicrosoftInternetConnectivity()_MSDNDataType()_PathFull/GetRelative/Split()_PathSplitEx()_PrintFromArray()_ProgressSetMarquee()_ReDim()_RockPaperScissors()/_RockPaperScissorsLizardSpock()_ScrollingCredits_SelfDelete()_SelfRename()_SelfUpdate()_SendTo()_ShellAll()_ShellFile()_ShellFolder()_SingletonHWID()_SingletonPID()_Startup()_StringCompact()_StringIsValid()_StringRegExpMetaCharacters()_StringReplaceWholeWord()_StringStripChars()_Temperature()_TrialPeriod()_UKToUSDate()/_USToUKDate()_WinAPI_Create_CTL_CODE()_WinAPI_CreateGUID()_WMIDateStringToDate()/_DateToWMIDateString()Au3 script parsingAutoIt SearchAutoIt3 PortableAutoIt3WrapperToPragmaAutoItWinGetTitle()/AutoItWinSetTitle()CodingDirToHTML5FileInstallrFileReadLastChars()GeoIP databaseGUI - Only Close ButtonGUI ExamplesGUICtrlDeleteImage()GUICtrlGetBkColor()GUICtrlGetStyle()GUIEventsGUIGetBkColor()Int_Parse() & Int_TryParse()IsISBN()LockFile()Mapping CtrlIDsOOP in AutoItParseHeadersToSciTE()PasswordValidPasteBinPosts Per DayPreExpandProtect GlobalsQueue()Resource UpdateResourcesExSciTE JumpSettings INISHELLHOOKShunting-YardSignature CreatorStack()Stopwatch()StringAddLF()/StringStripLF()StringEOLToCRLF()VSCROLLWM_COPYDATAMore Examples...

Updated: 22/04/2018

Link to comment
Share on other sites

Here is a snippet I use, just to show you another way. Will show who is using the file:

 

Edit - forum is hating me this morning, won't let me post code for some reason. See below in plain text.

 

$aSessions = $server.Resources

   For $element in $aSessions

      If $element.Path <> "" Then MsgBox(0, "", "Path: " & $element.Path & @CRLF & "User: " & $element.User)

   Next

 

Thanks JLogan :)

Could you please give more complete code for that? Because I have no idea what file need to be included and where $server come from

Link to comment
Share on other sites

Not really a "server", just a network shared. Is that make any different for JLogan's snippet?

 

i just took a look inside _Net_Share_FileEnum(), i expected to see a WMI call but i found a DllCall to local netapi32.dll, which means that it should work on network shares that don't have to be hosted on Windows servers - so Linux-hosted shares (i.e. most common storage devices) should be supported.

i didn't check this, but if you have, as you say, Not really a "server", then what do you have?

and if it's not a Windows server, can you check to tell us if it works?

or has anyone else tried it already?

Signature - my forum contributions:

Spoiler

UDF:

LFN - support for long file names (over 260 characters)

InputImpose - impose valid characters in an input control

TimeConvert - convert UTC to/from local time and/or reformat the string representation

AMF - accept multiple files from Windows Explorer context menu

DateDuration -  literal description of the difference between given dates

Apps:

Touch - set the "modified" timestamp of a file to current time

Show For Files - tray menu to show/hide files extensions, hidden & system files, and selection checkboxes

SPDiff - Single-Pane Text Diff

 

Link to comment
Share on other sites

  • Moderators

Sorry, it seems that the forum hates me today, keeps stripping my code. Let's see if this gets through:

 

Edit: Not sure why it is eating all my code today.

Here is plain text:

$aSessions = $server.Resources - Array of open files in used through Shared Folders area of System Management

For $element in $aSessions
 If $element.Path <> "" Then MsgBox(0, "", "Path: " & $element.Path & @CRLF & "User: " & $element.User) - Cycle through array and output any for which the path is not empty
Next

Edit: Ah, screw it, forum does not like the first line in the script - see attached

TestForumScripts.au3

Edited by JLogan3o13

"Profanity is the last vestige of the feeble mind. For the man who cannot express himself forcibly through intellect must do so through shock and awe" - Spencer W. Kimball

How to get your question answered on this forum!

Link to comment
Share on other sites

_Net_Share_FileEnum() may come in handy.

edit: in conjunction with _Net_Share_FileGetInfo(), if you need specific details.

 

I seems can not get this to work :(

Running code below (taken from help file) always bring result:

Error ...................: 0

Entries read ............: 0

 

Tried with WINXP machine/share and FreeBSD.

Any ideas?

#include <GUIConstantsEx.au3>
#include <NetShare.au3>
#include <WindowsConstants.au3>

Global $iMemo
_Main()

Func _Main()
    Local $sServer, $aInfo

    ; Create GUI
    GUICreate("NetShare", 400, 300)

    ; Create memo control
    $iMemo = GUICtrlCreateEdit("", 2, 2, 396, 296, $WS_VSCROLL)
    GUICtrlSetFont($iMemo, 9, 400, 0, "Courier New")
    GUISetState()

    ; Get server and share information
    $sServer = InputBox("NetWork Demo", "Enter Server Name:", "\\MyServer", "", 200, 130)
    If @error Then Exit

    ; Enumerate open files on the server
    $aInfo = _Net_Share_FileEnum($sServer)
    MemoWrite("Error ...................: " & @error)
    MemoWrite("Entries read ............: " & $aInfo[0][0])
    For $iI = 1 To $aInfo[0][0]
        MemoWrite("Resource ID .............: " & $aInfo[$iI][0])
        MemoWrite("Resource permissions ....: " & _Net_Share_PermStr($aInfo[$iI][1]))
        MemoWrite("Resource locks ..........: " & $aInfo[$iI][2])
        MemoWrite("Resource path ...........: " & $aInfo[$iI][3])
        MemoWrite("Resource user ...........: " & $aInfo[$iI][4])
        MemoWrite()
    Next

    ; Loop until user exits
    Do
    Until GUIGetMsg() = $GUI_EVENT_CLOSE
EndFunc   ;==>_Main

; Write message to memo
Func MemoWrite($sMessage = "")
    GUICtrlSetData($iMemo, $sMessage & @CRLF, 1)
EndFunc   ;==>MemoWrite
Link to comment
Share on other sites

Sorry, it seems that the forum hates me today, keeps stripping my code. Let's see if this gets through:

Edit: Not sure why it is eating all my code today.

Here is plain text:

$aSessions = $server.Resources - Array of open files in used through Shared Folders area of System Management

For $element in $aSessions

 If $element.Path <> "" Then MsgBox(0, "", "Path: " & $element.Path & @CRLF & "User: " & $element.User) - Cycle through array and output any for which the path is not empty

Next

Edit: Ah, screw it, forum does not like the first line in the script - see attached

attachicon.gifTestForumScripts.au3

 

I run the TestForumScripts.au3 and it display this error:

C:UsersMSDesktopscriptsTestForumScripts.au3 (2) : ==> Variable must be of type "Object".:

$aSessions = $server.Resources

$aSessions = $server^ ERROR

This is the code

edit: seems i cant get this code to be displayed too, just like JLogan

so I attached it here

 

$aSessions = $server.Resources

For $element in $aSessions

    If $element.Path <> "" Then MsgBox(0, "", "Path: " & $element.Path & @CRLF & "User: " & $element.User)

Next

 
 

Yes, I change the "myserver" string on line 1 to actual server name

Edited by michaelslamet
Link to comment
Share on other sites

Houston, we have a problem.

 

ObjGet method

the script suggested by JLogan3o13 has it's first line thus:

 

edit: now i can't submit a tagged code too... never mind, this is it:

edit2: what the... no code in body of post? ok, so download the thing and see the first line.

 

unless i'm dreadfully mistaken, it looks for a WinNT object on a BSD server. no wonder it can not work. for now i abandon this trail.

 

_Net_Share_FileEnum() function

the comment in the help file for _Net_Share_FileEnum() says:

Only members of the Administrators or Server Operators local group can execute this function

to my testing, it seems this is wrong (although - or because ;)  - it's copied from MSDN). the word local is not correct. i can get this function working only by RunAs a server administrator on the remote Windows server (did not try on non-Windows server).

 

PsFile external utility

PsTools package has the program PsFile, which gives the same result: works when user has admin rights on remote server, "Access Denied" in any other case.

 

these are preliminary results, i will now put some research into it, as i thought of using this feature in one of my scripts, now i see i 'll be facing a brick wall if&when i get to it on my script, so i better crash my head against it right now.

if anyone has some insights on this issue, please share.

updates to follow,

orbs

edit3: meanwhile, to focus on the original purpose, you can call FileMove() on the suspected file. if success, then file is not locked. although, if it fails, this will not give you any identification of the machine/process locking the file.

Edited by orbs

Signature - my forum contributions:

Spoiler

UDF:

LFN - support for long file names (over 260 characters)

InputImpose - impose valid characters in an input control

TimeConvert - convert UTC to/from local time and/or reformat the string representation

AMF - accept multiple files from Windows Explorer context menu

DateDuration -  literal description of the difference between given dates

Apps:

Touch - set the "modified" timestamp of a file to current time

Show For Files - tray menu to show/hide files extensions, hidden & system files, and selection checkboxes

SPDiff - Single-Pane Text Diff

 

Link to comment
Share on other sites

  • Moderators

Agreed, would have been good to know the OS's involved up front, as I wouldn't expect the code I provided to work on a FreeBSD machine.

"Profanity is the last vestige of the feeble mind. For the man who cannot express himself forcibly through intellect must do so through shock and awe" - Spencer W. Kimball

How to get your question answered on this forum!

Link to comment
Share on other sites

Agreed, would have been good to know the OS's involved up front, as I wouldn't expect the code I provided to work on a FreeBSD machine.

 

Yes, I'm sorry for that :) I thought that will not make any different because they're using same protocol (SMB).

Anyway, I'm trying to connect to WinXP share, same error message.

Link to comment
Share on other sites

Houston, we have a problem.

 

ObjGet method

the script suggested by JLogan3o13 has it's first line thus:

edit: now i can't submit a tagged code too... never mind, this is it:

edit2: what the... no code in body of post? ok, so download the thing and see the first line.

 

unless i'm dreadfully mistaken, it looks for a WinNT object on a BSD server. no wonder it can not work. for now i abandon this trail.

 

_Net_Share_FileEnum() function

the comment in the help file for _Net_Share_FileEnum() says:

Only members of the Administrators or Server Operators local group can execute this function

to my testing, it seems this is wrong (although - or because ;)  - it's copied from MSDN). the word local is not correct. i can get this function working only by RunAs a server administrator on the remote Windows server (did not try on non-Windows server).

 

PsFile external utility

PsTools package has the program PsFile, which gives the same result: works when user has admin rights on remote server, "Access Denied" in any other case.

 

these are preliminary results, i will now put some research into it, as i thought of using this feature in one of my scripts, now i see i 'll be facing a brick wall if&when i get to it on my script, so i better crash my head against it right now.

if anyone has some insights on this issue, please share.

updates to follow,

orbs

edit3: meanwhile, to focus on the original purpose, you can call FileMove() on the suspected file. if success, then file is not locked. although, if it fails, this will not give you any identification of the machine/process locking the file.

 

Unfortunetely, I cant use FileMove() or FileDelete() for this purpose, because what I'm trying to do is applying self update, so I cant FileMove or FileDelete the script itself

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...