删除文件名中的特定数字[已关闭]

问题描述 投票:0回答:2

我正在构建一个批处理文件,文件名中可能有也可能没有 10 位数字。如果是 10 位数字,我想删除第一位数字。

更具体地说,前导数字可能是 1,在这种情况下我希望将其删除。

示例文件名:

Softphone Test3_2020-12-08 15-23_13216549871.WAV

所需输出:

Softphone Test3_2020-12-08 15-23_3216549871.WAV

在某些情况下,可能只有 9 位数字,没有前导 1,这是可以的,应该保持不变。

请帮助我调整此脚本以使其正常工作:

for %%z in ("D:\Ipitomy\Recordings\%mm%-%dd%-%yyyy%\AT1*.WAV") do (
  for /f tokens^=4^,8^,10^,12^ delims^=^" %%a in ('type "D:\Ipitomy\Recordings\%mm%-%dd%-%yyyy%\index.xml"^|find /i "%%~nxz"') do (
    for /f "tokens=1,2 delims=:" %%t in ("%%b") do (
        ren "%z" "%c-%t-%%u_%d%~xz" 2>nul
        if errorlevel 1 set "Number=2" & call :NumberedRename "%%z" "%%c-%%t-%%u_%%d%%~xz"
    )
  )
)

batch-file file-rename
2个回答
1
投票

我建议使用此代码来完成任务:

@echo off
setlocal EnableExtensions DisableDelayedExpansion
for /F "delims=" %%I in ('%SystemRoot%\System32\where.exe "*_???????????.wav" 2^>nul') do (
    set "FullName=%%I"
    set "FileName=%%~nxI"
    setlocal EnableDelayedExpansion
    ren "!FullName!" "!FileName:~0,-15!!FileName:~-14!"
    endlocal
)
endlocal

它仅处理文件名末尾带有十位数字的 WAV 文件,并通过删除十位数字中的第一位数字来重命名这些文件。

要了解所使用的命令及其工作原理,请打开命令提示符窗口,执行以下命令,并完整、仔细地阅读每个命令显示的帮助页面。

  • echo /?
  • endlocal /?
  • for /?
  • ren /?
  • set /?
  • setlocal /?
  • where /?

阅读有关 使用命令重定向运算符的 Microsoft 文档,了解

2>nul
的说明。重定向运算符
>
必须在
FOR
命令行上使用脱字符号 ^ 进行转义,以便在 Windows 命令解释器在执行命令 FOR(执行嵌入的
where
命令行)之前处理此命令行时将其解释为文字字符使用在后台启动的单独命令进程,并使用
%ComSpec% /c
'
中的命令行作为附加参数附加。


1
投票
@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
rem The following settings for the source directory, destination directory, target directory,
rem batch directory, filenames, output filename and temporary filename [if shown] are names
rem that I use for testing and deliberately include names which include spaces to make sure
rem that the process works using such names. These will need to be changed to suit your situation.

SET "sourcedir=u:\your files"
SET "destdir=u:\your results"

FOR /f "delims=" %%b IN ('dir /b /a-d "%sourcedir%\*.wav" ') DO (
 SET "namepart=%%~nb"
 IF "!namepart:~-11,1!" neq "_" ECHO REN "%sourcedir%\%%b" "!namepart:~0,-11!!namepart:~-10!.wav"
)
GOTO :EOF

建议的重命名已

echo
编辑。将
ECHO REN
更改为
REN
以执行实际重命名。

© www.soinside.com 2019 - 2024. All rights reserved.