使用命令行重命名多个文件夹

使用命令行重命名多个文件夹

我正在尝试一次重命名多个文件夹,并删除其名称的前几个字符:

我找到了周围的资源,但我尝试过的所有命令都不起作用:

2 - AT5CRO5JUDBWD4RUPXSQ
4 - LH4FVU3TQDEC87YGN6FL
12 - A878KB79QDIBFOTWB0T8
28 - 5UB5KFD2PK38Z4LS6W80

AT5CRO5JUDBWD4RUPXSQ
LH4FVU3TQDEC87YGN6FL
A878KB79QDIBFOTWB0T8
5UB5KFD2PK38Z4LS6W80

进入文件夹后我尝试:

rename "????*" "////*"
rename "....*" "////*"
ren "????*" "////*"
ren "....*" "////*"
rename "....*" "????*"
rename "????*" "????*"

每次我都收到语法错误或不匹配。我真的不明白。有人有解决办法吗?

问候,

答案1

熟悉以下命令将会对你有所帮助:


For
For /r 
For /d
Set 
Set string manipulation (substrings)
For loop expanding variables

  • 使用For循环可以扩展变量:
    %~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

观察.: 关于使用%%~xdirectory名称观察注释中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


您可以使用for /d循环来执行此操作并set删除之前(和一起)的所有内容*-[space]

所有for /d目录都会在循环中列出,并且它们的源名称将在中%~nxi,可以在ren命令语法中使用。

对于目标名称,使用!_dir:*- =!,它将删除*之前的所有内容() ,并且已经通过在同一行中扩展变量而不使用不需要的字符来-定义目标名称!_dir!cmd.exe /v:on /c

对于您一直尝试的操作,语法中for /d循环和子字符串的使用可以通过以下方式解决:setren

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:*- =!""



观察:对于做同样的事情

Get-ChildItem -Directory | Rename-Item -NewName {$_.Name -Replace '.* ',''}

# or, smaller with the same results...
gci -ad | ren -New {$_.Name -Replace '.* ',''}

答案2

在 powershell 中尝试这个:

Get-ChildItem "Filepath" | Foreach {
  $name = $_.Name.ToString().Split("-")[1].Trim()
  Rename-Item -Path $_.Fullname -NewName $name
}

答案3

PowerShell:详细:

Get-ChildItem -Directory | Rename-Item -NewName { $_.Name.Split(' ')[-1] }

KeyBanger:

gci -ad | ren -New { $_.Name.Split(' ')[-1] }

相关内容