command line batch remove _ from its file name

2019-09-12 16:48发布

问题:

I have multiple files and would like to rename its file.
For example, I have

"2014_19_24_english_test.doc"

and to change it to

"2014 19 24 english test.doc"

Here is what I have done so far:

for /f "delims=" %%f in ('dir /b *.hwp') do ren "%%~f" "%%~nf:_= %%~xf"

and this is not working. Any help would be appreciated,

Chris

回答1:

You cannot use the substring replacement syntax with for variables, so you need an interim variable to do that, using delayed expansion:

setlocal EnableDelayedExpansion
for /f "delims=" %%f in ('dir /b *.hwp') do (
    set "FNAME=%%~nf"
    ren "%%~ff" "!FNAME:_= !%%~xf"
)
endlocal


回答2:

I found it out and this is what I did :

for /f "delims=" %%i in ('dir /b "*.doc"') do set LIST=%%i & set LIST | ren "%%~fi" "%%LIST:_= %%"

Hope this helps.