Truncate a file: Difference between revisions
Content added Content deleted
mNo edit summary |
|||
Line 951: | Line 951: | ||
NOTE[4]: Designed for regular text files. Does not work when files contain: chars 0x00 and 0xFF as of bash 4.2 - these chars are treated differently in read -N. For this reason use of method#4 is not prefer over dd(1) or truncate(1) when using binary files, or large files. [4b] http://unix.stackexchange.com/questions/8618/script-that-keep-reading-a-stream,[4a] https://groups.google.com/forum/#!topic/gnu.bash.bug/a2rjQHpQYSU |
NOTE[4]: Designed for regular text files. Does not work when files contain: chars 0x00 and 0xFF as of bash 4.2 - these chars are treated differently in read -N. For this reason use of method#4 is not prefer over dd(1) or truncate(1) when using binary files, or large files. [4b] http://unix.stackexchange.com/questions/8618/script-that-keep-reading-a-stream,[4a] https://groups.google.com/forum/#!topic/gnu.bash.bug/a2rjQHpQYSU |
||
=={{header|VBScript}}== |
|||
<lang vb> |
|||
Sub truncate(fpath,n) |
|||
'Check if file exist |
|||
Set objfso = CreateObject("Scripting.FileSystemObject") |
|||
If objfso.FileExists(fpath) = False Then |
|||
WScript.Echo fpath & " does not exist" |
|||
Exit Sub |
|||
End If |
|||
content = "" |
|||
'stream the input file |
|||
Set objinstream = CreateObject("Adodb.Stream") |
|||
With objinstream |
|||
.Type = 1 |
|||
.Open |
|||
.LoadFromFile(fpath) |
|||
'check if the specified size is larger than the file content |
|||
If n < .Size Then |
|||
content = .Read(n) |
|||
Else |
|||
WScript.Echo "The specified size is larger than the file content" |
|||
Exit Sub |
|||
End If |
|||
.Close |
|||
End With |
|||
'write the truncated version |
|||
Set objoutstream = CreateObject("Adodb.Stream") |
|||
With objoutstream |
|||
.Type = 1 |
|||
.Open |
|||
.Write content |
|||
.SaveToFile fpath,2 |
|||
.Close |
|||
End With |
|||
Set objinstream = Nothing |
|||
Set objoutstream = Nothing |
|||
Set objfso = Nothing |
|||
End Sub |
|||
'testing |
|||
Call truncate("C:\temp\test.txt",30) |
|||
</lang> |
|||
=={{header|ZX Spectrum Basic}}== |
=={{header|ZX Spectrum Basic}}== |