Last active
December 29, 2015 17:58
-
-
Save rafaelfoster/7707366 to your computer and use it in GitHub Desktop.
VBS for files and folder manipulation functions
This file contains 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
Set objFSO = CreateObject("Scripting.FileSystemObject") | |
Const ForReading = 1 | |
Const ForWriting = 2 | |
Const ForAppend = 8 | |
Const OverwriteExisting = TRUE | |
'Case file exists, open in, other case create it. | |
If objFSO.FileExists(Log_File) Then | |
Set objCriaLog = objFSO.OpenTextFile("C:\arquivo", ForAppend, True) | |
Else | |
Set objCriaLog = objFSO.CreateTextFile("C:\arquivo") | |
End If | |
' Write in objCriaLog file | |
objCriaLog.WriteLine "" | |
'Copy File | |
objFSO.CopyFile [Source], [Destination] | |
'Delete file | |
objFSO.DeleteFile Log_File, True | |
'Delete Folder | |
objFSO.DeleteFolder Log_File, True |
This file contains 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
' Replace a Shortcut with regex search | |
On Error Resume Next | |
Set WshShell = CreateObject("WScript.Shell") | |
Set objRegEx = CreateObject("VBScript.RegExp") | |
Set objFSO = CreateObject("Scripting.FileSystemObject") | |
NumberFiles = 0 | |
Const ForReading = 1 | |
Const ForWriting = 2 | |
Const ForAppend = 8 | |
Const OverwriteExisting = TRUE | |
' ------------------------------------------------- | |
' Definição do Regex | |
objRegEx.Global = True | |
objRegEx.IgnoreCase = True | |
objRegEx.Pattern = "^(\\\\((host01)|(host02)|(<IPAddress>192.168.(\d+).(\d+)))\\path[s]?\\)" | |
strComputer = "." | |
strUserPath = wshShell.ExpandEnvironmentStrings( "%USERPROFILE%" ) | |
objStartFolder = strUserPath & "\Desktop\" | |
Set objFolder = objFSO.GetFolder(objStartFolder) | |
Set colFiles = objFolder.Files | |
Wscript.Echo "Iniciando validacao" | |
For Each objFile in colFiles | |
'Wscript.Echo "Extensao do arquivo " & objFile & " eh " & objFSO.GetExtensionName(objFile) | |
Select Case objFSO.GetExtensionName(objFile) | |
Case "lnk" | |
Set objShortcut = wshShell.CreateShortcut(objFile.Path) | |
' For URL shortcuts, only ".FullName" and ".TargetPath" are valid | |
'WScript.Echo "Full Name : " & objShortcut.FullName | |
'WScript.Echo "Arguments : " & objShortcut.Arguments | |
'WScript.Echo "Working Directory : " & objShortcut.WorkingDirectory | |
'WScript.Echo "Target Path : " & objShortcut.TargetPath | |
'WScript.Echo "Icon Location : " & objShortcut.IconLocation | |
'WScript.Echo "Hotkey : " & objShortcut.Hotkey | |
'WScript.Echo "Window Style : " & objShortcut.WindowStyle | |
'WScript.Echo "Description : " & objShortcut.Description | |
objShortcut.TargetPath = objRegEx.Replace( TargetPath , "\\servidor\caminho\" ) | |
objShortcut.WorkingDirectory = objRegEx.Replace( WorkingDirectory , "\\servidor\caminho\" ) | |
objShortcut.Save | |
objCriaLog.WriteLine "" | |
objCriaLog.WriteLine "Alterando o arquivo: " & objFile.Name | |
objCriaLog.WriteLine "Caminho antigo: " & TargetPath | |
objCriaLog.WriteLine "Caminho novo: " & objRegEx.Replace( TargetPath , "\\servidor\caminho\" ) | |
objCriaLog.WriteLine "" | |
Set objShortcut = Nothing | |
Case "url" | |
Set objArquivo = objFSO.OpenTextFile(objStartFolder & "\" & objFile.Name, ForReading, True) | |
strFileContent = objArquivo.ReadAll | |
if ( inStr(LCase(strFileContent), "[some_search_string]" ) <> 0 ) Then | |
NumberFiles = NumberFiles + 1 | |
If Not objFSO.FolderExists(Log_Folder & "\Shortcuts\" & strUserName) Then | |
objFSO.CreateFolder(Log_Folder & "\Shortcuts\" & strUserName) | |
End If | |
strFileContentEdited = objRegEx.Replace( strFileContent, "http://full.url.name" ) | |
Set objArquivoNovo = objFSO.OpenTextFile(objStartFolder & "\" & objFile.Name, ForWriting, True) | |
objArquivoNovo.WriteLine strFileContentEdited | |
objArquivoNovo.Close | |
objCriaLog.WriteLine "Alterando o arquivo: " & objFile.Name | |
End If | |
objArquivo.Close | |
End Select | |
Next |
This file contains 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
'FileSystemObject (objFSO) Methods: | |
'.BuildPath(strPath, strFileName) | |
'.CopyFile(Source, Dest [,Overwrite (True/False)] | |
'.CopyFolder(Source, Dest [,Overwrite (True/False)] | |
'.CreateFolder(Path) | |
'.CreateTextFile(FileName [,Overwrite (True/False) [, Unicode (True/False)]]) | |
'.DeleteFile(FileSpec, Force (True/False)) | |
'.DeleteFolder(FileSpec, Force (True/False)) | |
'.DriveExists(strDrive) (True/False) | |
'.FileExists(strFile) (True/False) | |
'.FolderExists(strFolder) (True/False) | |
'.GetAbsolutePathName(strPath) - Returns a string with the full drive, path, and file names: Drive:\Path\To\File.Ext | |
'.GetBaseName(strPath) - Returns a string with the file name, without the extension: File | |
'.GetDrive(strDrive) - Returns an object referring to a drive | |
'.GetDriveName(strDrive) - Returns a string referring to a drive. Drive: | |
'.GetExtensionName(strPath) - Returns a string referring to the extension of the file. Ext | |
'.GetFile(strPath) - Returns an object referring to a file. | |
'.GetFileName(strPath) - Returns a string referring to a file. File.Ext | |
'.GetFolder(strPath) - Returns an object referring to the path. | |
'.GetParentFolderName(strPath) - Returns a string referring to the path. \Path\To\ | |
'.GetSpecialFolderName(FolderType) FolderType=SystemFolder/TemporaryFolder/WindowsFolder | |
'.GetStandardStream(Type [,Unicode (True/False)]) | |
'.GetTempName() | |
'.MoveFile(Source, Dest) | |
'.MoveFolder(Source, Dest) | |
'.OpenTextFile(strFile [,IOMode (0=append, 1=Read, 2=Write) [,Create (True/False) [,Format (0=Ascii,-1=Unicode,-2=default)]]]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment