Last active
April 24, 2019 11:20
-
-
Save end2endzone/51fbc9b35638c45cf2a421e016824ac7 to your computer and use it in GitHub Desktop.
Force EOL to CRLF on Windows with this Visual Basic Script.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'Define the list of accepted file extensions | |
Set extensions = New List | |
extensions.Add( "h" ) | |
extensions.Add( "hpp" ) | |
extensions.Add( "cpp" ) | |
extensions.Add( "txt" ) | |
extensions.Add( "md" ) | |
extensions.Add( "bat" ) | |
extensions.Add( "cmd" ) | |
extensions.Add( "ps1" ) | |
extensions.Add( "gitignore" ) | |
extensions.Add( "in" ) | |
extensions.Add( "xml" ) | |
extensions.Add( "" ) 'file without an extension should be converted to CRLF. | |
Set allfiles = GetFilesRecursive(GetScriptDirectory()) | |
Dim file | |
For Each file In allfiles.GetArray | |
If file <> GetCurrentVbsFile() Then 'Skip this is our running script file | |
Dim extension | |
extension = GetFileExtension(file) | |
If ListContains(extensions, extension) Then | |
WScript.Echo "file: " & file | |
ForceCRLFLineEnddings file | |
Else | |
WScript.Echo "skipped: " & file | |
End If | |
Else | |
WScript.Echo "skipped: " & file | |
End If | |
Next | |
'******************************************************************** | |
' Function definitions | |
'******************************************************************** | |
' Read a file and store the content to a string | |
Function ReadFile(path) | |
Const ForReading = 1 | |
Dim fso, f | |
Set fso = CreateObject("Scripting.FileSystemObject") | |
'WScript.Echo "ReadFile(): " & path | |
Set f = fso.OpenTextFile(path, ForReading) 'false and -1 is required to allow opening a text file which is not ASCII but unicode. | |
ReadFile = f.ReadAll | |
f.Close | |
Set f= Nothing | |
Set fso = Nothing | |
End Function | |
' Write a string to a file | |
Function WriteFile(path, text) | |
Const ForWriting = 2 | |
Dim fso, f | |
Set fso = CreateObject("Scripting.FileSystemObject") | |
'WScript.Echo "WriteFile(): " & path | |
Set f = fso.OpenTextFile(path, ForWriting) | |
f.Write text | |
f.Close | |
Set f= Nothing | |
Set fso = Nothing | |
End Function | |
' Replace all EOL characters in a string by CRLF | |
Function ForceCRLFLineEnddings(path) | |
CR = chr(13) | |
LF = chr(10) | |
content = ReadFile(path) | |
content2 = replace(content, LF, CR & LF) | |
content3 = replace(content2, CR & CR & LF, CR & LF) | |
If content <> content3 Then | |
WriteFile path, content3 | |
End If | |
End Function | |
' Returns true If obj is found within myList | |
Function ListContains(myList, obj) | |
Dim element | |
ListContains = false | |
If myList.HasElements Then | |
For Each element In myList.GetArray | |
'WScript.Echo "element=" & element & " obj=" & obj | |
If element = obj Then | |
ListContains = true | |
End If | |
Next | |
End If | |
End Function | |
' Returns the file extension of the given path | |
Function GetFileExtension(path) | |
Set objFSO = CreateObject("Scripting.FileSystemObject") | |
GetFileExtension = objFSO.getextensionname(path) | |
Set objFSO = Nothing | |
End Function | |
' Get the path of the current running VBS file. | |
Function GetCurrentVbsFile() | |
GetCurrentVbsFile = Wscript.ScriptFullName | |
End Function | |
' Get the directory of the current running VBS file. | |
Function GetScriptDirectory() | |
GetScriptDirectory = ParentFolder(GetCurrentVbsFile()) | |
End Function | |
' Returns the name of the parent folder of the given path | |
Function ParentFolderName(ByVal path)' As String | |
vArray = Split(path, "\") | |
size = UBound(vArray) | |
ParentFolder = vArray(size - 1) | |
End Function | |
' Returns the path of the parent folder of the given path | |
Function ParentFolder(ByVal path)' As String | |
Set objFSO = CreateObject("Scripting.FileSystemObject") | |
Set objFile = objFSO.GetFile(path) | |
sName = objFile.Name | |
sPath = objFile.Path | |
ParentFolder = Left(sPath, Len(sPath)-Len(sName)-1) | |
Set objFile = Nothing | |
Set objFSO = Nothing | |
End Function | |
' Get the list of files within the given directory path. | |
' Note: the Function is not recursive and does not returns the list of directories within the given path. | |
Function GetFiles(path) | |
Set objFSO = CreateObject("Scripting.FileSystemObject") | |
Set objFolder = objFSO.GetFolder(path) | |
Set GetFiles = New List | |
'Get all files in given directory | |
Set colFiles = objFolder.Files | |
For Each objFile in colFiles | |
GetFiles.Add( objFile.Path ) | |
Next | |
Set colFiles = nothing | |
Set objFolder = Nothing | |
Set objFSO = Nothing | |
End Function | |
' Get the list of directories within the given directory path. | |
' Note: the Function is not recursive. | |
Function GetSubFolders(path) | |
Set objFSO = CreateObject("Scripting.FileSystemObject") | |
Set objFolder = objFSO.GetFolder(path) | |
Set GetSubFolders = New List | |
'Get all files in given directory | |
Set colFolders = objFolder.SubFolders | |
For Each objFolder in colFolders | |
GetSubFolders.Add( objFolder.Path ) | |
Next | |
Set colFolders = nothing | |
Set objFolder = Nothing | |
Set objFSO = Nothing | |
End Function | |
' Get the list of files within the given directory path. | |
' Note: the Function is recursive. | |
Function GetFilesRecursive(path) | |
Set objFSO = CreateObject("Scripting.FileSystemObject") | |
Set objFolder = objFSO.GetFolder(path) | |
Set GetFilesRecursive = New List | |
'Get all files in the given directory | |
Set files = GetFiles(path) | |
If files.HasElements Then | |
For Each element In files.GetArray | |
GetFilesRecursive.Add( element ) | |
Next | |
End If | |
Set files = nothing | |
'Get all subfolders in the given directory | |
Set subfolders = GetSubFolders(path) | |
If subfolders.HasElements Then | |
For Each folderPath In subfolders.GetArray | |
'Get files of the current subfolder | |
'WScript.Echo "folderPath=" & folderPath | |
Set subfiles = GetFilesRecursive(folderPath) | |
If subfiles.HasElements Then | |
For Each subSilePath In subfiles.GetArray | |
GetFilesRecursive.Add( subSilePath ) | |
Next | |
End If | |
Set subfiles = nothing | |
Next | |
End If | |
Set subfolders = nothing | |
Set objFolder = Nothing | |
Set objFSO = Nothing | |
End Function | |
' List class definition. | |
' Disclaimer: I am not the author of this class. | |
' Reference: https://stackoverflow.com/questions/13585660/lists-in-vbscript | |
Class List | |
Private mArray | |
Private Sub Class_Initialize() | |
mArray = Empty | |
End Sub | |
' Appends the specified element to the end of this list. | |
Public Sub Add(element) | |
If IsEmpty(mArray) Then | |
ReDim mArray(0) | |
mArray(0) = element | |
Else | |
If mArray(UBound(mArray)) <> Empty Then | |
ReDim Preserve mArray(UBound(mArray)+1) | |
End If | |
mArray(UBound(mArray)) = element | |
End If | |
End Sub | |
' Removes the element at the specified position in this list. | |
Public Sub Remove(index) | |
ReDim newArray(0) | |
For Each atom In mArray | |
If atom <> mArray(index) Then | |
If newArray(UBound(newArray)) <> Empty Then | |
ReDim Preserve newArray(UBound(newArray)+1) | |
End If | |
newArray(UBound(newArray)) = atom | |
End If | |
Next | |
mArray = newArray | |
End Sub | |
' Returns the number of elements in this list. | |
Public Function Size | |
Size = UBound(mArray)+1 | |
End Function | |
' Returns the element at the specified position in this list. | |
Public Function GetItem(index) | |
GetItem = mArray(index) | |
End Function | |
' Removes all of the elements from this list. | |
Public Sub Clear | |
mArray = Empty | |
End Sub | |
' Returns true If this list contains elements. | |
Public Function HasElements | |
HasElements = Not IsEmpty(mArray) | |
End Function | |
Public Function GetIterator | |
Set iterator = New ArrayIterator | |
iterator.SetArray = mArray | |
GetIterator = iterator | |
End Function | |
Public Function GetArray | |
GetArray = mArray | |
End Function | |
End Class | |
' ArrayIterator class definition. | |
' Disclaimer: I am not the author of this class. | |
' Reference: https://stackoverflow.com/questions/13585660/lists-in-vbscript | |
Class ArrayIterator | |
Private mArray | |
Private mCursor | |
Private Sub Class_Initialize() | |
mCursor = 0 | |
End Sub | |
Public Property Let SetArray(array) | |
mArray = array | |
End Property | |
Public Function HasNext | |
HasNext = (mCursor < UBound(mArray)+1) | |
End Function | |
Public Function GetNext | |
GetNext = mArray(mCursor) | |
mCursor = mCursor + 1 | |
End Function | |
End Class |
To facilitate the use of the script, one can create a batch file along the vbs file (using the same name) for quickly launching the script:
force_crlf.bat:
@echo off
cscript //nologo "%~dpn0.vbs"
pause
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The script searches for all known "text files" in the current directory. For each identified text files, the script replaces all EOL characters by CRLF (vbNewLine).
The text files are identified by their file extension. To add new file extension to verify/convert, modify the list of accepted file extension at beginning of the script.
During the execution, each file status (
file
orskipped
) is printed on the console. For this reason, it is recommended to launch the script from the command line.