Monday, May 27, 2019

VBScript rename file names in folder

VBScript code rename files;find and replace file names windows;find and replace file names VBScript;VBScript Find and Replace in File Names










1) Create a file rename_script.vbs and paste the below code. VBScript can be written using Windows Notepad or any other plain text editor.

 Set objFso = CreateObject("Scripting.FileSystemObject")  
 Set Folder = objFSO.GetFolder("C:\Users\MyName\Documents\MyDirectory\")  
 For Each File In Folder.Files  
   sNewFile = File.Name  
   sNewFile = Replace(sNewFile,"find","_")  
 if (sNewFile<>File.Name) then  
   File.Move(File.ParentFolder+"\"+sNewFile)  
 end if  


2) Save the file and double click to execute.

Note: "Swim at your own risk", the code doesn't have extensive error handling. Just be sure to replace the "C:\Users\MyName\Documents\MyDirectory" with the path to your directory.

Sunday, May 26, 2019

How to display code snippets in blogger posts

How to insert code blocks in Blogger posts;code snippets blogspot;code snippets blogger;How to display code snippets in blogger posts;vba code display in blogger

1) First, we need to add the html code(syntax template) below on the head section of our blogger post.

<head>
<link href="https://alexgorbatchev.com/pub/sh/current/styles/shCore.css" rel="stylesheet" type="text/css"></link>
<link href="https://alexgorbatchev.com/pub/sh/current/styles/shThemeEclipse.css" rel="stylesheet" type="text/css"></link>
<script src="https://alexgorbatchev.com/pub/sh/current/scripts/shCore.js" type="text/javascript">
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushJava.js' type='text/javascript'/>
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushXml.js' type='text/javascript'/>
<script language='javascript' type='text/javascript'>
SyntaxHighlighter.config.bloggerMode = true;
SyntaxHighlighter.all();
</script>
</head>

2) Next step is the code rendering. Basically, we need to HTML escape some characters like right angle brackets, e.g. all < must be replaced with <. To do so, you can use the following online tool: http://codeformatter.blogspot.in/2009/06/about-code-formatter.html

3) Once you have the code with escaped characters, you can copy it on your blogger post inside
<pre> tags.


3) That's it! the complete example is below:


 <head>  
 <link href="https://alexgorbatchev.com/pub/sh/current/styles/shCore.css" rel="stylesheet" type="text/css"></link>  
 <link href="https://alexgorbatchev.com/pub/sh/current/styles/shThemeEclipse.css" rel="stylesheet" type="text/css"></link>  
 <script src="https://alexgorbatchev.com/pub/sh/current/scripts/shCore.js" type="text/javascript">  
 <script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushJava.js' type='text/javascript'/>  
 <script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushXml.js' type='text/javascript'/>  
 <script language='javascript' type='text/javascript'>  
 SyntaxHighlighter.config.bloggerMode = true;  
 SyntaxHighlighter.all();  
 </script>  
 </head>  
4) Another handy online converter of code to html is hilite.me. Convert the piece of code which is desirable to insert as a code block using this converter and paste the html in the post.

Let’s say we have the following piece of code which is desirable to insert as a code block:

Function OdbcConnectionStringSQLServer(ByVal Driver As String, ByVal Server As String, ByVal database As String, _
ByVal Username As String, ByVal Password As String) As String

'SQL Server

OdbcConnectionStringSQLServer = "Driver={" & Driver & "};Server=" & Server _
& ";UID=" & Username & ";PWD=" & Password & ";Database=" & database


End Function

Below is the result after conversion. style I used: friendly, CSS I used: border:none;



Function OdbcConnectionStringSQLServer(ByVal Driver As String, ByVal Server As String, ByVal database As String, _
    ByVal Username As String, ByVal Password As String) As String

'SQL Server
  
     OdbcConnectionStringSQLServer = "Driver={" & Driver & "};Server=" & Server _
            & ";UID=" & Username & ";PWD=" & Password & ";Database=" & database
  

End Function



VBA Copy Files Using FileSystemObject

VBA FileSystemObject;Copy VBA FileSystemObject;Copy Files Using VBA FileSystemObject;copies one file from one folder to another folder with the VBA FileSystemObject






VBA code to copy file from one folder to another folder using FileSystemObject:



 Sub CopyFile(SourceFilePath As String, DestPath As String, OverWrite As Boolean)  
 ' (1) copies one file from one folder to another folder with the VBA FileSystemObject  
 ' (2) contains extensive error handling (safeguards)  
 ' (3) requires a reference to the object library "Microsoft Scripting Runtime" under Options &gt; Tools &gt; References... in the Visual Basic Editor.  
 Dim blFileExists As Boolean, blSourceErr As Boolean  
 Dim strFileName As String, strSuccessMsg As String, strNewDestPath As String, strNewSourcePath As String  
 Dim FSO As Scripting.FileSystemObject  
 Dim strErrMsg As String  
 Set FSO = New Scripting.FileSystemObject  
 'Set FSO = VBA.CreateObject("Scripting.FileSystemObject")  
 With FSO  
 strNewDestPath = .BuildPath(.GetAbsolutePathName(DestPath), "\")  
 strFileName = .GetFileName(SourceFilePath)  
 'check if the source file exists  
 If Not .FileExists(SourceFilePath) Then  
 ' check if the root drive was specified  
 If .DriveExists(Left(SourceFilePath, 2)) Then  
 blSourceErr = True  
 ' the provided source path is incomplete  
 ' build new path and ask the user if he accepts the suggestion  
 Else  
 strNewSourcePath = .BuildPath(.GetAbsolutePathName(SourceFilePath), "")  
 If Not MsgBox("The source path " &amp; Chr(34) &amp; SourceFilePath &amp; Chr(34) &amp; _  
 " is incomplete. Will you accept the following suggestion: " _  
 &amp; Chr(34) &amp; strNewSourcePath &amp; Chr(34) &amp; "?", vbYesNo, "Confirm new source path") = vbYes Then _  
 blSourceErr = True  
 End If  
 ' error  
 If blSourceErr Then _  
 strErrMsg = "The source file," &amp; Chr(34) &amp; strFileName &amp; Chr(34) &amp; _  
 " does not exist, or the specified path to the file, " &amp; Chr(34) &amp; _  
 Replace(SourceFilePath, strFileName, "") &amp; Chr(34) &amp; " is incorrect."  
 ' check if the destination folder already exists  
 ElseIf Not .FolderExists(strNewDestPath) Then  
 ' prompt the user if the destination folder should be created  
 If MsgBox("The destination folder, " &amp; Chr(34) &amp; strNewDestPath &amp; Chr(34) &amp; ", does not exist. Do you want to create it?", vbYesNo, _  
 "Create new folder?") = vbYes Then  
 .CreateFolder (strNewDestPath)  
 Else  
 strErrMsg = "The destination folder could not be created."  
 End If  
 ' check if the file already exists in the destination folder  
 Else  
 blFileExists = .FileExists(strNewDestPath &amp; strFileName)  
 If Not OverWrite Then  
 If blFileExists Then _  
 strErrMsg = "The file, " &amp; Chr(34) &amp; strFileName &amp; Chr(34) &amp; _  
 ", already exists in the destination folder, " &amp; Chr(34) &amp; _  
 strNewDestPath &amp; Chr(34) &amp; "."  
 End If  
 End If  
 ' attempt to copy file  
 If strErrMsg = vbNullString Then  
 On Error Resume Next  
 If strNewSourcePath = vbNullString Then strNewSourcePath = SourceFilePath  
 Call .CopyFile(strNewSourcePath, strNewDestPath, OverWrite)  
 If Err.Number &lt;&gt; 0 Then strErrMsg = "Run-time error " &amp; Err.Number &amp; Chr(10) &amp; Err.Description  
 On Error GoTo 0  
 End If  
 ' succesful copy  
 If strErrMsg = vbNullString Then  
 strSuccessMsg = "The file" &amp; Chr(34) &amp; strFileName &amp; Chr(34) &amp; " was copied to " &amp; _  
 Chr(34) &amp; strNewDestPath &amp; Chr(34) &amp; "."  
 If blFileExists Then strSuccessMsg = strSuccessMsg &amp; Chr(10) &amp; _  
 "(Note, the existing file in the destination folder was overwritten)."  
 MsgBox strSuccessMsg, vbInformation, "File copied"  
 ' error  
 Else  
 MsgBox strErrMsg, vbCritical, "Error!"  
 End If  
 End With  
 Set FSO = Nothing  
 End Sub