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
LVL 1
LD16Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

RobSampsonCommented:
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.
0
Bill PrewIT / Software Engineering ConsultantCommented:
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
0
LD16Author Commented:
@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.
0
Introduction to Web Design

Develop a strong foundation and understanding of web design by learning HTML, CSS, and additional tools to help you develop your own website.

Bill PrewIT / Software Engineering ConsultantCommented:
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
0
Bill PrewIT / Software Engineering ConsultantCommented:
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
0
LD16Author Commented:
Hello Bill,

My fault I didn't specify properly. The rename need to be done when the file is in result and not before.
0
Bill PrewIT / Software Engineering ConsultantCommented:
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
0
LD16Author Commented:
Strange because when I did the test with#a40662505 I did get the rename.  I am going to test again.
0
LD16Author Commented:
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
0
LD16Author Commented:
Sorry I didn't embed all the images:
results folder:
Capture.GIFrequests folder:
Capture.GIF
0
Bill PrewIT / Software Engineering ConsultantCommented:
So you want to MOVE, not COPY, the csv and rename in the process?

~bp
0
LD16Author Commented:
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.
0
Bill PrewIT / Software Engineering ConsultantCommented:
Traveling this week, will get back to these next week...

~bp
0
Bill PrewIT / Software Engineering ConsultantCommented:
Okay, I think this is what you are looking for.  It leaves the file as is in RESULTS, and places a copy in REQUEST adding "_v2" to the end of the file name.

' 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

         ' Copy data file to REQUEST folder, and add "_v2" to data file name
         strTo = strDestDir & "\" & objFSO.GetBaseName(objFile.Path) & "_v2" & 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
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
LD16Author Commented:
It works! Thank you again for your help!
0
Bill PrewIT / Software Engineering ConsultantCommented:
Welcome.

~bp
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
VB Script

From novice to tech pro — start learning today.