Solved

I would like a Powershell equivalent of the following script

Posted on 2014-09-23
20
91 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
  • 10
  • 6
  • 3
  • +1
20 Comments
 
LVL 34

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 34

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
 

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 34

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 34

Accepted Solution

by:
Dan Craciun earned 500 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 68

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 34

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 34

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
Does Powershell have you tied up in knots?

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

 
LVL 68

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 34

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 68

Expert Comment

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

Expert Comment

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

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 34

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 68

Expert Comment

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

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 68

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

[Webinar] Disaster Recovery and Cloud Management

Learn from Unigma and CloudBerry industry veterans which providers are best for certain use cases and how to lower cloud costs, how to grow your Managed Services practice in IaaS clouds, and how to utilize public cloud for Disaster Recovery

Question has a verified solution.

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

Are you one of those front-line IT Service Desk staff fielding calls, replying to emails, all-the-while working to resolve end-user technological nightmares? I am! That's why I have put together this brief overview of tools and techniques I use in o…
How to sign a powershell script so you can prevent tampering, and only allow users to run authorised Powershell scripts
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…
As a trusted technology advisor to your customers you are likely getting the daily question of, ‘should I put this in the cloud?’ As customer demands for cloud services increases, companies will see a shift from traditional buying patterns to new…

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

21 Experts available now in Live!

Get 1:1 Help Now