我在文件夹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脚本应该做的工作:)只需在参数列表中使用相关文件调用它或用$@
替换$(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
在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
文件也将被重命名,但序列号不会在两个不同的子文件夹之间重置。
这是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