文件名前30个字符

时间:2019-10-17 12:49:27

标签: batch-file

我有此代码,该代码应在目录中保留每个文件名的前30个字符...出于某种原因,该代码不会截断名称。你能帮我理解为什么吗?

cd C:\Users\AGL\Documents\test

for /f "delims=" %%a in ('dir /b /a-d *.csv') do (
    set "name=%%~na"
    set "ext=%%~xa"
    setlocal enabledelayedexpansion
    set "nname=!name:~0,-30!"
     endlocal
)

pause

1 个答案:

答案 0 :(得分:1)

我相信您试图这样做?

@echo off
setlocal enabledelayedexpansion
cd /d "C:\Users\AGL\Documents\test"
for /f "delims=" %%a in ('dir /b /a-d *.csv') do (
    set "name=%%~na"
    ren "%%~a" "!name:~0,16!%%~xa"
)
pause