Имя пользователя:
Пароль:
 

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

Ветеран


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

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


Цитата sweetpleasure:
Я просто думала, что на ssed так же просто, как и удалить, можно вставить текст. »
Может быть. Я с ним не работал.


Цитата sweetpleasure:
Подойдёт) »
Попробуйте так:
читать дальше »
Код: Выделить весь код
Option Explicit

Dim strSourceFile
Dim strDestFile
Dim lngInsertPosition

Dim objFSO

Dim strSourceContent
Dim strDestContent

Dim strTempFile
Dim strBackupFile

Set objFSO = WScript.CreateObject("Scripting.FileSystemObject")

If WScript.Arguments.Count <> 3 Then
	WScript.Echo "Usage: cscript.exe //nologo """ & WScript.ScriptName & """ <Source file> <Destination file> <Line number for insert>"
	WScript.Quit 1
End If

With objFSO
	strSourceFile = WScript.Arguments.Item(0)
	
	If Not .FileExists(strSourceFile) Then
		WScript.Echo "Source file [" & strSourceFile & "] not found."
		WScript.Quit 2
	End If
	
	strDestFile = WScript.Arguments.Item(1)
	
	If Not .FileExists(strDestFile) Then
		WScript.Echo "Destination file [" & strDestFile & "] not found."
		WScript.Quit 3
	End If
	
	If Not IsNumeric(WScript.Arguments.Item(2)) Then
		WScript.Echo "Line number must be a numeric value."
		WScript.Quit 4
	End If
	
	lngInsertPosition = CLng(WScript.Arguments.Item(2))
	
	If lngInsertPosition < 0 Then
		WScript.Echo "Line number can't be less than zero."
		WScript.Quit 5
	End If
	
	With .OpenTextFile(strSourceFile)
		strSourceContent = .ReadAll()
		.Close
	End With
	
	With .OpenTextFile(strDestFile)
		strDestContent = ""
		
		Do Until .AtEndOfStream
			If .Line <> lngInsertPosition Then
				strDestContent = strDestContent & .ReadLine() & vbCrLf
			Else
				strDestContent = strDestContent & strSourceContent & .ReadLine() & vbCrLf
			End If
		Loop
		
		.Close
	End With
	
	strTempFile = GetTemporaryName()
	
	With .CreateTextFile(strTempFile)
		.Write strDestContent
		.Close
	End With
	
	strBackupFile = .BuildPath(.GetParentFolderName(strDestFile), .GetBaseName(strDestFile) & ".bak")
	
	If .FileExists(strBackupFile) Then
		.DeleteFile strBackupFile
	End If
	
	.MoveFile strDestFile, strBackupFile
	.CopyFile strTempFile, strDestFile
	.DeleteFile strTempFile
End With

WScript.Quit
'=============================================================================

'=============================================================================
' Серый форум / 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
'=============================================================================
Это сообщение посчитали полезным следующие участники:

Отправлено: 15:12, 28-05-2012 | #5