Renaming a *txt file based on the content of the *txt file

时间:2018-01-11 08:39:50

标签: batch-file

I have read all the similar questions and tried to use it to make a batch script, but unsuccessfully.

I have several *txt files, which are containing information within the file, which should be stored in the file name.

E.g. the *txt below, I would like to store the date from the first line (20171115) and the number in the back of the file (905707) in the file name.

*txt file

In this case, the file name should be "20171115_BOM_905707.txt"

Can anybody help me?

1 个答案:

答案 0 :(得分:0)

未经测试。

setlocal enableDelayedExpansion
set "dir_with_files=C:\files"
for %%# in ("%dir_with_files%\*") do (
  set "date_="
  set "number="
  for /f "usebackq skip=1 tokens=3,4,5,6 delims=. " %%a in ("%%#") do (
     if not defined date_ (
      set "date_=%%a%%b%%c"
      set "number=%%d"
     )
  )
  ren "%%~f#" "!date_!_BOM_!number!%%~x#"
)
endlocal