1

I have a series of files that I need to rename on a daily basis. The files I receive have the following format: yyyyMMdd_hhmmss_xxx.someFileName.txt I need to strip out the time stamp in the middle as well as the three digit field preceeding the filename and leave the date and the "someFileName.txt" piece. The resulting filename should look like: yyyyMMddsomeFileName.txt

I'm pretty clueless when it comes to bat files, I've done some experimenting:

@setlocal EnableDelayedExpansion

@for %%i in (.\*.txt) do call rename %%i

:rename
@set dateString=%%i:~0,8%
@set nameString=%%i:~20%
@set combinedString=%dateString%%nameString%
@echo %combinedString%

Clearly, this doesn't actually rename anything yet. It's just supposed to print the combinedString output. I'm getting a syntax error: "The syntax of the command is incorrect ~0,8 ~20"

What's going on here? What's the correct approach for this?

4

1 回答 1

1

这应该适用于您想要的。

@echo off
for /f "delims=" %%X in ('dir /a:-d /b *.txt') do (
    for /f "tokens=1,2,3,* delims=_." %%A in ("%%~nxX") do (
        echo %%A%%D
    )
)

echo命令替换为ren "%%~fX" "%%A%%D"要重命名它们时。

于 2013-06-27T19:18:41.350 回答