切换文件名的部分内容

切换文件名的部分内容

我有一大批 pdf 文件,它们的格式都是“[something] somethingelse”。

我怎样才能切换第一部分和结束部分来实现“somethingelse [something]”?

例如(之前和之后):

[P. Morandi] Field and Galois Theory
Field and Galois Theory [P. Morandi]

[D. S. Bridges] Foundations of Real and Abstract Analysis
Foundations of Real and Abstract Analysis [D. S. Bridges] 

[J. G. Ratcliffe] Foundations of Hyperbolic Manifolds
Foundations of Hyperbolic Manifolds [J. G. Ratcliffe]

[R. E. Edwards] Fourier Series - A Modern Introduction Volume 1
Fourier Series - A Modern Introduction Volume 1 [R. E. Edwards]

[B. Bollobás] Graph Theory - An Introductory Course
Graph Theory - An Introductory Course [B. Bollobás]

答案1

我怎样才能交换第一部分和结束部分?

例如(之前和之后):

[P. Morandi] Field and Galois Theory.pdf
Field and Galois Theory [P. Morandi].pdf

使用以下批处理文件(test.cmd):

@echo off 
setlocal enabledelayedexpansion
for /f "usebackq delims=] tokens=1,2" %%a in (`dir /b *.pdf`) do (
  rem %%b is end part of name and will become 1st part
  rem remove extension
  set _first=%%~nb
  rem remove leading space
  set _first=!_first:~1!
  ren "%%a]%%b" "!_first! %%a].pdf"
  )
endlocal  

笔记:

  • 使用问题中的前两个示例文件名进行测试。

使用示例:

> dir *.pdf
 Volume in drive F is Expansion
 Volume Serial Number is 3656-BB63

 Directory of F:\test

02/10/2016  19:43                 0 [D. S. Bridges] Foundations of Real and Abstract Analysis.pdf
02/10/2016  19:42                 0 [P. Morandi] Field and Galois Theory.pdf
               2 File(s)              0 bytes
               0 Dir(s)  1,733,769,015,296 bytes free

> test

> dir *.pdf
 Volume in drive F is Expansion
 Volume Serial Number is 3656-BB63

 Directory of F:\test

02/10/2016  19:42                 0 Field and Galois Theory [P. Morandi].pdf
02/10/2016  19:43                 0 Foundations of Real and Abstract Analysis [D. S. Bridges].pdf
               2 File(s)              0 bytes
               0 Dir(s)  1,733,769,015,296 bytes free

进一步阅读

  • Windows CMD 命令行的 AZ 索引- 与 Windows cmd 行相关的所有事物的绝佳参考。
  • 目录- 显示文件和子文件夹的列表。
  • 对于/f- 循环命令以执行另一个命令的结果。
  • 参数- 命令行参数(或参数)是传递到批处理脚本的任何值。
  • - 重命名一个或多个文件。
  • 变量- 提取变量的一部分(子字符串)。

相关内容