Solved

I need to modify a lot of files in one folder.

Posted on 2015-01-28
21
358 Views
Last Modified: 2015-04-21
I need to modify a lot of files in one folder. I want to read and insert the following

I want to read from each file the following:
SET  .     FLM_20050314_TL1:
And modify the following:
from
DATE .                     :  DATE
To
DATE .        14 MAR 2005             :  DATE
Please find one file attached as an example.

Regards,

Dallag
0
Comment
Question by:Mohammed Dallag
  • 8
  • 4
  • 3
  • +2
21 Comments
 
LVL 45

Expert Comment

by:aikimark
ID: 40576253
In what Operating System environment are you working?
What programming/utility tools have you used before?
0
 

Author Comment

by:Mohammed Dallag
ID: 40735780
Sorry I forgot to add the attachments. Please I need to read file1 and produce file2.

Regards,

Mohammed Dallag
0
 
LVL 45

Expert Comment

by:aikimark
ID: 40735812
still nothing attached
still no answer to my questions

...still waiting
0
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 

Author Comment

by:Mohammed Dallag
ID: 40735815
Sorry I forgot to add the attachments. Please I need to read file1 and produce file2.

 Regards,

 Mohammed Dallag
file1.txt
file2.txt
0
 
LVL 45

Expert Comment

by:aikimark
ID: 40735866
please answer my questions
0
 

Author Comment

by:Mohammed Dallag
ID: 40735879
I am working on Windows and can we do that by DOS script (batch file) or VB.
0
 
LVL 42

Expert Comment

by:pcelba
ID: 40735995
Is PowerShell script OK with you?
0
 

Author Comment

by:Mohammed Dallag
ID: 40736032
do I need to install anything in the Windows to run it?
0
 
LVL 42

Expert Comment

by:pcelba
ID: 40736086
No
0
 
LVL 42

Accepted Solution

by:
pcelba earned 250 total points
ID: 40736184
You may use following script to do what you PROBABLY need:
$path = "d:\temp"
$fileMask = "file1.txt"
$setFLM = "SET  .     FLM_"
$dateOrig = "DATE .        "

Get-ChildItem $path -Filter $fileMask | `
Foreach-Object {
    $rows = Get-Content $_.FullName
    $dateCalc = ($rows | Where-Object {($_ -match $setFLM) }).Substring(15,8)
    $dateCalc = $dateCalc.Substring(0,4)+"-"+$dateCalc.Substring(4,2)+"-"+$dateCalc.Substring(6,2)
    $dateCalc 
    $dateNew = $dateOrig + $dateCalc.Substring(8,2) + " " + (date $dateCalc -format "MMMM").Substring(0,3).ToUpper() + " " + $dateCalc.Substring(0,4)
    $dateNew
    IF ($rows -NotMatch $dateNew) {
       $rows = $rows -Replace $dateOrig, $dateNew
       $rows | Set-Content $_.FullName
       }
    }

Open in new window

Everything you need is to save the script into a file having .PS1 extension and update it by providing correct path and file mask.

I suppose the date inserted into the .TXT file is not constant but derived from the first line. I also suppose all files have equal format.

WARNING: Backup your original files before you start!  Test the script on one file.  The input file is overwritten by the result. To create a new file is your home work if necessary.

PowerShell IDE will open if you doubleclick the .PS1 file.
0
 
LVL 26

Expert Comment

by:wilcoxon
ID: 40736201
Is Perl acceptable?  You would have to install Perl to use it (unless it is already installed on your machine).

It should be possible to do this in Powershell but I don't know it well enough but it is likely simpler in Perl.
0
 

Author Comment

by:Mohammed Dallag
ID: 40736253
I am getting the following error:

File D:\temp\test.ps1 cannot be loaded because the execution of scripts is disa
bled on this system. Please see "get-help about_signing" for more details.
At line:1 char:17
+ D:\temp\test.ps1 <<<<  'D:\temp\'
    + CategoryInfo          : NotSpecified: (:) [], PSSecurityException
    + FullyQualifiedErrorId : RuntimeException
0
 
LVL 42

Expert Comment

by:pcelba
ID: 40736300
You have to enable scripts by the following PowerShell command:

Set-ExecutionPolicy Unrestricted

You may disable scripts execution after you finish the work:

Set-ExecutionPolicy Restricted

Above command requires administrator rights.
0
 

Author Comment

by:Mohammed Dallag
ID: 40736351
Thank you
0
 
LVL 54

Assisted Solution

by:Bill Prew
Bill Prew earned 250 total points
ID: 40736369
Here's a VBscript approach.  Save as a VBS file, adjust the path of the folder where the files are, and test it out.

' Constants for I/O
Const ForReading = 1
Const ForWriting = 2

' Define folder to process
strBaseDir = "B:\EE\EE28605325\Files\"

' Access the folder
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.GetFolder(strBaseDir)

' Process each file in the folder
For Each objFile in objFolder.Files

     ' Read the entire file
     strFile = objFile.Path
     Set objReader = objFSO.OpenTextFile(strFile, ForReading)
     strData = objReader.ReadAll
     objReader.Close

     ' Break it up into each line, and process them
     arrData = Split(strData, vbCrLf)
     For i = 0 To UBound(arrData)
        If arrData(i) <> "" Then
           strLine = arrData(i)

           ' Extract date from "SET" lines
           If Left(strLine, 4) = "SET " Then
              strDate = Mid(strLine, 16, 8)
              strYear = Mid(strDate, 1, 4)
              strMonth = Mid(strDate, 5, 2)
              strDay = Mid(strDate, 7, 2)
              datDate = DateValue(strMonth & "/" & strDay & "/" & strYear)
           End If

           ' Insert date from "SET" line into "DATE" line
           If Left(strLine, 5) = "DATE " Then
              strLine = Mid(strLine, 1, 14) & Day(datDate) & " " & MonthName(Month(datDate), True) & " " & Year(datDate) & Mid(strLine, 15)
              arrData(i) = strLine
           End If
        End If
     Next

     ' Write the updated data back to the file
     Set objWriter = objFSO.OpenTextFile(strFile, ForWriting, True)
     objWriter.Write Join(arrData, vbCrLf)
     objWriter.Close

Next

Open in new window


~bp
0
 

Author Comment

by:Mohammed Dallag
ID: 40736398
by mistake. Can I undo that?
0
 

Author Closing Comment

by:Mohammed Dallag
ID: 40736471
thank you
0

Featured Post

Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Synchronize a new Active Directory domain with an existing Office 365 tenant
A procedure for exporting installed hotfix details of remote computers using powershell
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

821 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question