Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

I would like a Powershell equivalent of the following script

Posted on 2014-09-23
20
Medium Priority
?
115 Views
Last Modified: 2014-10-09
I would like to convert the following script in Powershell.


Set fso = CreateObject("Scripting.FileSystemObject")

data = Split(fso.OpenTextFile("output.ed").ReadAll, "AIS*00*")
For i = 1 To UBound(data)
  fso.OpenTextFile("new" & i & ".txt", 2, True).Write "AIS*00*" & data(i)
Next
0
Comment
Question by:100questions
[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
  • 10
  • 6
  • 3
  • +1
20 Comments
 
LVL 35

Expert Comment

by:Dan Craciun
ID: 40340764
$i = 1
$path = "X:\your\path"
(gc "$path\output.ed") -split "AIS*00*" | %{ 
    New-Item -ItemType file -Path "$path\new$i.txt" -Value "AIS*00*$_" -Force
    $i++
}

Open in new window

HTH,
Dan
0
 
LVL 17

Expert Comment

by:Learnctx
ID: 40341198
Being that split uses a regular expression the above won't work as it is expecting 0 or more repetitions of 'S' and '0', not 'S*' and '0*'. You will want to make sure you escape the asterisk/star.

(gc "$path\output.ed") -split "AIS\*00\*" | %{

Open in new window

0
 
LVL 35

Expert Comment

by:Dan Craciun
ID: 40341278
Did not look at the search string :)

I can generalize the script so it does not matter if there are special characters in it:

###REPLACE WITH YOUR OWN DATA###
$path = "X:\your\path"
$searchString = "AIS*00*"
$i = 1

###DO NOT MODIFY BELOW###

$searchString = [regex]([regex]::Escape($searchString))
(gc "$path\output.ed") -split $searchString | %{ 
    New-Item -ItemType file -Path "$path\new$i.txt" -Value "$searchString$_" -Force
    $i++
}

Open in new window

0
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 

Author Comment

by:100questions
ID: 40342004
Thanks Dan.  The script does not seem to work well, it created many .txt files when in fact the data I was using had only 2 sets of data.  Also all the txt files seem to be one liners etc..
0
 
LVL 35

Expert Comment

by:Dan Craciun
ID: 40342010
OK. Please attach a sample file, so I can test on your data.
0
 

Author Comment

by:100questions
ID: 40342021
Thanks Learnctx.  
This script does not work, it produces this issue:



Missing closing '}' in statement block.
At C:.....1:1 char:47
+ (gc "$path\output.ed") -split "AIS\*00\*" | %{ <<<<
    + CategoryInfo          : ParserError: (CloseBraceToken:TokenId) [], Parse
   Exception
    + FullyQualifiedErrorId : MissingEndCurlyBrace
0
 
LVL 35

Accepted Solution

by:
Dan Craciun earned 2000 total points
ID: 40342300
There was a missing the parameter: raw. Without it, get-content gets one line at a time
###REPLACE WITH YOUR OWN DATA###
$path = "X:\your\path"
$searchString = "AIS*00*"
$i = 1

###DO NOT MODIFY BELOW###

$searchString = [regex]([regex]::Escape($searchString))
(gc "$path\output.ed" -Raw) -split $searchString | %{ 
    New-Item -ItemType file -Path "$path\new$i.txt" -Value "$searchString$_" -Force
    $i++
}

Open in new window

0
 
LVL 71

Expert Comment

by:Qlemo
ID: 40344382
There seems to be an error in the get-content parameter names evaluation, at least for me (PS 4). I had to put -Raw as first switch / parameter to get it work:
 (gc -raw "$path\output.ed") -split $searchString | %{

I cannot prove http:#a40342021 - getting that error is probably due to changes you made we do not see.
0
 
LVL 35

Expert Comment

by:Dan Craciun
ID: 40344394
Weird. I'm on PS4 also (win 8.1) and it worked with -raw at the end.
0
 
LVL 35

Expert Comment

by:Dan Craciun
ID: 40344401
@Qlemo: I don't think the OP did any changes. He simply used that line (
(gc "$path\output.ed") -split "AIS\*00\*" | %{
) and the parser complained it found an opening curly brace without a matching closing one.
0
 
LVL 71

Expert Comment

by:Qlemo
ID: 40344428
This is valid syntax. The parser knows there has to be more on following lines. It won't complain (yet) about a curly brace mismatch.
0
 
LVL 35

Expert Comment

by:Dan Craciun
ID: 40344440
It will, if you paste the line in the PS console:
Missing closing '}' in statement block.
    + CategoryInfo          : ParserError: (:) [], ParentContainsErrorRecordException
    + FullyQualifiedErrorId : MissingEndCurlyBrace
0
 
LVL 71

Expert Comment

by:Qlemo
ID: 40344499
No. All you get is a continuation prompt:PS2-please-continue.png
0
 
LVL 35

Expert Comment

by:Dan Craciun
ID: 40344532
Not in PS ISE on my computer:
Windows-PowerShell-ISE.png
0
 
LVL 71

Expert Comment

by:Qlemo
ID: 40344572
Ahh, using ISE. Important point to know. Yes, the ISE prompt will require "complete" code, because it runs line by line, not allowing for splitting code over several lines.

Note: Don't try multi-line code in the ISE's PS Console, because it is not the same as in the script window or the non-ISE PowerShell prompt.
0
 
LVL 35

Expert Comment

by:Dan Craciun
ID: 40344583
You just can't write multi-line code in ISE's PS Console. But pasting multiple lines of code works just fine.

Then again, in ISE you have a window above where to write the code...
0
 
LVL 71

Expert Comment

by:Qlemo
ID: 40344608
That's the point - there is no point in pasting it into the Console part :D
0
 
LVL 35

Expert Comment

by:Dan Craciun
ID: 40344619
You have to save the code to be able to run it from the script pane.

For a quick throwaway script it's faster (for me) to write it in the script pane and then just paste it in the console and run it.
0
 
LVL 71

Expert Comment

by:Qlemo
ID: 40344667
Using PowerGUI, so not having those issues. It runs unsaved scripts with temporary names, and I don't have to bother about anything :p
0
 

Author Closing Comment

by:100questions
ID: 40371602
Unsure if this works, I might try it.   I need to ask more questions therefore I need to move on.
0

Featured Post

Creating Active Directory Users from a Text File

If your organization has a need to mass-create AD user accounts, watch this video to see how its done without the need for scripting or other unnecessary complexities.

Question has a verified solution.

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

This article explains how to prepare an HTML email signature template file containing dynamic placeholders for users' Azure AD data. Furthermore, it explains how to use this file to remotely set up a department-wide email signature policy in Office …
Windows 10 came with  a lot of built in applications, Some organisations leave them there, some will control them using GPO's. This Article is useful for those who do not want to have any applications in their image (example:me).
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…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…

704 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