Vbscript Continue String on Next Line
VBScript: Flip Line Endings
28 Jun 2015
Recently I was working for a client who had particularly strict controls around access and tools that I could use. Whilst the bulk of the work I was carrying out on their Linux box could be managed using vi, I found it a little limiting when analysing some of the log files the work was producing. On the Windows machine I was using to access the files I only had Windows notepad and this really doesn't handle Linux file endings well. As a result I put together a quick VBScript to help with this.
Whilst there are often tools available on the Linux side to do line ending conversions I was unable to find any in this case and since I was unable to get a better text editor installed on the Windows PC it seemed my chances of putting new software on to the Linux server was even less likely. Hence my option to quickly put together a VBScript to do the job for me.
The issue is that on Linux file, the line feed character is used on its own to denote the end of a line whereas on Windows, the carriage return and line feed character combination is used to denote the end of a line. The script I created therefore just added on the extra carriage returns I needed.
As the work progressed there were a few occasions where I needed to do the reverse so I created a second script to support translating carriage return and line feeds into just line feeds. Since that work completed I decided to amalgamate and tidy the scripts into a single script an share it just in case anyone else might have need of it.
The basic premise remains a simple find and replace, but the script below is configured to accept multiple files (drag and drop, or send-to/command line parameters) and convert them based on the type of file. If it finds any carriage return and line feed combinations it will convert to just line feeds, otherwise it will add in the requisite carriage returns.
The script includes a constant named CONS_RUNSILENTLY
. Setting this to False will display message boxes when a conversion completes. Setting it to True (the default) will skip displaying those message boxes. There are also a couple of helper functions towards the bottom that set the silent running parameter of the main function to enable you to more easily use this functionality in your own scripts.
Option Explicit 'Set to True to run without any message box notifications Const CONS_RUNSILENTLY = True Main Sub Main () Dim astrScriptParameters Dim intCounter 'Get the command line parameters received by the script Set astrScriptParameters = Wscript . Arguments If astrScriptParameters . Count > 0 Then 'Each parameter should be a path for a plain text based file. '(Process each argument as though it is) For intCounter = 0 to astrScriptParameters . Count - 1 FlipLineEndings Wscript . Arguments ( intCounter ), NOT ( CONS_RUNSILENTLY ) Next Else 'If there's not even one argument/file to process then exit Msgbox "Please pass a file to this script" , 48 , "No File Provided" WScript . Quit End If End Sub 'Changes Windows line endings to UNIX and vice versa Function FlipLineEndings ( p_strFilePath , p_bDisplayMsgBox ) Dim strContent , strMessage Dim objFSO , objFile 'File access constants Const CON_FORREADING = 1 Const CON_FORWRITING = 2 'Title for message boxes Const CON_MSGBOXTITLE = "Line Conversion" 'Create our object for dealing with the file system Set objFSO = CreateObject ( "Scripting.FileSystemObject" ) 'Check path passed in exists If objFSO . FileExists ( p_strFilePath ) then 'Read in the file content Set objFile = objFSO . OpenTextFile ( p_strFilePath , CON_FORREADING ) strContent = objFile . ReadAll objFile . Close 'Does it contain CrLfs? If InStr ( 1 , strContent , vbCrLf , vbTextCompare ) > 0 Then 'Change Windows line endings to UNIX line endings strContent = Replace ( strContent , vbCrLf , vbLf ) 'Match any output message we might need to display strMessage = "Windows (CrLf) to UNIX (Lf)." Else 'Change UNIX line endings to Windows line endings strContent = Replace ( strContent , vbLf , vbCrLf ) 'Match any output message we might need to display strMessage = "UNIX (Lf) to Windows (CrLf)." End If 'Delete original file objFSO . DeleteFile p_strFilePath 'Write file Set objFile = objFSO . OpenTextFile ( p_strFilePath , CON_FORWRITING , True , False ) objFile . Write ( strContent ) 'Output success If p_bDisplayMsgBox then MsgBox strMessage & vbCrLf & p_strFilePath , vbOKOnly + vbInformation , CON_MSGBOXTITLE 'Return success FlipLineEndings = True Else 'Output failure If p_bDisplayMsgBox then MsgBox "File Not Found" & vbCrLf & p_strFilePath , vbOKOnly + VbCritical , CON_MSGBOXTITLE 'Return failure FlipLineEndings = False End If End Function 'Helper function if this is included elsewhere - run with notification Function FlipLineEndingsNotify ( p_strFilePath ) FlipLineEndingsNotify = FlipLineEndings ( p_strFilePath , True ) End Function 'Helper function if this is included elsewhere - run silently Function FlipLineEndingsSilent ( p_strFilePath ) FlipLineEndingsSilent = FlipLineEndings ( p_strFilePath , False ) End Function
This was a bit of an unusual case in that I didn't have access to my usual tools or have a way to easily access the files on my own work laptop. However in my experience if an unusual case pops up once there's a good chance it will pop up again for someone, somewhere, some time. If you don't have a better means available to you then perhaps this might be just the script your looking for.
A Self Installing VBScript
28 Feb 2014
Over the years I've been doing technical support I've had quite a few useful VBS scripts that I'd copy over to a user's machine. Then if it necessary, they could be run again at a later date (by remote control or referring the user to it's location and get them to run it). However I quickly tired of manually doing this and dealing with the instances where I'd accidentally copied the script to the user's desktop instead of the location I'd really wanted. As a result I created some extra lines to add into these scripts that would install the script to the desired location and then run it from there. This meant I could run it from a flash or network drive and it would automatically go to the right place - even updating older scripts.
Read More
Random Files Generator
26 Aug 2013
Whilst doing some house keeping on some old VBScript files the other day I came across one for creating test files. I've blogged previously about creating test files using my MTF utility and whilst it or fsutil
can be very useful, this script filled a particular niche. It was created as part of a test suite of scripts it creates multiple files of a specific size filled with random printable content.
Read More
VBScript to Convert Microsoft Office Files (Word, Excel, PowerPoint) to PDF
20 Dec 2012
I spend a lot of my time working with Word documents and Excel spreadsheets - it's in the nature of what I do. I frequently found that I need to create PDF copies of these files (to send out to clients) and eventually I grew tired of having to open the files one by one and select to save each as a PDF. As a result I created a bit of VBScript to automatically convert files for me (in Windows).
Read More
Converting ZIP files to 7Z archive file using VBScript and 7-Zip
29 Nov 2012
Whilst storage is continuing to get cheaper all the time I occasionally find myself needing to save some space. Usually where some cloud storage and archiving of old material is involved. ZIP files are very convenient for compressing files, but sometimes I need to go a step further. In these cases I opt for the 7z archive file format used in the free and portable software 7-Zip as I've always ended up with a smaller archive file.
Read More
Source: https://www.thoughtasylum.com/2015/06/28/VBScript-Flip-Line-Endings/
0 Response to "Vbscript Continue String on Next Line"
Post a Comment