?
Solved

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

Posted on 2015-01-28
21
Medium Priority
?
379 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
17 Comments
 
LVL 46

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 46

Expert Comment

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

...still waiting
0
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

 

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 46

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 43

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 43

Expert Comment

by:pcelba
ID: 40736086
No
0
 
LVL 43

Accepted Solution

by:
pcelba earned 1000 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 27

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 43

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 59

Assisted Solution

by:Bill Prew
Bill Prew earned 1000 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

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

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

This is a fine trick which I've found useful many times, when you just don't want to accidentally run a batch script or the commands needs administrator rights.
If you are a mobile app developer and especially develop hybrid mobile apps then these 4 mistakes you must avoid for hybrid app development to be the more genuine app developer.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Screencast - Getting to Know the Pipeline
Suggested Courses
Course of the Month15 days, 19 hours left to enroll

850 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