Windows 脚本作为批处理运行以替换文件名列表中的字符

Windows 脚本作为批处理运行以替换文件名列表中的字符

我有如下4个文件:

C:\Documents and Settings\vm\AC R\test

JE.BOS.20130516.CSV
JE.BES.20130516.CSV
KFROPN.SAVEFILE.CSV
KFBAD.SAVEFILE.CSV

我必须"."在文件名中替换为"-"。但扩展名.CSV应该保持不变。

1)我执行了以下命令并收到以下错误:

for /f "tokens=1*delims=." %%i in ('dir /a-d/b *.*.CSV') do ren "%%~i.%%~j" "%%~i-%%~j"

错误:

C:\Documents and Settings\vm\AC R\test>for /f "tokens=1*delims=." %%
i in ('dir /a-d/b *.*.CSV') do ren "%%~i.%%~j" "%%~i-%%~j"
%%i was unexpected at this time.

2)我删除了 i 之前的一个%,然后执行如下操作:

for /f "tokens=1*delims=." %i in ('dir /a-d/b *.*.CSV') do ren "%%~i.%%~j" "%%~i-%%~j"

出现以下错误:

C:\Documents and Settings\vmeruga\ACCESS RECS\test>for /f "tokens=1*delims=." %i
 in ('dir /a-d/b *.*.CSV') do ren "%%~i.%%~j" "%%~i-%%~j"

C:\Documents and Settings\vmeruga\ACCESS RECS\test>ren "%JEFFERIES.%BODPOS.20130
516.CSV" "%JE-%BODPOS.20130516.CSV"
The system cannot find the file specified.

C:\Documents and Settings\vmeruga\ACCESS RECS\test>ren "%JEFFERIES.%BODTRADES.20
130516.CSV" "%JE-%BODTRADES.20130516.CSV"
The system cannot find the file specified.

C:\Documents and Settings\vmeruga\ACCESS RECS\test>ren "%KFBAROPN.%SAVEFILE.CSV"
 "%KFOPN-%SAVEFILE.CSV"
The system cannot find the file specified.

C:\Documents and Settings\vmeruga\ACCESS RECS\test>ren "%KFBARTRD.%SAVEFILE.CSV"
 "%KFBARTRD-%SAVEFILE.CSV"
The system cannot find the file specified.

答案1

好了,将以下内容保存为.BAT 文件并执行:

@setlocal enabledelayedexpansion&&for /f %%a in ('dir /b *.csv') do @set fn=%%~na&&set fn=!fn:.=-!&&ren "%%a" "!fn!%%~xa"

答案2

尝试一下,查看输出并删除单词echo,如果可以的话:

@echo 关闭&设置本地
cd /d "C:\Documents and Settings\vm\AC R\test"
对于 (*.*.csv) 中的 %%i 执行(
    设置“fname=%%~ni”
    设置本地启用延迟扩展
    设置“nname=!fname:.=-!”
    回显重命名“!fname!.CSV”“!nname!.CSV”
    端局部

相关内容