views:

44

answers:

1

I'm using batch files to process text files with names of the form: CLL*1.txt, CLLM*2.txt located in a specific "download" folder. All files contain a string of the form: "File Reference : 0xxxx", where xxxx is a unique numerical identifier.

I am trying, without much success, to use the following script to rename the file to CLL*xxxx.txt (where xxxx replaces the integer suffix). Can anyone help?:

set target="S:\download\"

SetLocal EnableDelayedExpansion enableextensions 

for /f "usebackq tokens=2 delims=:" %%i IN (`findstr /b "File Reference  :" %target%CLL*.txt`) do ( 

   ren %target%CLL*.txt CLL*%%i.txt

)


Endlocal
A: 

findstr will not return any single values to you. It will only search for a string and return the whole line. try this vbscript

Set objFS = CreateObject( "Scripting.FileSystemObject" )
Set d = CreateObject("Scripting.Dictionary")
strFolder= WScript.Arguments(0)
Set objFolder = objFS.GetFolder(strFolder)
For Each strFile In objFolder.Files
    If objFS.GetExtensionName(strFile) = "txt" Then    
        strFileName = strFile.Name          
        Set objFile = objFS.OpenTextFile(strFile)       
        Do Until objFile.AtEndOfStream 
            strLine=objFile.ReadLine
            If InStr(strLine,"File Reference") > 0 Then
                s=Split(strLine,"File Reference : ")
                num=Split( s(UBound(s))," ")
                number=Mid(num(0),2) 'get the number
                strNewFileName = "CLL"&CStr(number)&".txt"
                objFile.Close               
                strFile.Name = strNewFileName
                Exit Do
            End If          
        Loop     
        Set objFile=Nothing
    End If  
Next 

save as myscript.vbs and run it

C:\download_folder> cscript //nologo myscript.vbs c:\download_folder
ghostdog74
Thanks. Use vbscript to format the txt files, but do use batch files to initially rename and move them depending on content. Believed that using tokens and delims would deliver just part of a string delivered by findstr /b
A Desai