Jump to content

Write files being copied to log file (logging)


Go to solution Solved by orbs,

Recommended Posts

Hi all

I am stuck at a process of making a log file for my application, which will copy some files from a folder to another. I know how to make the copy work, but I want to write every file name being copied to my copy.log file

Let me give you an example:

; Source and destination
$pSource = C:\Path\To\Source
$pDest = D:\Path\To\Dest

$oCopy = Run(@ComSpec " /c copy " & $pSource & " " & $pDest, @SW_HIDE)

For $Files in $pSource

  LockWrite($Files & @CRLF)

Next

MsgBox(0, "CopyGUI", "Done")

I know that the code example above is not a good start, but I simply don't know what is needed to be done.

Thanks in advance

Link to post
Share on other sites

Allright, here is what Is working so far 

; Program:          OSDLoader
; Programmer:       Daniel Østergaard Nielsen
; Version:          1.0
; Date:             May 6, 2014

; Includes
#include "LockFile.au3"
#include "Date.au3"
#include "Inet.au3"
#include "File.au3"

; Variables
Global $hOSDLoader = GUICreate("OSDGUI", 468, 80, -1, -1, 0x80000000+0x00400000)

; Call main function
Main()

Func Main()

   ; Set theme app properties
   DllCall("uxtheme.dll", "none", "SetThemeAppProperties", "int", 1)

   ; Hide tray
   TraySetState(2)

   ; GUI Controls
   Global $oLogo = GUICtrlCreateIcon("logo.ico", "", 8, 8, 64, 64)
   Global $oLabelTitle = GUICtrlCreateLabel("OS Deployment", 88, 9, 300, 20)
   Global $oLabelText = GUICtrlCreateLabel("", 88, 30, 450, 20)
   Global $aLabelText[2] = ["Please wait while WinPE initializes your system", "Waiting for network connectivity"]

   ; Set font types
   GUICtrlSetFont($oLabelTitle, 12, 600, Default, "Microsoft Sans", 0)
   GUICtrlSetFont($oLabelText, 10, 300, Default, "Arial Sans Serif", 0)

   ; Call OSDLoader
   OSDLoader()

   ; Wait for exit code
   While 1

      Switch GUIGetMsg()

         Case -3

            ExitLoop

      EndSwitch

   WEnd

EndFunc

; OSDLoader
Func OSDLoader()

   ; Path to osd logs and configuration files
   Global $OSDLog = @ScriptDir & "\OSDLoader.log"
   Global $OSDConf = @ScriptDir & "\OSDConfig.ini"

   ; Check if log file exists
   If FileExists($OSDLog) Then

      ; Clear file content
      $hLog = FileOpen($OSDLog, 2)
      FileWrite($hLog, "")
      FileClose($hLog)

   EndIf

   ; Lock osd logs to current process
   Global $hLock = LockFile($OSDLog, 1)

   ; Show gui
   GUISetState(@SW_SHOW, $hOSDLoader)

   ; Start initialization process
   GUICtrlSetData($oLabelText, $aLabelText[0]) ; Display current process on screen
   LockWrite($hLock, _NowDate() & " " & _NowTime() & " " & "OSDLoader (1.0)" & @CRLF) ; Write status to log file

   Sleep(250)

   LockWrite($hLock, _NowDate() & " " & _NowTime() & " " & "Launch process 'x:\windows\system32\wpeutil InitializeNetwork'" & @CRLF)
   RunWait("wpeutil.exe InitializeNetwork", "x:\windows\system32\", @SW_HIDE)
   If @error Then MsgBox(0x10, "OSDGUI", "Unable to launch process " & "'" & "X:\Windows\System32\wpeutil.exe InitializeNetwork" & "'", Default, $hOSDLoader) ; Display error

   Sleep(250)

   LockWrite($hLock, _NowDate() & " " & _NowTime() & " " & "Launch process 'x:\windows\system32\ping.exe localhost'" & @CRLF)
   RunWait("ping.exe localhost", "x:\windows\system32", @SW_HIDE)
   If @error Then MsgBox(0x10, "OSDGUI", "Unable to launch process " & "'" & "X:\Windows\System32\ping.exe localhost" & "'", Default, $hOSDLoader) ; Display error

   Sleep(250)

   LockWrite($hLock, _NowDate() & " " & _NowTime() & " " & "Launch process 'x:\windows\system32\wpeinit.exe'" & @CRLF)
   RunWait("wpeinit.exe", "x:\windows\system32", @SW_HIDE)
   If @error Then MsgBox(0x10, "OSDGUI", "Unable to launch process " & "'" & "X:\Windows\System32\wpeinit.exe" & "'", Default, $hOSDLoader) ; Display error

   Sleep(250)

   LockWrite($hLock, _NowDate() & " " & _NowTime() & " " & "ShareName= '" & IniRead($OSDConf, "Share", "Name", Default) & "'" & @CRLF)
   LockWrite($hLock, _NowDate() & " " & _NowTime() & " " & "Username= '" & IniRead($OSDConf, "Username", "User", Default) & "'" & @CRLF)

   TCPStartup()
   LockWrite($hLock, _NowDate() & " " & _NowTime() & " " & "ServerName= '" & _TCPIpToName ( IniRead($OSDConf, "Server", "IP", Default), 0) & "'" & @CRLF)

   GUICtrlSetData($oLabelText, $aLabelText[1])
   LockWrite($hLock, _NowDate() & " " & _NowTime() & " " & $aLabelText[1] & @CRLF)
   Sleep(250)

   ; Map network drive
   LockWrite($hLock, _NowDate() & " " & _NowTime() & " " & "Mapping network drive" & @CRLF)
   RunWait("cmd.exe /c" & " net use " & "Z: " & "\\" & IniRead($OSDConf, "Server", "IP", "") & "\" & IniRead($OSDConf, "Share", "Name", "") & " /user:" & IniRead($OSDConf, "Server", "IP", "") & "\" & IniRead($OSDConf, "Username", "User", "") & " /persistent:no " & IniRead($OSDConf, "Password", "Pass", ""), @SystemDir, @SW_HIDE)
   Sleep(250)

   If FileExists("Z:\osd\OSDGUI.exe") Then

      LockWrite($hLock, _NowDate() & " " & _NowTime() & " " & "Found GUI in folder 'Z:\OSD'" & @CRLF)
      LockWrite($hLock, _NowDate() & " " & _NowTime() & " " & "Copying OSD files to local store" & @CRLF)

      RunWait(@ComSpec & " /c copy Z:\OSD\*.* C:\Users\Doni\Desktop", "", @SW_HIDE)


      Else

         LockWrite($hLock, _NowDate() & " " & _NowTime() & " " & "GUI is missing in folder 'Z:\OSD'" & @CRLF)

   EndIf

EndFunc
Link to post
Share on other sites

so your copy method is line 121:

RunWait(@ComSpec & " /c copy Z:\OSD\*.* C:\Users\Doni\Desktop", "", @SW_HIDE)

you copy multiple files by a blocking function. you have 2 options:

1) after the copy is done (line 122), perform a 1-direction comparison of the source and destination folder, and FileWriteLine() the name of any file in the source that exists in the destination to a log file.

2) change the method to loop the files. copy each one and then log it.

Link to post
Share on other sites

@orbs

Can you help me with providing a short example?

I have tried to put in:

Local $FilePath = "C:\Users\Doni\Desktop\A\*.*"
Local $FileDest = "C:\Users\Doni\Desktop\B"
Local $FileCopy = RunWait(@ComSpec & " /c copy " & $FilePath & " " & $FileDest)
LockWrite($hLock, _NowDate() & " " & _NowTime() & " " & "Copying file '" & FileWriteLine($FileCopy, $FilePath <> $FileDest) & "'" & @CRLF)

But that only gives me a 0

Link to post
Share on other sites
  • Solution

switching to method 2: replace the copying command with this section:

$aFileList=_FileListToArray($FilePath,"*")
For $i=1 To $aFileList[0]
If FileCopy($FilePath&'\'&$aFileList[$i],$FileDest&'\*') Then LockWrite($hLock, _NowDate() & " " & _NowTime() & " " & "File copied: " & $aFileList[$i])
Next

this simply lists the files in to an array, then loops the array, copying each file in turn. if a file was copied successfully, a log entry is added.

you can easily add a log entry for a file failed or skipped.

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
  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...