0
Follow
17
View

Batch How to Batch truncate file names?

dafa1680 注册会员
2023-02-28 22:18

The original file name is as follows:

21001_20170101_340000_1_HK_BCF_201701122132_2060.txt
21001_20170101_340000_1_LY_BCF_201701122132_2059.txt
210 02_20170101_340000_1_NBCLCK_BCF_201701122132_2064.txt
21002_20170101_340000_1_NBCLJK_BCF_201701122132_2063.txt
2 1003_20170101_340000_1_TD_BCF_201701122132_2061.txt
21002_20170101_340000_1_ZY_BCF_201701122132_2062.txt

The

shell script is as follows, where tshort=${tfile%BCF*.txt} bcf. txt deletes all the words after BCF and changes them to bcf.txt. tshort=${tshort# _ } further truncates the preceding 210001_(deletes the smallest part that matches *_* and returns the rest).

for tfile in *.txt
do
  tshort=${tfile%BCF*.txt}BCF.txt
  tshort=${tshort#*_*}
  if [ ${tfile} != ${tshort} ]; then
  mv ${tfile} ${tshort}
 fi
done

The command output is as follows:

20170101_340000_1_HK_BCF.txt
20170101_340000_1_LY_BCF.txt
20170101_340000_1_NBCLCK_BCF.txt
20170101_34000 0_1_NBCLJK_BCF.txt
20170101_340000_1_TD_BCF.txt
20170101_340000_1_ZY_BCF.txt

To keep file names consistent, remove NBCL from some file names. You can do this by using the rename command(change _NBCL to _), i.e.

rename _NBCL _ *BCF.txt

The

rename command is dedicated to renaming files. As you can see from the above example, rename can rename either a single file or a batch file.
Note: The rename command takes three arguments instead of two. The second argument above is left blank to remove the character specified in the first argument.

The command output is as follows:

20170101_340000_1_HK_BCF.txt
20170101_340000_1_LY_BCF.txt
20170101_340000_1_CK_BCF.txt
20170101_340000_1_ JK_BCF.txt
20170101_340000_1_TD_BCF.txt
20170101_340000_1_ZY_BCF.txt

========================================

is a good method, pay attention to the use of wildcards, in fact, the key lies in the use of parameter substitution, the following summary:

${param:-default} If param is empty, the value of the entire expression is default
${#param} The value of the entire expression is the length of param
${param%word} Delete the smallest part matching word starting at the end of param and return the rest
${param%%word} Delete the longest part matching word starting at the end of param and return the rest
${param#word} Delete the smallest part matching word from the param header and return the rest
${param##word} Delete the longest part matching word from the param header and return the rest

The smallest part is the first match, and the longest part is the last match, for example:

Suppose aa=iamlaosongsongdc

then echo ${aa%song*} displays: iamlaosong

while echo ${aa%%song*} displays: iamlao

If you do not use wildcards, echo ${aa%%song} shows iamlaosongsongdc because there is no match.

Add a few more uses

${param:=bar} Checks whether param is empty, and returns its value if it is not; Otherwise assign param to bar and return the value
${param:? If param is empty or absent, output param:bar and terminate the script with an exception
${param:+bar} param is present and not empty, return param only if it is not empty, otherwise return empty(seemingly bar is not used here)

${param:m:n} from the MTH character left of param(0 for the first), take n characters backward, if n is omitted, take all subsequent characters
${param:0-m:n} from the MTH character right of param(0-m equals a negative value), Take n characters backward. If n is omitted, take all subsequent characters
${param:m} starting from the MTH character left of param(0 being the first), and take all characters backward

heping8815 注册会员
2023-02-28 22:18

Windows系统中,文件名。首先,打开命令提示符,然后输入“ren”命令,接着输入要修改的文件名,最后输入代莱文件名即可。例如,输入“ren oldname.txt newname.txt”,就可以将文件oldname.txt的文件名修改为newname.txt。

zg9954 注册会员
2023-02-28 22:18

Batch scripts can be used to batch truncate file names. Here is a simple batch script that can implement all the files in the current directory whose file names are longer than 10, with the first 20 bits truncated and renamed:

@echo off
for %%F in (*) do (
  set "FILENAME=%%~nF"
  set "EXT=%%~xF"
  if "!FILENAME!" neq "!FILENAME:~0,10!" (
    set "NEWNAME=!FILENAME:~0,20!!EXT!"
    echo Renaming "%%F" to "!NEWNAME!"
    ren "%%F" "!NEWNAME!"
  )
)

The script traverses all files in the current directory. For each file, it determines whether the length of the file name is longer than 10 characters. If it is longer, it intercepts the first 20 characters, adds the original file extension, and renames the file with the truncated name. To execute the script, open the command prompt window, switch to the desired directory, and then run the script.

Note that this script only renames files whose file names are longer than 10 characters in length, in order to avoid duplicate names. if you need to rename all files, you can remove the if statement.

happyjian88 注册会员
2023-02-28 22:18

You can refer to this to modify

@echo off
set /p firststr="请输入文件名前缀:"
 
set /p kuozhan="请输入文件扩展名:"
 
set a=0
setlocal EnableDelayedExpansion
dir /b .\*.%kuozhan% | find /c /v "" >> .\tmp.txt
set /p c=<.\tmp.txt
del /a /f /q .\tmp.txt 
 
for %%i in (*.%kuozhan%) do (
set /a a+=1
if !a! gtr %c% (goto aa)
echo %firststr%!a!
echo %%i 
ren "%%i" "%firststr%!a!.%kuozhan%"
)
:aa
pause

effect611 注册会员
2023-02-28 22:18
I don't know if this problem has been solved for you, if not:

If you have solved the problem, I hope you can share the solution, write a blog about it, and put a link to it in the comments section to help more people. ^-^ /div>
daidaikeai0816 注册会员
2023-02-28 22:18
< div class = "md_content_show e397 data - v - 3967" = "" >

thank you!! It worked with your code, but there's a problem. If you want to set it to, if the file is truncated and the name is duplicated, how do you change it by adding a _1 _2 suffix,

cyyhao 注册会员
2023-02-28 22:18

Question, I will solve this problem for you, if it helps, also hope to adopt, click on the right side of the answer can be adopted.

Step:

1, first open the folder to extract the file name, create a txt file

2. Edit the txt text and enter the following batch command to save the text file.

dir . /b > a.txt

Tip: In this step, if you need to extract a file name with a fixed suffix, change *.* to the corresponding suffix.

3. Change the suffix of txt file to.bat and convert it into a batch file. 4. Double-click the batch file to extract the file name

zjj04250511 注册会员
2023-02-28 22:18

You can use a batch script to rename a file whose file name is longer than 10 characters in the current directory to the first 20 characters.


@echo off
setlocal enabledelayedexpansion
for %%f in (*) do (
    set "filename=%%~nf"
    set "ext=%%~xf"
    if "!filename!" EQU "!filename:~0,10!" (
        echo Skipping file %%f
    ) else (
        set "newname=!filename:~0,20!!ext!"
        ren "%%f" "!newname!"
        echo Renamed file %%f to !newname!
    )
)

The script first uses a for loop to iterate through all the files in the current directory. For each file, it extracts the file name and extension, then checks to see if the file name is longer than 10 characters. If the file name is less than 10 characters long, the file is skipped. Otherwise, it uses the set command to truncate the file name to the first 20 characters, combines the new file name with the extension, and renames the file using the ren command.

Please note that this script will change the file name, be sure to prepare

before running the script.
jiaoliuyang321 注册会员
2023-02-28 22:18

You can use the following batch script to rename the first 20 characters of all file names in the current directory that are longer than 10:

@echo off
setlocal EnableDelayedExpansion

for %%f in (*) do (
    set "filename=%%~nf"
    if "!filename!" neq "" (
        set "newname=!filename:~0,20!%%~xf"
        if "!newname!" neq "%%~nxf" (
            ren "%%~f" "!newname!"
            echo "%%~f" renamed to "!newname!"
        )
    )
)

echo All files processed.
pause
dangrongjing 注册会员
2023-02-28 22:18

@echo off
setlocal enabledelayedexpansion

rem 获取当前目录
set "current_dir=%cd%"

rem 遍历目录下所有文件
for %%f in ("%current_dir%\*.*") do (
    set "filename=%%~nxf"
    rem 判断文件名长度是否超过10
    if "!filename:~10!" neq "" (
        rem 获取文件名的前20位
        set "new_filename=!filename:~0,20!"
        rem 构造新的文件路径
        set "new_filepath=%current_dir%\!new_filename!"
        set "old_filepath=%current_dir%\!filename!"
        rem 重命名文件
        ren "!old_filepath!" "!new_filename!"
        echo 将文件 !filename! 重命名为 !new_filename!
    )
)

First gets the current directory by %cd%, then uses the for loop to iterate through all the files in the directory.
For each file, use the set command to save the filename into the filename variable, and then determine whether the filename is longer than 10.
If so, use string interception syntax! Filename: ~ 0, 20! Get the first 20 digits of the file name, construct a new file path, and then rename the file using the ren command.
Finally the result of the rename is printed.

About the Author

Question Info

Publish Time
2023-02-28 22:18
Update Time
2023-02-28 22:18