String 使用批处理文件提取字符串的可变部分以用于重命名txt文件
我使用批处理文件来处理文本文件,其格式为:CLL*1.txt,CLLM*2.txt,位于特定的“下载”文件夹中。所有文件都包含以下格式的字符串: “文件引用:0xxxx”,其中xxxx是唯一的数字标识符 我正在尝试使用以下脚本将文件重命名为CLL*xxxx.txt(其中xxxx替换整数后缀),但没有取得多大成功。有人能帮忙吗String 使用批处理文件提取字符串的可变部分以用于重命名txt文件,string,variables,batch-file,extract,batch-rename,String,Variables,Batch File,Extract,Batch Rename,我使用批处理文件来处理文本文件,其格式为:CLL*1.txt,CLLM*2.txt,位于特定的“下载”文件夹中。所有文件都包含以下格式的字符串: “文件引用:0xxxx”,其中xxxx是唯一的数字标识符 我正在尝试使用以下脚本将文件重命名为CLL*xxxx.txt(其中xxxx替换整数后缀),但没有取得多大成功。有人能帮忙吗 set target="S:\download\" SetLocal EnableDelayedExpansion enableextensions for /f "
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
findstr
不会向您返回任何单个值。它将只搜索一个字符串并返回整行。试试这个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
另存为myscript.vbs
并运行它
C:\download_folder> cscript //nologo myscript.vbs c:\download_folder
谢谢使用vbscript格式化txt文件,但根据内容使用批处理文件进行初始重命名和移动。相信使用令牌和delims只会传递findstr/b传递的字符串的一部分