Цитата Hixi:
1. Шаблон нужен исключительно краткий. »
|
Это три формата/функции: год, месяц, дата в нестандартном формате.
В общем, держите:
читать дальше »
Код:

Option Explicit
Dim strIniFile
Dim objDictionary
Dim objRegExp
Dim objRegExp4Eval
Dim objMatchCollection
Dim strLine
Dim strParameter
Dim strValue
Dim strSourceFolder
Dim objFile
Dim strText
Dim strPhrase
Dim strDate
Dim strDestPath
If WScript.Arguments.Count = 1 Then
With WScript.CreateObject("Scripting.FileSystemObject")
strIniFile = WScript.Arguments.Item(0)
If .FileExists(strIniFile) Then
Set objDictionary = WScript.CreateObject("Scripting.Dictionary")
Set objRegExp = WScript.CreateObject("VBScript.RegExp")
Set objRegExp4Eval = WScript.CreateObject("VBScript.RegExp")
objRegExp.Pattern = "^\s*("")?([^\1=]+?)\1\s*=\s*("")?(.+?)\3\s*$"
objRegExp4Eval.Pattern = "(\?)([^\?]+?)\1"
strSourceFolder = WScript.CreateObject("WScript.Shell").CurrentDirectory
WScript.Echo "Ini file: [" & strIniFile & "]"
WScript.Echo "--------------------------------------------"
With .OpenTextFile(strIniFile)
Do Until .AtEndOfStream
strLine = .ReadLine()
If objRegExp.Test(strLine) Then
Set objMatchCollection = objRegExp.Execute(strLine)
strParameter = objMatchCollection.Item(0).SubMatches(1)
strValue = objMatchCollection.Item(0).SubMatches(3)
Do While objRegExp4Eval.Test(strValue)
strValue = objRegExp4Eval.Replace(strValue, Eval(objRegExp4Eval.Execute(strValue).Item(0).Submatches.Item(1)))
Loop
WScript.Echo "Parameter: [" & strParameter & "]"
WScript.Echo "Value: [" & strValue & "]"
WScript.Echo
Else
WScript.Echo "Can't determine line [" & strLine & "]"
End If
If Not objDictionary.Exists(strParameter) Then
objDictionary.Add strParameter, strValue
Else
WScript.Echo "Duplicate parameter [" & strParameter & "] found"
End If
Loop
.Close
End With
WScript.Echo
WScript.Echo "Source folder: [" & strSourceFolder & "]"
WScript.Echo "--------------------------------------------"
For Each objFile In .GetFolder(strSourceFolder).Files
If LCase(.GetExtensionName(objFile.Name)) = "txt" Then
WScript.Echo "File: " & objFile.Path
With objFile.OpenAsTextStream()
strText = StrConvert(.ReadAll(), "windows-1251", "cp866")
.Close
End With
For Each strPhrase In objDictionary.Keys
If InStr(1, strText, strPhrase, vbTextCompare) > 0 Then
WScript.Echo Space(4) & "Phrase found: " & strPhrase
strDestPath = objDictionary.Item(strPhrase)
CreateFolderEx strDestPath
objFile.Move .BuildPath(strDestPath, "\")
WScript.Echo Space(4) & "Moved to folder [" & strDestPath & "]"
End If
Next
End If
Next
objDictionary.RemoveAll
Set objRegExp = Nothing
Set objDictionary = Nothing
Else
WScript.Echo "Can't find ini file [" & strIniFile & "]"
End If
End With
Else
WScript.Echo "Usage: cscript.exe //nologo """ & WScript.ScriptName & """ <ini file>"
End If
WScript.Quit 0
'=============================================================================
'=============================================================================
Sub CreateFolderEx(strPath)
With WScript.CreateObject("Scripting.FileSystemObject")
If Not .FolderExists(strPath) Then
CreateFolderEx .GetParentFolderName(strPath)
.CreateFolder strPath
End If
End With
End Sub
'=============================================================================
'=============================================================================
' HKEY_CLASSES_ROOT\MIME\Database\Charset
' cp866, windows-1251, koi8-r, unicode, utf-8, _autodetect
'=============================================================================
Function StrConvert(ByVal strText, ByVal strSourceCharset, ByVal strDestCharset)
Const adTypeText = 2
Const adModeReadWrite = 3
With WScript.CreateObject("ADODB.Stream")
.Type = adTypeText
.Mode = adModeReadWrite
.Open
.Charset = strSourceCharset
.WriteText strText
.Position = 0
.Charset = strDestCharset
StrConvert = .ReadText
.Close
End With
End Function
'=============================================================================
и играйтесь.
Вызов:
Код:

cscript.exe //nologo "Script.vbs" "Путь\ini-файл"
Внутри ini-файла можно указывать выражения на VBScript, обрамляя их символами «?» — лепите, что пожелаете. Пробельные символы обрезаются, если потребны — обрамляйте кавычками. Кодировка ini-файла — «windows-1251».
Пример ini-файла, на основе
приведённых Вами данных:
Код:

Родина = C:\try\?CStr(Year(Now()))?\?Right("00" & CStr(Month(Now())), 2)?\?CStr(Year(Now())) & "_" & Right("00" & CStr(Month(Now())), 2) & "_" & Right("00" & CStr(Day(Now())), 2)?\прием
Горы = E:\asd\?CStr(Year(DateAdd("d", -1, Now())))?\?Right("00" & CStr(Month(DateAdd("d", -1, Now()))), 2)?\?CStr(Year(DateAdd("d", -1, Now()))) & "_" & Right("00" & CStr(Month(DateAdd("d", -1, Now()))), 2) & "_" & Right("00" & CStr(Day(DateAdd("d", -1, Now()))), 2)?\прием
Привет = D:\789
Тополь-М = F:\14\тру
Возможный результат:
читать дальше »
Код:

Ini file: [0001.ini]
--------------------------------------------
Parameter: [Родина]
Value: [C:\try\2013\06\2013_06_03\прием]
Parameter: [Горы]
Value: [E:\asd\2013\06\2013_06_02\прием]
Parameter: [Привет]
Value: [D:\789]
Parameter: [Тополь-М]
Value: [F:\14\тру]
Source folder: [E:\Песочница\0267]
--------------------------------------------
File: E:\Песочница\0267\0001.txt
File: E:\Песочница\0267\0002.txt
File: E:\Песочница\0267\0003.txt
File: E:\Песочница\0267\0004.txt
Phrase found: Горы
Moved to folder [E:\asd\2013\06\2013_06_02\прием]
File: E:\Песочница\0267\0005.txt
Phrase found: Родина
Moved to folder [C:\try\2013\06\2013_06_03\прием]
***** script completed - exit code: 0 *****