Solved

I would like a Powershell equivalent of the following script

Posted on 2014-09-23
20
100 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
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 

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 69

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
 
LVL 69

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 69

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 69

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 69

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 69

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

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Powershell command to read csv and remove license. 5 26
rename files 4 24
Powershell - Script Refine - v3 8 41
How to disable AD users from a csv  list? 4 27
Utilizing an array to gracefully append to a list of EmailAddresses
Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

840 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