Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

I would like a Powershell equivalent of the following script

Posted on 2014-09-23
20
Medium Priority
?
118 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 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 18

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
 [eBook] Windows Nano Server

Download this FREE eBook and learn all you need to get started with Windows Nano Server, including deployment options, remote management
and troubleshooting tips and tricks

 

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

New Tabletop Appliances Blow Competitors Away!

WatchGuard’s new T15, T35 and T55 tabletop UTMs provide the highest-performing security inspection in their class, allowing users at small offices, home offices and distributed enterprises to experience blazing-fast Internet speeds without sacrificing enterprise-grade security.

Question has a verified solution.

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

Previously, on our Nano Server Deployment series, we've created a new nano server image and deployed it on a physical server in part 2. Now we will go through configuration.
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
Loops Section Overview
Screencast - Getting to Know the Pipeline

876 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