Sign in to follow this  
Followers 0
vcivilen

File detection

21 posts in this topic

Hello,

I want to write a program that will do an action if my file at C:\file.txt has any change.

Please give me some ideas.

Thanks,

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

Hello,

I want to write a program that will do an action if my file at C:\file.txt has any change.

Please give me some ideas.

Thanks,

Hi,

Use All file get functions : FileGetAttrib ; FileGetSize ; FileGetTime ; FileGetVersion

Then you check if any thing have changed :

;Check First time
FileWrite("C:\test.txt", "")
$1Attrib = FileGetAttrib("C:\test.txt");Check Attrib
$1Size = FileGetSize("C:\test.txt");Check Size
$1Time = FileGetTime("C:\test.txt");Check Time
$Time1 = $1Time[2] & "/" & $1Time[1] & "/" & $1Time[0]& " " & $1Time[3]& ":" & $1Time[4]& ":" & $1Time[5]

Sleep(1000)

;Check Second time
FileWrite("C:\test.txt", "file changed");write something to file
FileSetAttrib("C:\test.txt", "+HO")
$2Attrib = FileGetAttrib("C:\test.txt");Check Attrib
$2Size = FileGetSize("C:\test.txt");Check Size
$2Time = FileGetTime("C:\test.txt");Check Time
$Time2 = $2Time[2] & "/" & $2Time[1] & "/" & $2Time[0]& " " & $2Time[3]& ":" & $2Time[4]& ":" & $2Time[5]


If $1Attrib <> $2Attrib Then
    MsgBox(64, "", "Attrib has changed !" & @CRLF & "Before : " & $1Attrib & @CRLF & "After : " & $2Attrib)
EndIf

If $1Size <> $2Size Then
    MsgBox(64, "", "Size has changed !" & @CRLF & "Before : " & $1Size & @CRLF & "After : " & $2Size)
EndIf

If $Time1 <> $Time2 Then
    MsgBox(64, "", "Time has changed !" & @CRLF & "Before : " & $Time1 & @CRLF & "After : " & $Time2)
EndIf

FileDelete("C:\test.txt")
Edited by FireFox

 

OS : Win XP SP2 (32 bits) / Win 7 SP1 (64 bits) / Win 8 (64 bits) | Autoit version: latest stable / beta.
Hardware : Intel(R) Core(TM) i5-2400 CPU @ 3.10Ghz / 8 GiB RAM DDR3.

My UDFs : Skype UDF | TrayIconEx UDF | GUI Panel UDF | Excel XML UDF | Is_Pressed_UDF

My Projects : YouTube Multi-downloader | FTP Easy-UP | Lock'n | WinKill | AVICapture | Skype TM | Tap Maker | ShellNew | Scriptner | Const Replacer | FT_Pocket | Chrome theme maker

My Examples : Capture toolIP Camera | Crosshair | Draw Captured Region | Picture Screensaver | Jscreenfix | Drivetemp | Picture viewer

My Snippets : Basic TCP | Systray_GetIconIndex | Intercept End task | Winpcap various | Advanced HotKeySet | Transparent Edit control

 

Share this post


Link to post
Share on other sites

In this case, you have know this file was changed. But I want to run my program with windows, and always detect this file. Any change of this file, my program will do an action.

Please give me more suggestion,

Thanks

Share this post


Link to post
Share on other sites

;Run Start-up
RegWrite("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Run", "File Checker", "REG_SZ", @ScriptFullPath)
;Delete Start-up
;~ FileDelete("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Run", "File Checker")


If Not FileExists(@TempDir&"\FileChecker.txt") Then;Check if file not exists
;Check First time
$Attrib = FileGetAttrib("C:\test.txt");Check Attrib
$Size = FileGetSize("C:\test.txt");Check Size
$Time = FileGetTime("C:\test.txt");Check Time
$Time = $Time[2] & "/" & $Time[1] & "/" & $Time[0]& " " & $Time[3]& ":" & $Time[4]& ":" & $Time[5]

FileWrite(@TempDir&"\FileChecker.txt",$Attrib);Write first check change
FileWrite(@TempDir&"\FileChecker.txt",@CRLF&$Size)
FileWrite(@TempDir&"\FileChecker.txt",@CRLF&$Time)
EndIf

;Read First Check change
$Attrib1 = FileReadLine(@TempDir&"\FileChecker.txt",1)
$Size1 = FileReadLine(@TempDir&"\FileChecker.txt",2)
$Time1 = FileReadLine(@TempDir&"\FileChecker.txt",3)

;Check everytime
$Attrib2 = FileGetAttrib("C:\test.txt");Check Attrib
$Size2 = FileGetSize("C:\test.txt");Check Size
$Time2 = FileGetTime("C:\test.txt");Check Time
$2Time = $Time2[2] & "/" & $Time2[1] & "/" & $Time2[0]& " " & $Time2[3]& ":" & $Time2[4]& ":" & $Time2[5]

If $Attrib1 <> $Attrib2 Then
    MsgBox(64, "", "Attrib has changed !" & @CRLF & "Before : " & $1Attrib & @CRLF & "After : " & $2Attrib)
EndIf

If $Size1 <> $Size2 Then
    MsgBox(64, "", "Size has changed !" & @CRLF & "Before : " & $1Size & @CRLF & "After : " & $2Size)
EndIf

If $Time1 <> $Time2 Then
    MsgBox(64, "", "Time has changed !" & @CRLF & "Before : " & $Time1 & @CRLF & "After : " & $Time2)
EndIf

;Delete Temp File
;~ FileDelete(@TempDir&"\FileChecker.txt")


 

OS : Win XP SP2 (32 bits) / Win 7 SP1 (64 bits) / Win 8 (64 bits) | Autoit version: latest stable / beta.
Hardware : Intel(R) Core(TM) i5-2400 CPU @ 3.10Ghz / 8 GiB RAM DDR3.

My UDFs : Skype UDF | TrayIconEx UDF | GUI Panel UDF | Excel XML UDF | Is_Pressed_UDF

My Projects : YouTube Multi-downloader | FTP Easy-UP | Lock'n | WinKill | AVICapture | Skype TM | Tap Maker | ShellNew | Scriptner | Const Replacer | FT_Pocket | Chrome theme maker

My Examples : Capture toolIP Camera | Crosshair | Draw Captured Region | Picture Screensaver | Jscreenfix | Drivetemp | Picture viewer

My Snippets : Basic TCP | Systray_GetIconIndex | Intercept End task | Winpcap various | Advanced HotKeySet | Transparent Edit control

 

Share this post


Link to post
Share on other sites

There is a WMI action to check if files have been modified... in about 5 lines of code... But i can't find the link. Try searching :)


Some Projects:[list][*]ZIP UDF using no external files[*]iPod Music Transfer [*]iTunes UDF - fully integrate iTunes with au3[*]iTunes info (taskbar player hover)[*]Instant Run - run scripts without saving them before :)[*]Get Tube - YouTube Downloader[*]Lyric Finder 2 - Find Lyrics to any of your song[*]DeskBox - A Desktop Extension Tool[/list]indifference will ruin the world, but in the end... WHO CARES :P---------------http://torels.altervista.org

Share this post


Link to post
Share on other sites

$Path = "C:\Folder1\Folder2\Folder3"
$split = Stringsplit($Path,"\")
$Drive = $split[1]
$Folders = Stringsplit(sPath,":")
$Folders = StringReplace($Folders[2], "\", "\\") & "\\"

$objWMIService = ObjGet("winmgmts:\\" & @ComputerName & "\root\cimv2")
$colMonitoredEvents = $objWMIService.ExecNotificationQuery ("SELECT * FROM __InstanceModificationEvent WITHIN 1 WHERE " & "TargetInstance ISA 'CIM_DataFile' AND " & "TargetInstance.Drive='" & $Drive & "' AND " & "TargetInstance.Path='" & $Folders & "'")

ConsoleWrite("Begin Monitoring for a Folder Change Event...")

While 1
$objLatestEvent = $colMonitoredEvents.NextEvent
Switch $objLatestEvent.Path_.Class
Case "__InstanceModificationEvent"
If $objLatestEvent.TargetInstance.LastModified <> $objLatestEvent.PreviousInstance.LastModified then
    ConsoleWrite($objLatestEvent.TargetInstance.FileName & " was modified")
EndIf
EndSwitch
Wend

$objWMIService = ""
$colMonitoredEvents = ""
$objLatestEvent = ""

Should Work!!


Some Projects:[list][*]ZIP UDF using no external files[*]iPod Music Transfer [*]iTunes UDF - fully integrate iTunes with au3[*]iTunes info (taskbar player hover)[*]Instant Run - run scripts without saving them before :)[*]Get Tube - YouTube Downloader[*]Lyric Finder 2 - Find Lyrics to any of your song[*]DeskBox - A Desktop Extension Tool[/list]indifference will ruin the world, but in the end... WHO CARES :P---------------http://torels.altervista.org

Share this post


Link to post
Share on other sites

#7 ·  Posted (edited)

CODE
$Path = "C:\Folder1\Folder2\Folder3"

$split = Stringsplit($Path,"\")

$Drive = $split[1]

$Folders = Stringsplit(sPath,":")

$Folders = StringReplace($Folders[2], "\", "\\") & "\\"

$objWMIService = ObjGet("winmgmts:\\" & @ComputerName & "\root\cimv2")

$colMonitoredEvents = $objWMIService.ExecNotificationQuery ("SELECT * FROM __InstanceModificationEvent WITHIN 1 WHERE " & "TargetInstance ISA 'CIM_DataFile' AND " & "TargetInstance.Drive='" & $Drive & "' AND " & "TargetInstance.Path='" & $Folders & "'")

ConsoleWrite("Begin Monitoring for a Folder Change Event...")

While 1

$objLatestEvent = $colMonitoredEvents.NextEvent

Switch $objLatestEvent.Path_.Class

Case "__InstanceModificationEvent"

If $objLatestEvent.TargetInstance.LastModified <> $objLatestEvent.PreviousInstance.LastModified then

ConsoleWrite($objLatestEvent.TargetInstance.FileName & " was modified")

EndIf

EndSwitch

Wend

$objWMIService = ""

$colMonitoredEvents = ""

$objLatestEvent = ""

Should Work!!

But doesn't! :)

Just needed some tweaking, though. This version monitors Creation, Deletion, and Modification events:

HotKeySet("{ESC}", "_Quit")

$sPath = "C:\Temp\Testing"
$split = StringSplit($sPath, "\")
$Drive = $split[1]
$Folders = StringSplit($sPath, ":")
$Folders = StringReplace($Folders[2], "\", "\\") & "\\"

$objWMIService = ObjGet("winmgmts:\\" & @ComputerName & "\root\cimv2")
$colMonitoredEvents = $objWMIService.ExecNotificationQuery("SELECT * FROM __InstanceOperationEvent " & _
        "WITHIN 1 WHERE TargetInstance ISA 'CIM_DataFile' AND " & "TargetInstance.Drive='" & $Drive & _
        "' AND " & "TargetInstance.Path='" & $Folders & "'")

ConsoleWrite("Begin Monitoring for a Folder Change Event..." & @LF)

While 1
    $objLatestEvent = $colMonitoredEvents.NextEvent
    Switch $objLatestEvent.Path_.Class
    ; Created
        Case "__InstanceCreationEvent"
            ConsoleWrite($objLatestEvent.TargetInstance.FileName & "." & _
                    $objLatestEvent.TargetInstance.Extension & " was created" & @LF)
            
    ; Deleted
        Case "__InstanceDeletionEvent"
            ConsoleWrite($objLatestEvent.TargetInstance.FileName & "." & _
                    $objLatestEvent.TargetInstance.Extension & " was deleted" & @LF)
            
    ; Modified
        Case "__InstanceModificationEvent"
            If $objLatestEvent.TargetInstance.LastModified <> $objLatestEvent.PreviousInstance.LastModified Then
                ConsoleWrite($objLatestEvent.TargetInstance.FileName & "." & _
                        $objLatestEvent.TargetInstance.Extension & " was modified" & @LF)
            EndIf
    EndSwitch
WEnd

Func _Quit()
    $objWMIService = ""
    $colMonitoredEvents = ""
    $objLatestEvent = ""
    Exit
EndFunc;==>_Quit

The script is blocked at $colMonitoredEvents.NextEvent, so if you hit ESC to exit, the HotKeySet() doesn't react until the next monitored event occurs.

I wonder if this can be done as an AutoIt COM event...?

:o

P.S. I wanted the file extension on the file name, but it doesn't work. Wassupwidat?

Nevermind, must have been a typo. All I did was reformat the line to avoid line wrap, and now the extension works.

:D

Edited by PsaltyDS

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law

Share this post


Link to post
Share on other sites

Sorry I am very new in AutoIT, with PsaltyDS's one I do not know how to use it (just set path C:/test.txt but not successful)

Please give me in example.

Thanks,

Share this post


Link to post
Share on other sites

Sorry I am very new in AutoIT, with PsaltyDS's one I do not know how to use it (just set path C:/test.txt but not successful)

Please give me in example.

Thanks,

Should be:

C:\test.txt


Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Share this post


Link to post
Share on other sites

Yes C:\test.txt. But not successful.

Please give me in example.

Thanks,

Share this post


Link to post
Share on other sites

Yes C:\test.txt. But not successful.

Please give me in example.

Thanks,

The script monitors a folder, not a file. You could monitor C:\ and changes to C:\test.txt (or any other file in C:\) will be listed.

:)


Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law

Share this post


Link to post
Share on other sites

Could you please give me solution to monitor file instead of folders?

Thanks,

Share this post


Link to post
Share on other sites

#13 ·  Posted (edited)

monitor the folder

if the filename is different from the file you want to monitor, ignore it

otherwise do what you need to do with the changed file

:)

Edited by torels

Some Projects:[list][*]ZIP UDF using no external files[*]iPod Music Transfer [*]iTunes UDF - fully integrate iTunes with au3[*]iTunes info (taskbar player hover)[*]Instant Run - run scripts without saving them before :)[*]Get Tube - YouTube Downloader[*]Lyric Finder 2 - Find Lyrics to any of your song[*]DeskBox - A Desktop Extension Tool[/list]indifference will ruin the world, but in the end... WHO CARES :P---------------http://torels.altervista.org

Share this post


Link to post
Share on other sites

#15 ·  Posted (edited)

I tried http://www.autoitscript.com/forum/index.php?showtopic=69044, but the given script not working. The last one did, but I want to program without GUI (so that I can run it with windows startup without logon).

PsaltyDS's one not work properly too.

Please give me more solution?

Thanks,

Edited by vcivilen

Share this post


Link to post
Share on other sites

#16 ·  Posted (edited)

@vcivilen

Why dont you use my ?

#Region THIS CAN BE CHANGED !
;Run Start-up
 RegWrite("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Run", "File Checker", "REG_SZ", @ScriptFullPath)
;Delete Start-up
;~ RegDelete("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Run", "File Checker")
 #EndRegion THIS CAN BE CHANGED !
 
 If Not FileExists(@TempDir & "\FileChecker.txt") Then;Check if file not exists
;Check First time
     $File = FileOpenDialog("File Checker", "", "All Files (*.*)", 1 + 2)
     $Attrib = FileGetAttrib($File);Check Attrib
     $Size = FileGetSize($File);Check Size
     $Time = FileGetTime($File);Check Time
     $Time = $Time[2] & "/" & $Time[1] & "/" & $Time[0] & " " & $Time[3] & ":" & $Time[4] & ":" & $Time[5]
 
     FileWrite(@TempDir & "\FileChecker.txt", $File);Write file path to check
     FileWrite(@TempDir & "\FileChecker.txt", @CRLF & $Attrib);Write first check change
     FileWrite(@TempDir & "\FileChecker.txt", @CRLF & $Size);....
     FileWrite(@TempDir & "\FileChecker.txt", @CRLF & $Time)
 Else
;Read First Check change
 $File1 = FileReadLine(@TempDir & "\FileChecker.txt", 1)
 $Attrib1 = FileReadLine(@TempDir & "\FileChecker.txt", 2)
 $Size1 = FileReadLine(@TempDir & "\FileChecker.txt", 3)
 $Time1 = FileReadLine(@TempDir & "\FileChecker.txt", 4)
 
;Check everytime
 $Attrib2 = FileGetAttrib($File1);Check Attrib
 $Size2 = FileGetSize($File1);Check Size
 $Time2 = FileGetTime($File1);Check Time
 $2Time = $Time2[2] & "/" & $Time2[1] & "/" & $Time2[0] & " " & $Time2[3] & ":" & $Time2[4] & ":" & $Time2[5]
 
 #Region THIS CAN BE CHANGED !
 If $Attrib1 <> $Attrib2 Then
     MsgBox(64, "", "Attrib has changed !" & @CRLF & "Before : " & $Attrib1 & @CRLF & "After : " & $Attrib2)
 EndIf
 
 If $Size1 <> $Size2 Then
     MsgBox(64, "", "Size has changed !" & @CRLF & "Before : " & $Size1 & @CRLF & "After : " & $Size2)
 EndIf
 
 If $Time1 <> $2Time Then
     MsgBox(64, "", "Time has changed !" & @CRLF & "Before : " & $Time1 & @CRLF & "After : " & $2Time)
 EndIf
 
;Delete Temp File
;~ FileDelete(@TempDir&"\FileChecker.txt")
 EndIf
 #EndRegion THIS CAN BE CHANGED !
Edited by FireFox

 

OS : Win XP SP2 (32 bits) / Win 7 SP1 (64 bits) / Win 8 (64 bits) | Autoit version: latest stable / beta.
Hardware : Intel(R) Core(TM) i5-2400 CPU @ 3.10Ghz / 8 GiB RAM DDR3.

My UDFs : Skype UDF | TrayIconEx UDF | GUI Panel UDF | Excel XML UDF | Is_Pressed_UDF

My Projects : YouTube Multi-downloader | FTP Easy-UP | Lock'n | WinKill | AVICapture | Skype TM | Tap Maker | ShellNew | Scriptner | Const Replacer | FT_Pocket | Chrome theme maker

My Examples : Capture toolIP Camera | Crosshair | Draw Captured Region | Picture Screensaver | Jscreenfix | Drivetemp | Picture viewer

My Snippets : Basic TCP | Systray_GetIconIndex | Intercept End task | Winpcap various | Advanced HotKeySet | Transparent Edit control

 

Share this post


Link to post
Share on other sites

I tried http://www.autoitscript.com/forum/index.php?showtopic=69044, but the given script not working. The last one did, but I want to program without GUI (so that I can run it with windows startup without logon).

PsaltyDS's one not work properly too.

Please give me more solution?

Thanks,

You're really not that lazy right? The whole idea is for you go to get off your ass and create a solution for your specific issue.

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Share this post


Link to post
Share on other sites

@Firefox: Yes, I tried, but I dont know how to ";Check everytime", and can It provide me a instant monitor?

@SmOke N: I know this thread say about script that I need, but I wonder how can it use without GUI (so that I can run it with windows startup without logon).

Thanks,

Share this post


Link to post
Share on other sites

#19 ·  Posted (edited)

@vcivilen

Everything is explain in example...

you check 1st time :

$File = FileOpenDialog("File Checker", "", "All Files (*.*)", 1 + 2)
      $Attrib = FileGetAttrib($File);Check Attrib
      $Size = FileGetSize($File);Check Size
      $Time = FileGetTime($File);Check Time
      $Time = $Time[2] & "/" & $Time[1] & "/" & $Time[0] & " " & $Time[3] & ":" & $Time[4] & ":" & $Time[5]
 
      FileWrite(@TempDir & "\FileChecker.txt", $File);Write file path to check
      FileWrite(@TempDir & "\FileChecker.txt", @CRLF & $Attrib);Write first check change
      FileWrite(@TempDir & "\FileChecker.txt", @CRLF & $Size);....
      FileWrite(@TempDir & "\FileChecker.txt", @CRLF & $Time)

Then you check when you want :

;Check everytime
$File1 = FileReadLine(@TempDir & "\FileChecker.txt", 1)
 $Attrib1 = FileReadLine(@TempDir & "\FileChecker.txt", 2)
 $Size1 = FileReadLine(@TempDir & "\FileChecker.txt", 3)
 $Time1 = FileReadLine(@TempDir & "\FileChecker.txt", 4)

  $Attrib2 = FileGetAttrib($File1);Check Attrib
  $Size2 = FileGetSize($File1);Check Size
  $Time2 = FileGetTime($File1);Check Time
  $2Time = $Time2[2] & "/" & $Time2[1] & "/" & $Time2[0] & " " & $Time2[3] & ":" & $Time2[4] & ":" & $Time2[5]
  
  #Region THIS CAN BE CHANGED !
  If $Attrib1 <> $Attrib2 Then
       MsgBox(64, "", "Attrib has changed !" & @CRLF & "Before : " & $Attrib1 & @CRLF & "After : " & $Attrib2)
  EndIf
  
  If $Size1 <> $Size2 Then
       MsgBox(64, "", "Size has changed !" & @CRLF & "Before : " & $Size1 & @CRLF & "After : " & $Size2)
  EndIf
  
  If $Time1 <> $2Time Then
       MsgBox(64, "", "Time has changed !" & @CRLF & "Before : " & $Time1 & @CRLF & "After : " & $2Time)
  EndIf
  
;Delete Temp File
;~ FileDelete(@TempDir&"\FileChecker.txt")
  EndIf
Edited by FireFox

 

OS : Win XP SP2 (32 bits) / Win 7 SP1 (64 bits) / Win 8 (64 bits) | Autoit version: latest stable / beta.
Hardware : Intel(R) Core(TM) i5-2400 CPU @ 3.10Ghz / 8 GiB RAM DDR3.

My UDFs : Skype UDF | TrayIconEx UDF | GUI Panel UDF | Excel XML UDF | Is_Pressed_UDF

My Projects : YouTube Multi-downloader | FTP Easy-UP | Lock'n | WinKill | AVICapture | Skype TM | Tap Maker | ShellNew | Scriptner | Const Replacer | FT_Pocket | Chrome theme maker

My Examples : Capture toolIP Camera | Crosshair | Draw Captured Region | Picture Screensaver | Jscreenfix | Drivetemp | Picture viewer

My Snippets : Basic TCP | Systray_GetIconIndex | Intercept End task | Winpcap various | Advanced HotKeySet | Transparent Edit control

 

Share this post


Link to post
Share on other sites

#20 ·  Posted (edited)

@Firefox: I know I can check when I want, but if I want realtime monitor, your script not meet it (because it have to use loop to check for a interval time). Thank you very much about your suggestion. I think http://www.autoitscript.com/forum/index.php?showtopic=69044 is good for me.

The last post of http://www.autoitscript.com/forum/index.php?showtopic=69044 show me the way to working without GUI, so it can be work properly.

Thank to all,

Edited by vcivilen

Share this post


Link to post
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
Sign in to follow this  
Followers 0