Solved

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

Posted on 2015-01-28
21
354 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:dallagmm
  • 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:dallagmm
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
 

Author Comment

by:dallagmm
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:dallagmm
ID: 40735879
I am working on Windows and can we do that by DOS script (batch file) or VB.
0
 
LVL 41

Expert Comment

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

Author Comment

by:dallagmm
ID: 40736032
do I need to install anything in the Windows to run it?
0
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 
LVL 41

Expert Comment

by:pcelba
ID: 40736086
No
0
 
LVL 41

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:dallagmm
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 41

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:dallagmm
ID: 40736351
Thank you
0
 
LVL 52

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:dallagmm
ID: 40736398
by mistake. Can I undo that?
0
 

Author Closing Comment

by:dallagmm
ID: 40736471
thank you
0

Featured Post

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

Suggested Solutions

Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
Set OWA language and time zone in Exchange for individuals, all users or per database.
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
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 …

867 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now