开发者

.bat Find and Rename

开发者 https://www.devze.com 2023-01-13 17:36 出处:网络
I have outputted some text files all in the same directory. Each .txt file has within a group number, this number always starts with RXC and can go upwards of 5 characters afterwards, giving us RXCXXX

I have outputted some text files all in the same directory. Each .txt file has within a group number, this number always starts with RXC and can go upwards of 5 characters afterwards, giving us RXCXXXXX i开发者_JAVA技巧 need the script to find this RXC number and rename the file to its corresponding group number, then do the same for all files in the same directory.

Thanks in advance, Joe


using System.IO;
foreach(var file in Directory.GetFiles("."))
{
    var content = File.ReadAllText(file);
    var startIndex = content.IndexOf("RXC");
    var id = content.Substring(startIndex, 8);
    File.Move(file, id);
}


Set objFS = CreateObject("Scripting.FileSystemObject")
strFolder="c:\test"
Set objFolder = objFS.GetFolder(strFolder)
For Each strFile In objFolder.Files
    If objFS.GetExtensionName(strFile) = "txt" Then    
        strFileName = strFile.Name
        Set objFile = objFS.OpenTextFile(strFileName)
        Do Until objFile.AtEndOfStream 
            strLine=objFile.ReadLine
            If InStr(strLine,"RXC" ) > 0 Then
                number=Mid(strLine,4)               
                objFile.Close
                strFile.Name = Trim(number)&".txt"                      
                Exit Do 
            End If          
        Loop       
    End If  
Next 
0

精彩评论

暂无评论...
验证码 换一张
取 消