Reputation: 431
I have a numbers of files in folder A, what I need to do is to rename the filename to different pattern example: TTFILE-201109265757.dat
to YTL.MSZSH1.ch1.201109265757_0001.0001.dat
Where YTL, MSZSH1, ch1 is prefix then follow by the filename then _
then sequence number
The filename pattern should be like this: YTL.MSZSH1.ch1.filename_SequenceNumber.SequenceNumber
where SequenceNumber
is 4 digit, reset to 0 after 9999.
Upvotes: 1
Views: 1452
Reputation: 587
this is the way in 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
Upvotes: 0
Reputation: 136
In a Windows environment, here is the script I would run:
@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
To run it: script.cmd "D:\Test Area" TTFILE YTL MSZSH1 ch1
, where D:\Test Area
is the directory containing the .dat
files and the following arguments are the prefixes to use.
If D:\Test Area
contains sub-directories, the .dat
files contained in them will also be renamed, but the sequence number will not be reset between two different sub-folders.
Upvotes: 2
Reputation: 3908
This little bash script should do the work :) Just call it with the files in question in the argument list or replace $@
with $(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
Upvotes: 2