如何在 Windows 中一次重命名 80.000 个文件

ane*_*yzm 8 windows-7 command-line

我在一个文件夹中有 80.000 个文件,我需要从

filename.jpg
Run Code Online (Sandbox Code Playgroud)

._filename.jpg
Run Code Online (Sandbox Code Playgroud)

在 Windows 环境中,我猜来自 dos。原因是我已将这些文件从 unix 压缩到 tar.gz 并复制到 windows 中,并且由于某种原因文件名已更改。

你能告诉我执行它的命令是什么吗?谢谢

Sii*_*m K 15

这是使用 PowerShell 的一种方法:

导航到您的文件夹并运行此命令

Get-ChildItem *.jpg | Rename-Item -newname {"._" + $_.Name}
Run Code Online (Sandbox Code Playgroud)

额外奖励短版:

gci *.jpg | ren -newname {"._" + $_.Name}
Run Code Online (Sandbox Code Playgroud)


kok*_*ira 6

我有两个解决方案:

  1. 所有文件都在同一个文件夹中

  2. 当子文件夹中有文件并且您想用您想要的字符串替换“n”个第一个字符时,完整的解决方案:D

    • 使用以下命令创建批处理文件
    • 将可变参数更改为您想要的
      • path: 放在""文件的根路径中(例如“C:\documents and settings\user\desktop\new folder”
      • numfirstchars2replace: 用要替换的第一个字符输入一个数字(在您的情况下,为 2)
      • str2put:放置一个字符串作为新文件名的前缀(在您的情况下,._
    • 在与文件所在位置不同的文件夹中运行它


@echo off

::only to tell user what this bat are doing
echo.1.initializing...

::enable that thing to allow, for example, incremental counter in a for loop :)
echo.- EnableDelayedExpansion
SETLOCAL EnableDelayedExpansion

::variables
echo.- variables
:: - place here the absolute root path of your files
set path="put here where are the root folder of your files"
set pathbak=%cd%
set numfirstchars2replace=2
set str2put=._

::go to %path% and its driveletter
echo.- entering the path you want
for /f "delims=¯" %%i in ('echo.%path%') do %%~di
cd %path%

::search all subfolders and save them to a temp file
echo.- searching for subfolders
echo.%path%>%temp%\tmpvar.txt
for /f "delims=¯" %%i in ('dir /s /b /on /ad') do echo."%%i">>%temp%\tmpvar.txt

::execute command for root folder and all found subfolders
echo.
echo.2.executing...
for /f "delims=¯" %%i in (%temp%\tmpvar.txt) do (
  cd %%i
  echo.- in folder: %%i
  for /f "delims=¯" %%j in ('dir /b /on /a-d') do (
    set newname=%%j
    set newname=!newname:~%numfirstchars2replace%,1000!
    echo.- renaming from "%%j" to "%str2put%!newname!"...
    ren "%%j" "%str2put%!newname!"
  )
)

echo.
echo.3.exiting...
::return to %pathbak% and its driveletter
for /f "delims=¯" %%i in ('echo.%pathbak%') do %%~di
cd %pathbak%

@echo on
Run Code Online (Sandbox Code Playgroud)


Chr*_*isF 3

您可以使用内置renameren命令:

ren *.jpg ._*.jpg

不过,与所有这些事情一样,请先在仅包含几个文件的目录上尝试。