Jump to content

Script almost finished... Having trouble at FileGetTime


Recommended Posts

The script itself runs ok, however, when doing search for files created TODAY only and including it into zip archive... The script fails to do so. I probably did bad job implementing finding of today's files. When archive appears, the structure of archive is directory "logs" and in that directory all files with log extension are included. Even yesterdays one - but they shouldn't be. So once again, please help!

Been playing around for the last 2 days with this and got massive help on this forum - for which I'm VERY grateful :)

#Include <File.au3>

;constants
Dim $filename = @UserName & "." &  @MDAY & "." &  @MON & "." & @YEAR
Dim $ext = ".zip"
Dim $sharedFolder = "\\td-sys-iztok\tiger-x86"
Dim $newFilename = $filename & $ext
Dim $Shell,$srcFolder,$DestFolder,$items;Objects

;and some more constants
$Shell = ObjCreate("Shell.Application")
$Destfolder = @UserProfileDir & "\Local Settings\Temp" & "\avayalogs\" & $newFilename
$srcFolder = "C:\Program Files\Avaya\IC71\logs"
DirCreate(@UserProfileDir & "\Local Settings\Temp\avayalogs")

;compression part
InitZip($DestFolder)
$Shell.NameSpace($DestFolder).Copyhere ($srcFolder,0)
sleep(5000)

;find files created today and copy them to temp directory
$search = FileFindFirstFile("C:\Program Files\Avaya\IC71\logs\" & "*.log")
If $search = -1 Then
     Exit
EndIf

While 1
    $FileDate = FileGetTime($search, 1)
    If @MDAY Then
        FileCopy($FileDate, @UserProfileDir & "\Local Settings\Temp" & "\avayalogs")
        $file = FileFindNextFile($search)
        If @error Then ExitLoop
    EndIf
WEnd
FileClose($search)

;more compression
Func InitZip ($zip_path_name)
If NOT FileExists($zip_Path_name) then
      $init_zipString= Chr(80) & Chr(75) & Chr(5) & Chr(6);Create the Header String
        for $n =1 to 18                       ;the    
            $init_zipString= $init_zipString & Chr(0);Header    
        next
    $file =FileOpen($zip_path_name,18)                        
    FileWrite($file,$init_zipString)       ;Write the string in a file    
    FileClose($file)    
EndIf
EndFunc

;incremental file naming and copying
If FileExists($sharedFolder & "\" & $filename & $ext) Then
$num = 1
Do
$newFilename = $filename & "-" & $num & $ext
$num += 1
Until NOT FileExists($sharedFolder & "\" & $newFilename)
EndIf

FileCopy (@UserProfileDir & "\Local Settings\Temp" & "\avayalogs" & "\" & $filename & $ext, $sharedFolder & "\" & $newFilename)

;removing temporary files
FileDelete(@UserProfileDir & "\Local Settings\Temp" & "\avayalogs" & "\" & $newFilename)
DirRemove(@UserProfileDir & "\Local Settings\Temp" & "\avayalogs")
Edited by skysel
Link to comment
Share on other sites

Yeah your code is all wrong. Try this:

;find files created today and copy them to temp directory
$search = FileFindFirstFile("C:\Program Files\Avaya\IC71\logs\" & "*.log")
If $search = -1 Then
     Exit
EndIf

While 1
    $file = FileFindNextFile($search)
    If @error Then ExitLoop
    $FileDate = FileGetTime($file, 1)
    If $FileDate[2] = @MDAY Then FileCopy($file, @UserProfileDir & "\Local Settings\Temp" & "\avayalogs")
WEnd
FileClose($search)
Edited by zorphnog
Link to comment
Share on other sites

Yeah your code is all wrong. Try this:

;find files created today and copy them to temp directory
$search = FileFindFirstFile("C:\Program Files\Avaya\IC71\logs\" & "*.log")
If $search = -1 Then
     Exit
EndIf

While 1
    $file = FileFindNextFile($search)
    If @error Then ExitLoop
    $FileDate = FileGetTime($file, 1)
    If $FileDate[2] = @MDAY Then FileCopy($file, @UserProfileDir & "\Local Settings\Temp" & "\avayalogs")
WEnd
FileClose($search)
Error...

Operater-beta.au3 (33) : ==> Subscript used with non-Array variable.:

If $FileDate[2] = @MDAY Then FileCopy($file, @UserProfileDir & "\Local Settings\Temp" & "\avayalogs")

If $FileDate^ ERROR

Link to comment
Share on other sites

FileGetTime() returns an array by default, though you can change the format returned with the available parameters. In this example, it will return the created time in the format YYYYMMDDHHMMSS, then compare it to the current YYYYMMDD (just because your original code only checked @MDAY, so a file created on February 01 would return a positive match on March 01, April 01, etc).

$FileDate = FileGetTime($file,1,1)
If StringLeft($FileDate,8) = @YEAR &@MON &@MDAY Then
;filecopy yada-yada
EndIf
Edited by Monamo

- MoChr(77)& Chr(97)& Chr(100)& Chr(101)& Chr(32)& Chr(121)& Chr(97)& Chr(32)& Chr(108)& Chr(111)& Chr(111)& Chr(107)-------I've told you 100,000 times not to exaggerate!-------Don't make me hit you with my cigarette hand...-------My scripts:Random Episode Selector, Keyboard MouseMover, CopyPath v2.1, SmartRename for XP,Window Tracer[sup]New![/sup]

Link to comment
Share on other sites

Error...

Operater-beta.au3 (33) : ==> Subscript used with non-Array variable.:

If $FileDate[2] = @MDAY Then FileCopy($file, @UserProfileDir & "\Local Settings\Temp" & "\avayalogs")

If $FileDate^ ERROR

Ah ok, I forgot error checking. Try this:

$search = FileFindFirstFile("C:\Program Files\Avaya\IC71\logs\" & "*.log")
If $search = -1 Then
     Exit
EndIf

While 1
    $file = FileFindNextFile($search)
    If @error Then ExitLoop
    $FileDate = FileGetTime($file, 1)
    If Not @error Then
        If $FileDate[2] = @MDAY Then FileCopy($file, @UserProfileDir & "\Local Settings\Temp" & "\avayalogs")
    EndIf
WEnd
FileClose($search)
Edited by zorphnog
Link to comment
Share on other sites

That's better :) It runs, but not working as it should. Archive that's created contains directory logs\ and within that directory ALL files are there (yesterdays,etc,...) I'm guessing it's due to this part:

;compression part
InitZip($DestFolder)
$Shell.NameSpace($DestFolder).Copyhere ($srcFolder,0)

Any workaround?

Link to comment
Share on other sites

Well you've got all kinds of path problems, then you are creating the zip before you even search for the files, and your compressing the wrong source folder. This isn't particularly well structured code, but putting that aside this code should produce the results you are looking for.

#Include <File.au3>

;constants
Dim $filename = @UserName & "." &  @MDAY & "." &  @MON & "." & @YEAR
Dim $ext = ".zip"
Dim $sharedFolder = "\\td-sys-iztok\tiger-x86"
Dim $newFilename = $filename & $ext
Dim $Shell,$srcFolder,$zipArchive,$searchFolder,$items;Objects

;and some more constants
$Shell = ObjCreate("Shell.Application")
$srcFolder = @UserProfileDir & "\Local Settings\Temp\avayalogs"
$zipArchive = @UserProfileDir & "\Local Settings\Temp\zipout\" & $newFilename
$searchFolder = "C:\Program Files\Avaya\IC71\logs"
DirCreate($srcFolder)
DirCreate(@UserProfileDir & "\Local Settings\Temp\zipout")

;find files created today and copy them to temp directory
$search = FileFindFirstFile($searchFolder & "\" & "*.log")
If $search = -1 Then
     Exit
EndIf

While 1
    $file = FileFindNextFile($search)
    If @error Then ExitLoop
    $FileDate = FileGetTime($file, 1)
    If Not @error Then
        If $FileDate[2] = @MDAY Then FileCopy($file, $srcFolder)
    EndIf
WEnd
FileClose($search)

;compression part
InitZip($zipArchive)
$Shell.NameSpace($zipArchive).Copyhere ($srcFolder,0)
sleep(5000)

;more compression
Func InitZip ($zip_path_name)
    If NOT FileExists($zip_Path_name) then
          $init_zipString= Chr(80) & Chr(75) & Chr(5) & Chr(6);Create the Header String
            for $n =1 to 18                       ;the   
                $init_zipString= $init_zipString & Chr(0);Header   
            next
        $file =FileOpen($zip_path_name,18)                       
        FileWrite($file,$init_zipString)       ;Write the string in a file   
        FileClose($file)   
    EndIf
EndFunc

;incremental file naming and copying
If FileExists($sharedFolder & "\" & $filename & $ext) Then
$num = 1
Do
$newFilename = $filename & "-" & $num & $ext
$num += 1
Until NOT FileExists($sharedFolder & "\" & $newFilename)
EndIf

FileCopy ($zipArchive, $sharedFolder & "\" & $newFilename)

;removing temporary files
DirRemove($srcFolder, 1)
DirRemove(@UserProfileDir & "\Local Settings\Temp\zipout", 1)
Link to comment
Share on other sites

Unfortunatley, this code doesn't work. When running it, it says that "avayalogs" directory is empty, tehrefor it cannot compress anything. And yes, there is file created today in program files directory. Have we missed something? :)

Well you've got all kinds of path problems, then you are creating the zip before you even search for the files, and your compressing the wrong source folder. This isn't particularly well structured code, but putting that aside this code should produce the results you are looking for.

#Include <File.au3>

;constants
Dim $filename = @UserName & "." &  @MDAY & "." &  @MON & "." & @YEAR
Dim $ext = ".zip"
Dim $sharedFolder = "\\td-sys-iztok\tiger-x86"
Dim $newFilename = $filename & $ext
Dim $Shell,$srcFolder,$zipArchive,$searchFolder,$items;Objects

;and some more constants
$Shell = ObjCreate("Shell.Application")
$srcFolder = @UserProfileDir & "\Local Settings\Temp\avayalogs"
$zipArchive = @UserProfileDir & "\Local Settings\Temp\zipout\" & $newFilename
$searchFolder = "C:\Program Files\Avaya\IC71\logs"
DirCreate($srcFolder)
DirCreate(@UserProfileDir & "\Local Settings\Temp\zipout")

;find files created today and copy them to temp directory
$search = FileFindFirstFile($searchFolder & "\" & "*.log")
If $search = -1 Then
     Exit
EndIf

While 1
    $file = FileFindNextFile($search)
    If @error Then ExitLoop
    $FileDate = FileGetTime($file, 1)
    If Not @error Then
        If $FileDate[2] = @MDAY Then FileCopy($file, $srcFolder)
    EndIf
WEnd
FileClose($search)

;compression part
InitZip($zipArchive)
$Shell.NameSpace($zipArchive).Copyhere ($srcFolder,0)
sleep(5000)

;more compression
Func InitZip ($zip_path_name)
    If NOT FileExists($zip_Path_name) then
          $init_zipString= Chr(80) & Chr(75) & Chr(5) & Chr(6);Create the Header String
            for $n =1 to 18                       ;the   
                $init_zipString= $init_zipString & Chr(0);Header   
            next
        $file =FileOpen($zip_path_name,18)                       
        FileWrite($file,$init_zipString)       ;Write the string in a file   
        FileClose($file)   
    EndIf
EndFunc

;incremental file naming and copying
If FileExists($sharedFolder & "\" & $filename & $ext) Then
$num = 1
Do
$newFilename = $filename & "-" & $num & $ext
$num += 1
Until NOT FileExists($sharedFolder & "\" & $newFilename)
EndIf

FileCopy ($zipArchive, $sharedFolder & "\" & $newFilename)

;removing temporary files
DirRemove($srcFolder, 1)
DirRemove(@UserProfileDir & "\Local Settings\Temp\zipout", 1)
Link to comment
Share on other sites

Unfortunatley, this code doesn't work. When running it, it says that "avayalogs" directory is empty, tehrefor it cannot compress anything. And yes, there is file created today in program files directory. Have we missed something? :)

Well, I can't debug your specific situation, but you can add some ConsoleWrite calls to track whats happening. Apparently you need to check that the directory isn't empty before you attempt to compress it as well. Here's a start:

#Include <File.au3>

;constants
Dim $filename = @UserName & "." &  @MDAY & "." &  @MON & "." & @YEAR
Dim $ext = ".zip"
Dim $sharedFolder = "\\td-sys-iztok\tiger-x86"
Dim $newFilename = $filename & $ext
Dim $Shell,$srcFolder,$zipArchive,$searchFolder,$items;Objects

;and some more constants
$srcFolder = @UserProfileDir & "\Local Settings\Temp\avayalogs"
$zipArchive = @UserProfileDir & "\Local Settings\Temp\zipout\" & $newFilename
$searchFolder = "C:\Program Files\Avaya\IC71\logs"
DirCreate($srcFolder)
DirCreate(@UserProfileDir & "\Local Settings\Temp\zipout")

;find files created today and copy them to temp directory
$search = FileFindFirstFile($searchFolder & "\" & "*.log")
If $search = -1 Then
     Exit
EndIf

While 1
    $file = FileFindNextFile($search)
    If @error Then ExitLoop
    $FileDate = FileGetTime($file, 1)
    If Not @error Then
        If $FileDate[2] = @MDAY Then
            FileCopy($file, $srcFolder)
            ConsoleWrite("Copied file '" & $file & "' to temp directory" & @CRLF)
        EndIf
    EndIf
WEnd
FileClose($search)

;compression part
If FileExists($srcFolder & "\*.*") Then
    $Shell = ObjCreate("Shell.Application")
    InitZip($zipArchive)
    $Shell.NameSpace($zipArchive).Copyhere ($srcFolder,0)
   sleep(5000)
EndIf

;more compression
Func InitZip ($zip_path_name)
    If NOT FileExists($zip_Path_name) then
          $init_zipString= Chr(80) & Chr(75) & Chr(5) & Chr(6);Create the Header String
            for $n =1 to 18                       ;the   
                $init_zipString= $init_zipString & Chr(0);Header   
            next
        $file =FileOpen($zip_path_name,18)                       
        FileWrite($file,$init_zipString)       ;Write the string in a file   
        FileClose($file)   
    EndIf
EndFunc

;incremental file naming and copying
If FileExists($sharedFolder & "\" & $filename & $ext) Then
    $num = 1
    Do
        $newFilename = $filename & "-" & $num & $ext
        $num += 1
    Until NOT FileExists($sharedFolder & "\" & $newFilename)
EndIf

FileCopy ($zipArchive, $sharedFolder & "\" & $newFilename)

;removing temporary files
DirRemove($srcFolder, 1)
DirRemove(@UserProfileDir & "\Local Settings\Temp\zipout", 1)
Link to comment
Share on other sites

I finnaly got the script working (with huge help from you guys). However it has 2 glitches.

1. copies also yesterday's log files along with today. should copy only today's files.

2. if user with administrative rights runs the script it works perfectly. if a normal user runs it, it says that the directory avayalogs is empty and therefor it cannot compress anything.

Help again :)

#Include <File.au3>
#include <Array.au3>
;constants
Dim $filename = @UserName & "." &  @MDAY & "." &  @MON & "." & @YEAR
Dim $ext = ".zip"
Dim $sharedFolder = "\\vd-cc-mihav\AIC log collect"
Dim $newFilename = $filename & $ext
Dim $Shell,$srcFolder,$DestFolder,$items;Objects
Dim $FileList
Dim $FileTime
Dim $i

;and some more constants
DirCreate(@UserProfileDir & "\Local Settings\Temp\avayalogs")
DirCreate(@UserProfileDir & "\Local Settings\Temp\zipout")
$Destfolder = @UserProfileDir & "\Local Settings\Temp" & "\avayalogs\"
$zipout = @UserProfileDir & "\Local Settings\Temp" & "\zipout\" & $newFilename
$srcFolder = "C:\Program Files\Avaya\IC71\logs"


;find files created today and copy them to temp directory

$FileList = _FileListToArray($srcfolder)

For $i = 1 To UBound($FileList) - 1
    $FileTime = FileGetTime($srcfolder & "\" & $FileList[$i], 1)
    If $FileTime[0] = @YEAR And $FileTime[1] = @MON And $FileTime[2] = @MDAY Then
        FileCopy($srcfolder & "\" & $FileList[$i], @UserProfileDir & "\Local Settings\Temp" & "\avayalogs")
    EndIf
Next

;compression part
$Shell = ObjCreate("Shell.Application")
InitZip($zipout)
$Shell.NameSpace($zipout).Copyhere ($destfolder,0)

sleep(5000)

;more compression
Func InitZip ($zip_path_name)
If NOT FileExists($zip_Path_name) then
      $init_zipString= Chr(80) & Chr(75) & Chr(5) & Chr(6)
        for $n =1 to 18                         
            $init_zipString= $init_zipString & Chr(0)    
        next
    $file =FileOpen($zip_path_name,18)                        
    FileWrite($file,$init_zipString)        
    FileClose($file)    
EndIf
EndFunc

;incremental file naming and copying
If FileExists($sharedFolder & "\" & $filename & $ext) Then
$num = 1
Do
$newFilename = $filename & "-" & $num & $ext
$num += 1
Until NOT FileExists($sharedFolder & "\" & $newFilename)
EndIf

FileCopy (@UserProfileDir & "\Local Settings\Temp" & "\zipout" & "\" & $filename & $ext, $sharedFolder & "\" & $newFilename)

;removing temporary files
FileDelete(@UserProfileDir & "\Local Settings\Temp" & "\avayalogs" & "\" & $newFilename)
FileDelete(@UserProfileDir & "\Local Settings\Temp" & "\avayalogs" & "\" & "*.log")
DirRemove(@UserProfileDir & "\Local Settings\Temp" & "\avayalogs\")
FileDelete(@UserProfileDir & "\Local Settings\Temp" & "\zipout" & "\" & "*.zip")
DirRemove(@UserProfileDir & "\Local Settings\Temp" & "\zipout\")
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...