-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPowershellScript.ps1
104 lines (83 loc) · 2.81 KB
/
PowershellScript.ps1
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
92
93
94
95
96
97
98
99
100
101
102
103
using namespace System.IO
using namespace System.Collections.Generic
$softwareName = Read-Host "`nPlease enter the software name."
try
{
# Replace occurrences of "SoftwareName" in all files
Write-Host "`nReplacing occurrences of 'SoftwareName' with "$softwareName" in all files.`n"
$replace_successful = $true
Get-ChildItem -File -Recurse | ForEach-Object {
try {
If ((Get-Content $_.Extension -eq ".ps1") -or (Get-Content $_.Extension -eq ".bat"))
{
continue
}
} catch {}
try
{
(Get-Content $_.FullName) -replace 'SoftwareName', $softwareName | Set-Content $_.FullName
}
catch
{
Write-Host "`nAn error occurred when replacing file contents of " $_.FullName ":"
Write-Host $_
$replace_successful = $false
}
}
If($replace_successful)
{
Write-Host "Successfully replaced files contents.`n"
}
else
{
Write-Host "`nERROR: Could not replace some file contents.`n"
}
Write-Host "`nRenaming files and directories using "$softwareName":`n"
# Rename files and folders
$stack = [Stack[string]]::new()
$allPaths = [List[string]]::new()
# Get all files and directories containing "SoftwareName" recursively
Get-ChildItem -Recurse -Directory | ForEach-Object {
$dirpath = $_.FullName
$dirname = Split-Path $dirpath -Leaf
if ($dirname.Contains("SoftwareName"))
{
$stack.Push($dirpath)
$allPaths.Add($dirpath)
}
# Write-Host $_.FullName
foreach ($file in [Directory]::EnumerateFiles($dirpath))
{
$filename = [Path]::GetFileName($file)
if ($filename.Contains('SoftwareName') -and -not $allPaths.Contains($file))
{
$stack.Push($file)
$allPaths.Add($file)
}
}
}
# Add root files
Get-ChildItem -File | ForEach-Object {
if ($_.FullName.Contains("SoftwareName")) {
$stack.Push($_.FullName)
}
}
# Rename files and folders
while ($stack.Count) {
$poppedFullName = $stack.Pop()
$pathExists = (-not ([string]::IsNullOrEmpty($poppedFullName))) -and (Test-Path -Path $poppedFullName)
$filename = [Path]::GetFileName($poppedFullName)
if($filename.Contains('SoftwareName') -and $pathExists)
{
$newName = $filename.Replace('SoftwareName', $softwareName)
Write-Host "Renaming: " $poppedFullName " to: " $newName
Rename-Item -LiteralPath $poppedFullName -NewName $newName #-WhatIf
}
}
Write-Host "`nAll files and folders renamed successfully."
}
catch
{
Write-Host "`nERROR:"
Write-Host $_
}