Solved

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

Posted on 2015-01-28
21
365 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

 

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
My attempt to use PowerShell and other great resources found online to simplify the deployment of Office 365 ProPlus client components to any workstation that needs it, regardless of existing Office components that may be needing attention.
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

734 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