-
Notifications
You must be signed in to change notification settings - Fork 0
/
rename-all.ps1
57 lines (47 loc) · 2 KB
/
rename-all.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
function Rename-FilenamesAndDirectoryAndContent {
param (
[string]$NewPhrase
)
# Get the current directory
$Directory = Get-Location
# Get all files and directories in the current directory and subdirectories
$items = Get-ChildItem -Path $Directory -Recurse | Where-Object {
-not $_.PSIsContainer -and
$_.Name -notlike "README.md" -and
$_.Name -notlike "rename-all.ps1"
}
# Replace "ProjectName" with the new phrase in file content
foreach ($file in $items) {
try {
$content = Get-Content -Path $file.FullName -ErrorAction Stop
$newContent = $content -replace "ProjectName", $NewPhrase
Set-Content -Path $file.FullName -Value $newContent
Write-Host "Replaced content in '$($file.FullName)'"
}
catch {
Write-Warning "Could not read or write content of '$($file.FullName)'. Skipping content replacement."
}
}
# Get all directories in the current directory and subdirectories
$directories = Get-ChildItem -Path $Directory -Recurse | Where-Object {
$_.PSIsContainer -and
$_.Name -notlike "README.md" -and
$_.Name -notlike "rename-all.ps1"
}
# Rename directories
foreach ($dir in $directories) {
if ($dir.Name -like "*ProjectName*") {
$newName = $dir.Name -replace "ProjectName", $NewPhrase
$newPath = Join-Path -Path $dir.Parent.FullName -ChildPath $newName
Rename-Item -Path $dir.FullName -NewName $newName -Force
Write-Host "Renamed directory '$($dir.FullName)' to '$($newPath)'"
}
}
# Rename files (already handled above in $items)
# Notify user about completion
Write-Host "Rename operation completed."
}
# Prompt the user for the new phrase
$newPhrase = Read-Host "Enter the new phrase to replace 'ProjectName'"
# Call the function to replace filenames, directory names, and file content
Rename-FilenamesAndDirectoryAndContent -NewPhrase $newPhrase