a small console app that takes 3 params (last one is optional):
- A file (full path), UTF-8 BOM encoded that contains full paths of files / folders, #CRLF$ separated
- The "root level" of indentation
- Number of spaces to prefix deeper nested hierarchies
It creates this:
Code: Select all
Chromium_x64 [64.0.3282.140]
!Do not update chrlauncher if NOT necessary.txts
Chromium.lnk
@HowTo Configure
Add user scripts.txt
Install an extension.txt
Extensions
@IDs of installed extensions.txt
Cookie AutoDelete.txt
Code: Select all
R:\Chromium_x64 [64.0.3282.140]
R:\Chromium_x64 [64.0.3282.140]\!Do not update chrlauncher if NOT necessary.txts
R:\Chromium_x64 [64.0.3282.140]\Chromium.lnk
R:\Chromium_x64 [64.0.3282.140]\@HowTo Configure
R:\Chromium_x64 [64.0.3282.140]\@HowTo Configure\Add user scripts.txt
R:\Chromium_x64 [64.0.3282.140]\@HowTo Configure\Install an extension.txt
R:\Chromium_x64 [64.0.3282.140]\@HowTo Configure\Extensions
R:\Chromium_x64 [64.0.3282.140]\@HowTo Configure\Extensions\@IDs of installed extensions.txt
R:\Chromium_x64 [64.0.3282.140]\@HowTo Configure\Extensions\Cookie AutoDelete.txt
Code: Select all
indenter.exe "R:\test.txt" 2 4
Any hints on what could be improved?
A necessity: The final file should not have a trailing newline (because of this restriction I'm using two
WriteString(N) versions)...
Code: Select all
EnableExplicit
#PB_Compiler_IsMainFile = #True
; *************************************************************************************************
XIncludeFile "#includes\constants.pbi" : UseModule Consts
; *************************************************************************************************
; How to call this app
; Param 0 = file (UTF-8 BOM) in double quotes if it contains spaces
; Param 1 = root indentation level
; Param 2 = spaces per level (4 by default) - optional!
Procedure Main()
Protected.i rootIndentation, spacesPerLevel, hFile, encoding, i
Protected.s file, path, newFile, line
; Validate parameters
If CountProgramParameters() < 2
End #ExitCode_MissingParameter
EndIf
file = ProgramParameter(0)
If FileSize(file) = -1
End #ExitCode_FileDoesNotExist
ElseIf FileSize(file) = 0
End #ExitCode_FileIsEmpty
EndIf
rootIndentation = Val(ProgramParameter(1))
; Parameter is optional, default = 4
If CountProgramParameters() <> 3
spacesPerLevel = 4
Else
spacesPerLevel = Val(ProgramParameter(2))
EndIf
; Get path of the file (for the new one to write)
path = GetPathPart(file)
; Full path of new file
newFile = path + GetFilePart(file, #PB_FileSystem_NoExtension) + "_new." + GetExtensionPart(file)
NewList lines.s()
; Read file as UTF8 and place each line in a list
hFile = OpenFile(#PB_Any, file, #PB_File_SharedRead|#PB_UTF8)
If hFile
encoding = ReadStringFormat(hFile)
While Eof(hFile) = #False
AddElement(lines())
line = ReadString(hFile)
lines() = LSet(" ", (CountString(line, "\") - rootIndentation) * spacesPerLevel) + GetFilePart(line)
Wend
CloseFile(hFile)
EndIf
; Write all lines to a new file
i = 1
hFile = CreateFile(#PB_Any, newFile, #PB_File_SharedWrite|#PB_File_NoBuffering|#PB_UTF8)
If hFile
encoding = WriteStringFormat(hFile, #PB_UTF8)
If ListSize(lines()) > 0
ForEach lines()
If i <> ListSize(lines())
WriteStringN(hFile, lines(), #PB_UTF8)
Else
WriteString(hFile, lines(), #PB_UTF8)
EndIf
i + 1
Next
EndIf
CloseFile(hFile)
EndIf
FreeList(lines())
; Overwrite old file
If FileSize(newFile) > 0
DeleteFile(file, #PB_FileSystem_Force)
If RenameFile(newFile, file)
End #ExitCode_Ok
Else
End #ExitCode_FileMoveFailed
EndIf
Else
End #ExitCode_CreateNewFileFailed
EndIf
EndProcedure
; *************************************************************************************************
; Call the main procedure
Main()