Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2015-01-28
21
Medium Priority
?
377 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 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
Looking for the Wi-Fi vendor that's right for you?

We know how difficult it can be to evaluate Wi-Fi vendors, so we created this helpful Wi-Fi Buyer's Guide to help you find the Wi-Fi vendor that's right for your business! Download the guide and get started on our checklist 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 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 58

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

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

A recent project that involved parsing Tableau Desktop and Server log files to extract reusable user queries for use in other systems. I chose to use PowerShell to gather the data, and SharePoint to present it...
Auditing domain password hashes is a commonly overlooked but critical requirement to ensuring secure passwords practices are followed. Methods exist to extract hashes directly for a live domain however this article describes a process to extract u…
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
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 …
Suggested Courses

636 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