Renaming multiple folder using command line
I am trying to rename multiple folders at once, removing the first few characters of their name:
I found resource around but none of the command that I have tried worked:
2 - AT5CRO5JUDBWD4RUPXSQ
4 - LH4FVU3TQDEC87YGN6FL
12 - A878KB79QDIBFOTWB0T8
28 - 5UB5KFD2PK38Z4LS6W80
to
AT5CRO5JUDBWD4RUPXSQ
LH4FVU3TQDEC87YGN6FL
A878KB79QDIBFOTWB0T8
5UB5KFD2PK38Z4LS6W80
Once in the folder I tried:
rename "????*" "////*"
rename "....*" "////*"
ren "????*" "////*"
ren "....*" "////*"
rename "....*" "????*"
rename "????*" "????*"
Everytime I get a syntax error or that it doesn't match. I really don't understand. Does anyone have a solution ?
Regards,
What can and will help in many commands would be to familiarizing yourself with:
For
For /r
For /d
Set
Set string manipulation (substrings)
For loop expanding variables
- Using a
For
loop you can expand your variable:
%~i - expands %i removing any surrounding quotes (")
%~fi - expands %i to a fully qualified path file/dir name only
%~ni - expands %i to a file/dir name only
%~xi - expands %i to a file/dir extension only
%%~nxi => expands %%~i to a file/dir name and extension
-
Use the FOR variable syntax replacement: %~pI - expands %I to a path only %~nI - expands %I to a file name only %~xI - expands %I to a file extension only
-
The modifiers can be combined to get compound results: %~pnI - expands %I to a path and file name only %~pnxI - expands %I to a path, file name and extension only
Obs.: About using %%~x
in directory
name observation note in ss64.com
:
-
Full Stop Bug Although Win32 will not recognise any file or directory name that begins or ends with a '.' (period/full stop) it is possible to include a Full Stop in the middle of a directory name and this can cause issues with FOR /D.
-
Parameter expansion will treat a Full Stop as a file extension, so for a directory name like "Sample 2.6.4" the output of %%~nI will be truncated to "Sample 2.6" to return the whole folder name use %%I or %%~nxI
You can do this using a for /d
loop and set
to remove everything that comes before (and together) the *-[space]
:
With for /d
all directories will be listed in the loop, and their source names will be in %~nxi
, which can be used in the ren
command syntax.
For target name, use !_dir:*- =!
, it will remove everything (*
) that comes before -
, and already defining the destination name by expanding in same line the !_dir!
variable without unwanted characters using cmd.exe /v:on /c
For what you have been trying, a use of for /d
loop and substring set
in ren
syntaxes would be resolved by:
for /d %i in (*)do cmd.exe /v:on /c "set "_dir=%~nxi" && move "%~nxi" "!_dir:*- =!""
rem :: or, smaller with the same results...
for /d %i in (*)do cmd/v/c"set "_dir=%~nxi"&&move "%~nxi" "!_dir:*- =!""
-
Some further reading:
[√] Set
[√] CMD /?
[√] For Loop
[√] For /D Loop
[√] For /R Loop
[√] Conditional Execution || && ...
[√] DelayedExpansion (Refer:
cmd /v /c
)[√] Substring in Set command (Refer:
!_dir:*- =!
)
Obs.: For do the same in powershell:
Get-ChildItem -Directory | Rename-Item -NewName {$_.Name -Replace '.* ',''}
# or, smaller with the same results...
gci -ad | ren -New {$_.Name -Replace '.* ',''}
Try this in powershell:
Get-ChildItem "Filepath" | Foreach {
$name = $_.Name.ToString().Split("-")[1].Trim()
Rename-Item -Path $_.Fullname -NewName $name
}
PowerShell: VErbose:
Get-ChildItem -Directory | Rename-Item -NewName { $_.Name.Split(' ')[-1] }
KeyBanger:
gci -ad | ren -New { $_.Name.Split(' ')[-1] }