使用BATCH,VBSCRIPT或BASH SHELL重命名多个文件

问题描述 投票:1回答:3

我在文件夹A中有一些文件,我需要做的是将文件名重命名为不同的模式示例:TTFILE-201109265757.dat to YTL.MSZSH1.ch1.201109265757_0001.0001.dat

其中YTL,MSZSH1,ch1是前缀,然后是文件名,然后是_,然后是序列号

文件名模式应该是这样的:YTL.MSZSH1.ch1.filename_SequenceNumber.SequenceNumber,其中SequenceNumber是4位,在9999之后重置为0。

bash vbscript batch-file
3个回答
2
投票

这个小bash脚本应该做的工作:)只需在参数列表中使用相关文件调用它或用$@替换$(ls)

#!/bin/bash                                                                     
counter=1
prefix="YTL.MSZSH1.ch1."
for i in "$@" ; do
    file=$(basename "$i")
    counter=$(printf "%04d" $counter)
    mv "$i" "$prefix${file/TTFILE-/}_$counter.$counter.dat"
    counter=$(( $counter+1 ))
done

2
投票

在Windows环境中,这是我将运行的脚本:

@echo off
setlocal EnableDelayedExpansion
pushd %1

set c=0
for /r %%i in ( %2-*.dat ) do (
  set filename=%%~ni
  set digits=!filename:%2-=!
  ren "%%i" %3.%4.%5.!digits!_!c.!c!.dat
  set /a c+=1
  if !c! equ 10000 set c=0
)

popd

要运行它:script.cmd "D:\Test Area" TTFILE YTL MSZSH1 ch1,其中D:\Test Area是包含.dat文件的目录,以下参数是要使用的前缀。

如果D:\Test Area包含子目录,则包含在其中的.dat文件也将被重命名,但序列号不会在两个不同的子文件夹之间重置。


0
投票

这是vbscript中的方式

    Dim objFSO,myFolder,objFolder,colFiles,objFile,newName,i,n
    set sh=createobject("wscript.shell")
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    myFolder = "C:\users\eng\desktop\Scripts"    '' here you can write the path for your folder
    Set objFolder = objFSO.GetFolder(myFolder)
    Set colFiles = objFolder.Files

    i=0:n="0000"
    For Each objFile in colFiles
     if Not instr(1,objFile.name,"YTL.MSZSH1.ch1.",1) > 0 then  ''check if the file name change.this step to avoid change file name again after we rename

    newName=replace(objFile.Name,"TTFILE-","YTL.MSZSH1.ch1.")   ''replace "TTFILE-" with "YTL.MSZSH1.ch1." in the file name
    newName=replace(newName,right(newName,4),"_"&n&"."&n&".dat")   ''replace in modefiy newName ".dat" to "_0000.0000.dat" in the file name

    objFSO.getfile(objFile).name=newName    ''change the file name with newName
    sh.popup objfile,1,"In_The_Name_Of_Allah"

    i=i+1

       If i < 10 Then
            n= CStr("000" & i)
        ElseIf i < 100 Then
            n= CStr("00" & i)
        ElseIf i < 1000 Then
            n= CStr("0" & i)
        Else
            n= i
        End If
    End If
Next
wscript.quit
© www.soinside.com 2019 - 2024. All rights reserved.