Имя пользователя:
Пароль:  
Помощь | Регистрация | Забыли пароль?  

Показать сообщение отдельно

Ветеран


Сообщения: 27449
Благодарности: 8087

Профиль | Отправить PM | Цитировать


Mikhael2011, поздравляю! Вы созрели для WSH:
читать дальше »
Код: Выделить весь код
Option Explicit

Dim objFSO
Dim objTSSource
Dim objTSDest

Dim strString4Find
Dim strString4Append

Dim strSourceFile
Dim strTempFile
Dim strBackupFile

Dim strLine


If WScript.Arguments.Count = 3 Then
	With WScript.CreateObject("Scripting.FileSystemObject")
		strSourceFile = .GetAbsolutePathName(WScript.Arguments.Item(0))
		
		If .FileExists(strSourceFile) Then
			strString4Find   = WScript.Arguments.Item(1)
			strString4Append = WScript.Arguments.Item(2)
			
			strTempFile = GetTemporaryName()
			
			Set objTSSource = .OpenTextFile(strSourceFile)
			Set objTSDest   = .CreateTextFile(strTempFile)
			
			Do Until objTSSource.AtEndOfStream
				strLine = objTSSource.ReadLine()
				
				objTSDest.WriteLine strLine
				
				If InStr(1, strLine, strString4Find, vbTextCompare) > 0 Then
					objTSDest.WriteLine strString4Append
				End If
			Loop
			
			objTSDest.Close
			objTSSource.Close
			
			Set objTSDest   = Nothing
			Set objTSSource = Nothing
			
			strBackupFile = .BuildPath(.GetParentFolderName(strSourceFile), .GetBaseName(strSourceFile) & ".bak")
			
			.CopyFile strSourceFile, strBackupFile, True
			.CopyFile strTempFile, strSourceFile, True
		Else
			WScript.Echo "Can't find source file [" & strSourceFile & "]."
		End If
	End With
Else
	WScript.Echo "Usage: cscript.exe //nologo " & WScript.ScriptName & " <Source file> <String for find> <String for append>"
End If

WScript.Quit 0

'=============================================================================
' Серый форум / vbscript: генерация пути для временного файла или папки
' (http://forum.script-coding.com/viewtopic.php?id=1221)
'=============================================================================
Function GetTemporaryName()
	Const TemporaryFolder = 2
	
	Dim strTempName
	
	With WScript.CreateObject("Scripting.FileSystemObject")
		Do
			strTempName = .BuildPath(.GetSpecialFolder(TemporaryFolder), .GetTempName)
		Loop While .FileExists(strTempName) Or .FolderExists(strTempName)
	End With
	
	GetTemporaryName = strTempName
End Function
'=============================================================================

Отправлено: 06:27, 26-05-2013 | #7