简体   繁体   English

如何将子文件夹中的文件重命名为特定格式

[英]How to rename files in subfolder into a specific format

I have files named as RabcdYYMMKKACCOUNT.TXT in the Subfolders of a folder where YYMM is year, month this will change. 我在YYMM为年,月的文件夹的子文件夹中有名为RabcdYYMMKKACCOUNT.TXT的文件,这将更改。 KK is another identifier, I want all the files to be renamed to MSFKKDNB.ABC , the KK is the identifier in the input file. KK是另一个标识符,我希望将所有文件重命名为MSFKKDNB.ABCKK是输入文件中的标识符。

Below is the one i tried and the result of it: 以下是我尝试过的一个及其结果:

FOR /R %%f IN (*account.txt) DO REN "%%f" *dnb.abc

R00531706AUAccount.txt is renamed to R00531706AUAccount.txtdnb.abc R00531706AUAccount.txt重命名为R00531706AUAccount.txtdnb.abc

but the output should be MSFAUDNB.abc 但输出应为MSFAUDNB.abc

Try this: 尝试这个:

@Echo Off
For %%A In ("*account.txt") Do (Set "_=%%~nA"
    SetLocal EnableDelayedExpansion
    Ren "%%A" "MSF!_:~-9,2!DNB.abc"
    EndLocal)

This could be done for example with: 例如,可以使用以下方法完成此操作:

@echo off
setlocal EnableExtensions EnableDelayedExpansion
for /R %%I in (???????????account.txt) do (
    set "FileName=%%~nI"
    set "NewFileName=MSF!FileName:~9,2!DNB.abc"
    if not exist "%%~dpI!NewFileName!" (
        ren "%%~fI" "!NewFileName!" 2>nul
        if not exist "%%~dpI!NewFileName!" echo Failed to rename file: "%%~fI"
    ) else (
        echo Cannot rename file: "%%~fI"
    )
)
endlocal

The file name of found account text file is assigned to environment variable FileName . 找到的帐户文本文件的文件名已分配给环境变量FileName

The new name for the file is created by concatenating the fixed parts MSF and DNB.abc with the 2 characters to keep from file name using string substitution and delayed expansion . 通过使用字符串替换和延迟扩展将固定部分MSFDNB.abc与2个字符连接起来以防止文件名创建文件的新名称。

Next it is checked if a file with new name does not already exist. 接下来,检查是否不存在具有新名称的文件。 Is this the case the file renaming is done otherwise an error message is output. 这是文件重命名完成的情况,否则输出错误消息。

After renaming the file it is checked if that was successful. 重命名文件后,将检查是否成功。 A slightly different error is output if renaming failed for example because of a sharing violation. 如果重命名失败(例如由于共享冲突),则会输出略有不同的错误。

For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully. 为了了解所使用的命令及其工作方式,请打开命令提示符窗口,在其中执行以下命令,并非常仔细地阅读每个命令显示的所有帮助页面。

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

Read also the Microsoft article about Using Command Redirection Operators . 另请阅读有关使用命令重定向运算符的Microsoft文章。

I would probably do it the following way, provided that the files to rename are located in immediate sub-directories ( YYMM ) of the given root directory and nowhere else: 如果要重命名的文件位于给定根目录的直接子目录( YYMM )中,并且没有其他地方,我可能会采用以下方式进行操作:

@echo off
setlocal EnableExtensions DisableDelayedExpansion

set "_ROOT=." & rem // (specify path to the root directory)

for /D %%D in ("%_ROOT%\????") do (
    for %%F in ("%_ROOT%\%%~nxD\R??????????Account.txt") do (
        set "FDIR=%%~nxD" & set "FILE=%%~nxF"
        setlocal EnableDelayedExpansion
        ECHO ren "!_ROOT!\!FDIR!\!FILE!" "MSF!FILE:~9,2!DNB.abc"
        endlocal
    )
)

endlocal
exit /B

If you want to check whether both the sub-directory name and the year/month portion of the file names are purely numeric, you could use the following script: 如果要检查子目录名称和文件名的年/月部分是否均为纯数字,则可以使用以下脚本:

@echo off
setlocal EnableExtensions DisableDelayedExpansion

set "_ROOT=." & rem // (specify path to the root directory)

for /F "delims= eol=|" %%D in ('
    dir /B /A:D "%_ROOT%\????" ^| ^
        findstr "^[0123456789][0123456789][0123456789][0123456789]$"
') do (
    for /F "delims= eol=|" %%F in ('
        dir /B /A:-D "%_ROOT%\%%~nxD\R??????????Account.txt" ^| ^
            findstr "^R....[0123456789][0123456789][0123456789][0123456789].."
    ') do (
        set "FDIR=%%~nxD" & set "FILE=%%~nxF"
        setlocal EnableDelayedExpansion
        ECHO ren "!_ROOT!\!FDIR!\!FILE!" "MSF!FILE:~9,2!DNB.abc"
        endlocal
    )
)

endlocal
exit /B

If you want to check whether the sub-directory name matches the year/month ( YYMM ) portion of the file names, replace the pattern R??????????Account.txt by R????%%~nxD??Account.txt (for both scripts). 如果要检查子目录名称是否与文件名的年/月( YYMM )部分匹配,请用R????%%~nxD??Account.txt替换模式R??????????Account.txtR????%%~nxD??Account.txt (对于两个脚本)。

After having verified the correct output of either script, remove the upper-case ECHO commands to actually rename any files! 在验证了两个脚本的正确输出之后,请删除大写的ECHO命令以实际重命名任何文件!

Basically, both scripts use sub-string expansion to extract the identifier part ( KK ) from the file names. 基本上,两个脚本都使用子字符串扩展来从文件名中提取标识符部分( KK )。 Since there are variables set and read in the same block of code, delayed expansion is required for that. 由于在同一代码块中设置并读取了变量,因此需要延迟扩展 The second approach does not list the sub-directories and files by standard for loops , it uses the dir command , findstr to filter their names and a for /F loop to capture the resulting output for both sub-directories and files. 第二种方法不是按标准的for循环列出子目录和文件,而是使用dir命令findstr过滤其名称,并使用for /F循环来捕获子目录和文件的结果输出。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何重命名包含特定文本字符串的文件? - How to rename files that contain specific text string? 获取特定子文件夹中的文件列表 - get files list in a specific subfolder 如何将特定文件夹的每个子文件夹中的所有 *.pdf 文件移动到每月创建的子文件夹? - How to move all *.pdf files in each subfolder of a specific folder to a monthly created subfolder? 创建批处理文件以删除,重命名和移动多个文件的多个子文件夹 - Create batch file to delete,rename and move multiple files multiple subfolder 将特定子文件夹中的文件移动到另一个子文件夹 - Move files in specific subfolders to another subfolder 如何使用文本文件的内容(制表符分隔格式)重命名文件夹中的文件? - How to use the contents of a text file(tab delimited format) to rename files in a folder? 如何连续批量重命名文件? - How to batch rename files consecutively? 如何使用特定的 XML 节点值批量重命名多个 XML 文件? - How to batch rename multiple XML files using specific XML node value? 如何使用 Windows Bash 递归重命名所有文件和文件夹,包括文件名的特定部分? - How to recursively rename all files and folder including specific part of the filename with Windows Bash? 需要BAT文件来复制和重命名特定树中的所有文件 - Need a BAT file to copy & rename all files in specific tree
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM