I created a repository on the web service 'GitHub', to which I uploaded files one by one or in a group via the web interface from the browser. The files are placed in folders of different nesting depth. The files have different extensions.
Now I needed to rename 157 files in the repository according to the following principle:
00-00_file-name.ext # before
00-000_file-name.ext # after, added '0'
These are templates. There can be different extensions instead of .ext
, I use .cpp
and .h
. The file-name
can be of different lengths. Instead of 00-00
, there can be 00-01
, 00-02
, ..., 01-00
, 01-01
, ..., 02-00
, 02-01
, 02-02
and so on. That is, I needed in the second number (after the hyphen -
) three digits instead of two.
I know how to rename files one by one through the web interface, but I don't know how to rename many files at once through the 'GitHub' web interface.
I cloned the repository to my computer. I wrote a PowerShell script to rename 157 files and renamed these files in the cloned repository with it. After that, I created one commit with all the renames and sent the changes to the original repository on 'GitHub'. Below I will briefly outline the details on the points. (I work on the 'Windows 10' operating system, I use the 'Git for Windows' program from the command line. I use 'PowerShell' version 7 as a shell. I use 'Windows Terminal' as a terminal emulator.)
- Cloning a repository to my computer:
PS C:\Users\Илья\source\repos> git clone https://github.com/ilyachalov/learncpp-com-examples
- PowerShell script (in the file
rename-files.ps1
, UTF-8 encoding without BOM, CRLF line endings) for renaming files:
Get-ChildItem -File -Recurse |
Rename-Item -NewName {
$_.Name -replace '^(\d\d)-(\d\d)_(.*)', '$1-0$2_$3'
}
I also wrote a version of the script that does the same thing, but still outputs messages to the console about the progress of renaming and the final results:
$total = 0
$renamed = 0
Get-ChildItem -File -Recurse |
Rename-Item -NewName {
$old = $_.Name
$new = $old -replace '^(\d\d)-(\d\d)_(.*)', '$1-0$2_$3'
$script:total++
if ($old -ne $new) {
Write-Host "$old --> $new"
$script:renamed++
} else {
Write-Host "$new"
}
$new
}
"Total files checked: $total, including renamed: $renamed"
Starting renaming (before using the script, I put it in the target folder and it checks the subfolders in the target folder too; after renaming, the script from the target folder should be deleted so that it does not get into the commit):
PS ..\repos\learncpp-com-examples\chapters> .\rename-files
- Creating a commit:
PS ..\learncpp-com-examples> git add --all
As far as I understand, in this case, the git add --all
command executes git rm 'old-name-file'
and git add 'new-name-file'
for each file, and the 'Git' system diagnoses file renaming. (There's a book about it.)
PS ..\learncpp-com-examples> git commit -m "Renaming a group of files (157)"
- Sending changes to the original 'GitHub' repository:
PS C:\Users\Илья\source\repos\learncpp-com-examples> git push origin main
In the end, I succeeded, but I'm curious if there are more optimal ways to do the same. After all, I'm new to 'GitHub' and may not know something.
My questions. Does GitHub have tools for group file processing via the web interface and through other interfaces? Is it possible to write a script for renaming files in PowerShell more optimally than I did? I couldn't use the git mv
command, is it possible to use regular expressions in this command? What are more convenient programs (tools) for renaming files on 'GitHub'?