0

我正在使用批处理文件来处理具有以下形式名称的文本文件:CLL*1.txt、CLLM*2.txt 位于特定的“下载”文件夹中。所有文件都包含以下形式的字符串:“File Reference : 0xxxx”,其中 xxxx 是唯一的数字标识符。

我正在尝试使用以下脚本将文件重命名为 CLL*xxxx.txt(其中 xxxx 替换整数后缀),但没有取得多大成功。谁能帮忙?:

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
4

1 回答 1

0

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
于 2010-10-11T13:39:15.617 回答