使用文件内容重命名 [英] Use the contents of a file to rename it
问题描述
我有大约20 MB大小且具有随机文件名的二进制数据文件,所有文件均以"AA"开头.在每个文件的内容中,它们在固定位置具有一个特定的字符串(在所有文件中,从第2086个字节开始).我想读取由2个单词组成的字符串(在下面的示例中,它们之间有1个空格,例如"MyName Sirname"),并将其与文件创建日期一起使用以重命名文件.
I have binary datafiles that are approximately 20 MB in size and that have random filenames, all starting with "AA". In each file's content, they have a specific string at a fixed position (in all files starting at 2086'th byte). I want to read that string consisting of 2 words (with 1 space in between like "MyName Sirname" in the example below) and use it along with the file creation date to rename the file.
这是一个缩短的示例文件(前3Kb):
dl.dropboxusercontent.com/u/18286876/short.zhr
Here is a shortened example file (first 3Kb):
dl.dropboxusercontent.com/u/18286876/short.zhr
我们要将此特定文件重命名为"MyName Sirname YYYY-MM-DD".
We want to rename this specific file to "MyName Sirname YYYY-MM-DD".
最好是脚本循环访问currant目录中所有以"AA"开头的文件.该脚本可以是vbs或批处理+ vbs组合,无论哪种方法都更简单.
It would be best if the script iterates through all files starting with "AA" in the currant directory. The script could be vbs or batch+vbs combination whatever is simplier.
这似乎是重复的,但原始问题缺少细节,错误地将重点放在批处理上,给出的答案还不够.
This may seem a duplicate but the original question lacked detail, was wrongly focused on batch and the given answer was not sufficient.
推荐答案
假设您的所有文件都在同一文件夹C:\some\where
中,这样的事情可能会起作用:
Something like this might work, assuming that all of your files are in the same folder C:\some\where
:
Const offset = 2085
Set fso = CreateObject("Scripting.FileSystemObject")
For Each f In fso.GetFolder("C:\some\where").Files
If Left(f.Name, 2) = "AA" Then
Set stream = f.OpenAsTextStream
stream.Skip(offset)
words = Array()
Do
length = Asc(stream.Read(1))
If length <> 0 Then
ReDim Preserve words(UBound(words)+1)
words(UBound(words)) = stream.Read(length)
End If
Loop Until length = 0 Or stream.AtEndOfStream
stream.Close
If UBound(words) >= 1 Then
fdate = Year(f.DateCreated) & "-" & Right("0" & Month(f.DateCreated), 2) _
& "-" & Right("0" & Day(f.DateCreated), 2)
f.Name = words(0) & " " & words(1) & " " & fdate _
& "." & fso.GetExtensionName(f.Name)
End If
End If
Next
这篇关于使用文件内容重命名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!