Shell Scripts for Manipulating Filenames
Powershell script to replace spaces with dashes in filenames, and to convert to lowercase.
get-childitem *.md | foreach {rename-item $_ $_.name.replace(" ","-")}; dir . -r | % { if ($_.Name -cne $_.Name.ToLower()) { ren $_.FullName $_.Name.ToLower() } }
For renaming files, Batch Script provides the REN or RENAME command.
Rename files with CMD
RENAME [drive:][path][directoryname1 | filename1] [directoryname2 | filename2]
Rename file extensions
rename *.txt *.bak
Rename single file
rename "TESTA.txt" "TESTB.txt"
Rename whole filenames
cd C:\folder
for /f "Tokens=*" %f in ('dir /l/b/a-d') do (rename "%f" "%f")
pause
Convert filenames to lowercase
for /F %%a in ('dir /L /B') do rename %%a %%a
Rename files with PowerShell
Get-ChildItem "C:\folder" -recurse |
Where {-Not $_.PSIsContainer} |
Rename-Item -NewName {$_.FullName.ToLower()}
# Replace spaces in file names with "-"
for f in *\ *; do mv "$f" "${f// /-}"; done
A short script this week to replace spaces in filenames in a given folder:
# Looping over files, forging new filename, and then renaming it.
# Spaces will be replaced by underscores (_).
for oldname in *
do
newname=`echo $oldname | sed -e 's/ /_/g'`
mv "$oldname" "$newname"
done
# Spaces are now underscores
This one remove the spaces, only the sed expression is different:
for oldname in *
do
newname=`echo $oldname | sed -e 's/ //g'`
mv "$oldname" "$newname"
done
And this one lowercase all letters in addition to removing spaces:
for oldname in *
do
newname=`echo $oldname | sed -e 's/ //g' | tr '[:upper:]' '[:lower:]'`
mv "$oldname" "$newname"
done