views:

41

answers:

2

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 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

A: 
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);
}
Rotsor
Sorry just edited subject, I need this in a .bat script.Thanks for your reply.
jmituzas
You can call csharp from the .bat script. :)I've spent half an hour trying to solve a similar task with unix shell scripts, then solved it with csharp in less than five minutes.
Rotsor
Your right wrote it to a .vbs file but am receiving errors?Line: 1 Char: 16Error: Expected end of statement
jmituzas
Actually it is a script for Mono CsharpRepl (http://www.mono-project.com/CsharpRepl) shell.
Rotsor
A: 
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 
ghostdog74