Link to home
Start Free TrialLog in
Avatar of Luis Diaz
Luis DiazFlag for Colombia

asked on

VB Script: copy files based on the contains of a correspondent log file

Hello experts,

I hava a folder C:\results\

Wich contains .csv and .log files every log file has a correspondent csv file ex:

Req15858.csv  Req18589.log.

I am looking for a script that:

Read .log with a with a DateDiff("d", file.DateLastModified, Now) < 1 and if the first line of the log file end with the number for “4” then copy csv file correspondent into C:\request\
Modify the name of csv file by adding a _v2 at the end of the name.
Every log file contains just one line.

Example :

Req5896log.

Contains the following and has a datemodified equal of today :
1;TTII_00001;;;4


Req5896.csv

Contains the following:
ACC;TT;ID;GlobalID;fd;TT;Name;AcS;Cf;thfj
China Fg;TTII_00001;OoN0009SBE;No_Global_Id;OoN0009SBE;No_Global_Id;SSBEA support;Out of accounting system;No;Creation.

After I launch the script  Req5896_v2.csv with the same contains need to be created or copy at :
C:\request\
ACC;TT;ID;GlobalID;fd;TT;Name;AcS;Cf;thfj
China Fg;TTII_00001;OoN0009SBE;No_Global_Id;OoN0009SBE;No_Global_Id;SSBEA support;Out of accounting system;No;Creation.


Log requirements:

If C:\result\ doesn’t exist log output =  Now C:\result\ doesn’t exist
If C:\request\ doesn’t exist log output =  Now C:\result\ doesn’t exist
If .there is a .log file which ends with “4” and meet the requirement of Datelastmodified specify bellow logput NOW No v2.csv files have been created.
If the condition above is not meet log ouput = Now There is not files to treat
Log file name = log-req.log
Log file should be generated at: objFSO.GetAbsolutePathName(".")

Thank you in advance
Avatar of RobSampson
RobSampson
Flag of Australia image

Hi, can I just clarify, your example of a CSV and matching LOG, you wrote
Req15858.csv  Req18589.log

They don't seem to match.....should it be the same number:
Req15858.csv  Req15858.log

Rob.
Avatar of Bill Prew
Bill Prew

Okay, this should be pretty close to what you asked for, adjust folders as needed.  DEBUG mode is one now, turn off after testing.

' Option to display some information to the console (STDOUT) during processing for testing
blnDebug = True

' Text file I/O constants
Const ForReading = 1
Const ForWriting = 2
Const ForAppending = 8

'Create the file system object for creating folders:
Set objFSO = CreateObject("Scripting.FileSystemObject")

' Define folders and files to work with
strScriptDir = objFSO.GetAbsolutePathName(".")
strBaseDir = "B:\EE\EE28634582\Results"
strDestDir = "B:\EE\EE28634582\Request"
strLogFile = strScriptDir & "\log-req.log.txt"
strLogExt = ".log"
strDataExt = ".csv" 
intAgeDays = 1

' Open log file for appending
Set objLog = objFSO.OpenTextFile(strLogFile, ForAppending, True)

' Make sure input folder exist
If Not objFSO.FolderExists(strBaseDir) Then
   objLog.WriteLine Now & " ERROR: Input folder """ & strBaseDir & """ does not exist."
   Wscript.Quit
End If

' Make sure output folder exist
If Not objFSO.FolderExists(strDestDir) Then
   objLog.WriteLine Now & " ERROR: Output folder """ & strDestDir & """ does not exist."
   Wscript.Quit
End If

' Process all files in this data folder
intFiles = 0
For Each objFile In objFSO.GetFolder(strBaseDir).Files
   If blnDebug Then Wscript.Echo Now & " DEBUG: Checking file """ & objFile.Path & """."
   intFiles = intFiles + 1

   ' Make sure it matches the files we want to copy
   If ProcessFile(objFile) Then
      strFrom = strBaseDir & "\" & objFSO.GetBaseName(objFile.Path) & strDataExt
      strTo = strDestDir & "\" & objFSO.GetBaseName(objFile.Path) & strDataExt
      If objFSO.FileExists(strFrom) Then
         If blnDebug Then Wscript.Echo Now & " DEBUG: Copying file """ & strFrom & """ to """ & strTo & """."
         objFSO.CopyFile strFrom, strTo
         strTo = strBaseDir & "\" & objFSO.GetBaseName(objFile.Path) & "_v2" & strDataExt
         If blnDebug Then Wscript.Echo Now & " DEBUG: Renaming file """ & strFrom & """ to """ & strTo & """."
         objFSO.MoveFile strFrom, strTo
      Else
         objLog.WriteLine Now & " WARNING: Missing data file """ & strFrom & """."
      End If
   End If
Next

' Warn if no matching files in this data folder
If intFiles = 0 Then
   objLog.WriteLine Now & " WARNING: Data folder """ & strBaseDir & """ does not contain any matching files."
Else
   objLog.WriteLine Now & " SUCCESS: Files processed."
End If

' Wrap up
objLog.Close

' Function to determine if we want to process this file
Function ProcessFile(objFile)
   ProcessFile = False
   ' Make sure it is the extension we want
   If LCase(Right(objFile.Name, Len(strLogExt))) = LCase(strLogExt) Then
      ' Make sure it is recent enough
      If blnDebug Then Wscript.Echo Now & " DEBUG: File age """ & DateDiff("d", objFile.DateLastModified, Now) & """ days."
      If DateDiff("d", objFile.DateLastModified, Now) < intAgeDays Then
         If ProcessLog(objFile) Then
            ProcessFile = True
         End If
      End If
   End If
End Function

' Function to read log file and check end of first line for a '4' to process
Function ProcessLog(objFile)
   Set objReadFile = objFSO.OpenTextFile(objFile.Path, ForReading, False, TriStateUseDefault)
   arrLines = Split(objReadFile.ReadAll, VbCrLf)
   objReadFile.Close
   Set objReadFile = Nothing

   If blnDebug Then Wscript.Echo Now & " DEBUG: Checking Log data """ & arrLines(0) & """."
   If Right(arrLines(0), 1) = "4" Then
      ProcessLog = True
   Else
      ProcessLog = False
   End If
End Function

Open in new window

~bp
Avatar of Luis Diaz

ASKER

@Bill,

I test your script and it works, however just one little remark, the file is copied in request folder however the name doesn't finish with a "_V2".
Thank you again for your help.
Okay, had wondered about that from your original question, it wasn't completely clear if you wanted to rename before the copy, or afterward.  I will adjust.

~bp
This change should rename before the copy.

' Option to display some information to the console (STDOUT) during processing for testing
blnDebug = True

' Text file I/O constants
Const ForReading = 1
Const ForWriting = 2
Const ForAppending = 8

'Create the file system object for creating folders:
Set objFSO = CreateObject("Scripting.FileSystemObject")

' Define folders and files to work with
strScriptDir = objFSO.GetAbsolutePathName(".")
strBaseDir = "B:\EE\EE28634582\Results"
strDestDir = "B:\EE\EE28634582\Request"
strLogFile = strScriptDir & "\log-req.log.txt"
strLogExt = ".log"
strDataExt = ".csv" 
intAgeDays = 1

' Open log file for appending
Set objLog = objFSO.OpenTextFile(strLogFile, ForAppending, True)

' Make sure input folder exist
If Not objFSO.FolderExists(strBaseDir) Then
   objLog.WriteLine Now & " ERROR: Input folder """ & strBaseDir & """ does not exist."
   Wscript.Quit
End If

' Make sure output folder exist
If Not objFSO.FolderExists(strDestDir) Then
   objLog.WriteLine Now & " ERROR: Output folder """ & strDestDir & """ does not exist."
   Wscript.Quit
End If

' Process all files in this data folder
intFiles = 0
For Each objFile In objFSO.GetFolder(strBaseDir).Files
   If blnDebug Then Wscript.Echo Now & " DEBUG: Checking file """ & objFile.Path & """."
   intFiles = intFiles + 1

   ' Make sure it matches the files we want to copy
   If ProcessFile(objFile) Then
      strFrom = strBaseDir & "\" & objFSO.GetBaseName(objFile.Path) & strDataExt
      If objFSO.FileExists(strFrom) Then

         ' Add "_v2" to data file name
         strTo = strBaseDir & "\" & objFSO.GetBaseName(objFile.Path) & "_v2" & strDataExt
         If blnDebug Then Wscript.Echo Now & " DEBUG: Renaming file """ & strFrom & """ to """ & strTo & """."
         objFSO.MoveFile strFrom, strTo

         ' Copy data file to REQUEST folder
         strFrom = strTo
         strTo = strDestDir & "\" & objFSO.GetBaseName(strFrom) & strDataExt
         If blnDebug Then Wscript.Echo Now & " DEBUG: Copying file """ & strFrom & """ to """ & strTo & """."
         objFSO.CopyFile strFrom, strTo

      Else
         objLog.WriteLine Now & " WARNING: Missing data file """ & strFrom & """."
      End If
   End If
Next

' Warn if no matching files in this data folder
If intFiles = 0 Then
   objLog.WriteLine Now & " WARNING: Data folder """ & strBaseDir & """ does not contain any matching files."
Else
   objLog.WriteLine Now & " SUCCESS: Files processed."
End If

' Wrap up
objLog.Close

' Function to determine if we want to process this file
Function ProcessFile(objFile)
   ProcessFile = False
   ' Make sure it is the extension we want
   If LCase(Right(objFile.Name, Len(strLogExt))) = LCase(strLogExt) Then
      ' Make sure it is recent enough
      If blnDebug Then Wscript.Echo Now & " DEBUG: File age """ & DateDiff("d", objFile.DateLastModified, Now) & """ days."
      If DateDiff("d", objFile.DateLastModified, Now) < intAgeDays Then
         If ProcessLog(objFile) Then
            ProcessFile = True
         End If
      End If
   End If
End Function

' Function to read log file and check end of first line for a '4' to process
Function ProcessLog(objFile)
   Set objReadFile = objFSO.OpenTextFile(objFile.Path, ForReading, False, TriStateUseDefault)
   arrLines = Split(objReadFile.ReadAll, VbCrLf)
   objReadFile.Close
   Set objReadFile = Nothing

   If blnDebug Then Wscript.Echo Now & " DEBUG: Checking Log data """ & arrLines(0) & """."
   If Right(arrLines(0), 1) = "4" Then
      ProcessLog = True
   Else
      ProcessLog = False
   End If
End Function

Open in new window

~bp
Hello Bill,

My fault I didn't specify properly. The rename need to be done when the file is in result and not before.
Confused now, that is what I had in the prior version: #a40662505

That version copied the file as it existed from RESULTS to REQUEST, then after that renamed the file in RESULTS with "_v2".

~bp
Strange because when I did the test with#a40662505 I did get the rename.  I am going to test again.
Hello Bill,

I made the test with the code #a40662505 and I have this:
.csv is re-generated in results (with "_v2). However the .csv is generated in requests without "_v2". The objective is to have "_v2" in request and not in results.

Ref:
Results folder:
Requests folder:
Capture.GIF
Sorry I didn't embed all the images:
results folder:
User generated imagerequests folder:
User generated image
So you want to MOVE, not COPY, the csv and rename in the process?

~bp
Yes it can be a solution, however my initial though was to:

keep the initial csv file in results (nothing need to be rename or modify in results)
 and re-create csv files with "_V2" in request if the correspondent log that contains "1" char

As you can see in my screen-shot the "_V2"  csv file is in results and the initial csv file is in requests. So it should be the opposite. Anyway I will test tomorrow ID:40663327 as I haven't test it and in the meantime I will revise the paths.

Thank you again for your help.
Traveling this week, will get back to these next week...

~bp
ASKER CERTIFIED SOLUTION
Avatar of Bill Prew
Bill Prew

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
It works! Thank you again for your help!
Welcome.

~bp