forked from patriklindstrom/Powershell-pasen
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChange-BatchScriptPath.ps1
More file actions
91 lines (88 loc) · 5.85 KB
/
Copy pathChange-BatchScriptPath.ps1
File metadata and controls
91 lines (88 loc) · 5.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
<#
.SYNOPSIS
Search andr Replaces content in text files like path in bat files.
.DESCRIPTION
All files in a directory and its subdirectories will be search after a reg expression and get that replaced.
.EXAMPLE
runs .\Change-BatchScriptPath.ps1 -dir c:\batfiles -OldRegExp "badtext" -newstring "bettertext"
.EXAMPLE
Pipe directory "c:\batfiles" | .\Change-BatchScriptPath.ps1 -OldRegExp "d:\\" -newstring "f:\\engineroom"
.EXAMPLE
Only change powershell script files "c:\scripts" | .\Change-BatchScriptPath.ps1 -old "d:\\" -new "f:\\engineroom" -filter "ps1"
.EXAMPLE
Pipe directory and user order of parameters "c:\scripts" | .\Change-BatchScriptPath.ps1 "txt" "badtext" "goodtext"
.PARAMETER Dir
The full path to the directory where all the text files are. Eg T:\goodstuff\batsorun . Can also be piped into the script.
.PARAMETER Filter
What files should be checked. Default bat files.
.PARAMETER OldRegExp
What regular expression to search for. Alias old
.PARAMETER NewString
what it should be replaced with . Alias new
latest version
http://github.com/patriklindstrom/Powershell-pasen
.LINK
About Author and script
http://www.lcube.se
.LINK
Regular Expression Tutorial
http://www.regular-expressions.info/tutorial.html
.NOTES
File Name : Change-BatchScript.ps1
Author : Patrik Lindström LCube
#>
param
(
[Parameter(
Position=0,
Mandatory=$true,
ValueFromPipeline=$true,
ValueFromPipelineByPropertyName=$true)
]
[string]$Dir ,
[Parameter(
Position=1,
Mandatory=$false,
ValueFromPipeline=$false,
ValueFromPipelineByPropertyName=$true)
]
[Alias('f')]
[string]$Filter="bat" ,
[Parameter(
Position=2,
Mandatory=$true,
ValueFromPipeline=$false,
ValueFromPipelineByPropertyName=$true)
]
[Alias('old')]
[string]$OldRegExp ,
[Parameter(
Position=3,
Mandatory=$true,
ValueFromPipeline=$false,
ValueFromPipelineByPropertyName=$true)
]
[Alias('new')]
[string]$NewString
)
# Test for existence of bat directory path
if (!$Dir)
{
$(Throw 'Missing argument: Dir')
}
if (-not $Dir.EndsWith("\"))
{
$Dir += "\"
}
if (!(test-path $Dir))
{
$(Throw "The Dir: $Dir does not exist")
}
# Set batfilerna så att man kan skriva till dem. Kan vara show stopper
$Dir | gci -r -fi *.$Filter | % { if($_.IsReadOnly){$_.IsReadOnly= $false} }
# Lista batfilerna att de har status not readonly helt onödigt men kul
# $Dir | gci -r -fi *.$Filter | select fullname,isreadonly
# Byt ut 'Tieto Ftp' och 'TietoFtp' mot nytt namn. The main thing
$Dir | gci -r -fi *.$Filter | % { $(gc $_.FullName) -replace $OldRegExp, $NewString| sc $_.FullName }
# Tada lista dem. helt onödigt
$Dir | gci -r -fi *.$Filter | gc