Batch script to invert the order of a date in file names

强颜欢笑 提交于 2019-12-02 19:35:50

问题


I wanted to use a batch script to rename a bunch of files which are using the following name scheme:

File 2-9.pdf
File 3-9.pdf
File 4-9.pdf
[...]

I want to invert the numbers so that they will become...

File 9-2.pdf
File 9-3.pdf
File 9-4.pdf
[...]

Normally I would search for the repeating string and replace it, but in this case the string changes with each file, so I don't know how to go about it. Any ideas?

Thank you for reading.

EDIT: I am on Windows, I am seeking to create a .bat file. Something like

For %%# in ("file-path") Do (
    Set "File=%%~nx#"
    Ren "%%#" "!File:%Pattern%=%Replace%!"
)

回答1:


relatively easy. Don't use "search and replace", but use proper tokens and delimiters to split your filenames:

@echo off 
for %%f in (*.pdf) do (
  for /f "tokens=1,2,3 delims=- " %%a in ("%%~nf") do (
    ECHO ren "%%~f" "%%a %%c-%%b%%~xf"
  )
)

(Note: remove the ECHO after troubleshooting to enable the rename command)



来源:https://stackoverflow.com/questions/46121328/batch-script-to-invert-the-order-of-a-date-in-file-names

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!