Skip to content

Instantly share code, notes, and snippets.

@OralSeth
Last active March 17, 2021 23:40
Show Gist options
  • Save OralSeth/8a6072de271e7cd75bdafbafca1e997b to your computer and use it in GitHub Desktop.
Save OralSeth/8a6072de271e7cd75bdafbafca1e997b to your computer and use it in GitHub Desktop.
<#
Ok - so there's multiple ways to skin this cat.
(1) BAT File that calls this GIST into cache and runs it - just save the following as a .BAT:
@ECHO OFF
powershell.exe /executionpolicy bypass /command "(New-Object Net.WebClient).DownloadString('http://bit.ly/renFiles') | Invoke-Expression"
(2) BAT File + PS1 File...the PS1 is saved in the same folder as the BAT, BAT calls the PS1, PS1 can be edited whenever you need to adjust the FolderPath.
- Copy the stuff starting with 'Function...' to the end. Save it somewhere as 'Rename-Files.ps1'
- Copy the next two lines and save it as 'Rename-Files.bat':
@ECHO OFF
powershell.exe /executionpolicy bypass /command ".\Rename-Files.ps1"
As long as they're in the same folder, you're golden.
#>
Function Rename-Files {
Param (
[Parameter(Mandatory=$true)]
[string]$folderPath
)
$files = Get-ChildItem $folderPath
$count = $files.Count
For ($i = 0; $i -lt $count; $i++) {
$n = $i + 1
If ($n -lt 10) {
$prefix = "0$($n)"
}
Else {
$prefix = "$($n)"
}
$name = $files[$i].Name
$newName = "$($prefix) - $($name)"
Rename-Item $files[$i].FullName -NewName "$($newName)"
}
}
Rename-Files -folderPath "C:\Users\1155022\Documents\-Rename"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment