How do I search for a file extension located in the desktop and change their file types? - vbscript

User = CreateObject("WScript.Network").UserName ' gets username
Set objFSO = CreateObject("Scripting.FileSystemObject")
Recurse objFSO.GetFolder("C:\Users\" & User & "\Desktop\") ' searches for file extensions in the desktop
Sub Recurse(objFolder)
Dim objFile, objSubFolder
For Each objFile In objFolder.Files
If LCase(objFSO.GetExtensionName(objFile.Name)) = "mymom" Then ' if a file extension is mymom (just a test)
objFSO.MoveFile objFile.Name objFile.Name & ".ayy" ' changes the file extension to ayy (another test)
End If
Next
End Sub
When I do this, I get an error saying, "Expected end of statement." However, I do not know where to add the end statement. What I am trying to do is I am trying to let the script search the Desktop for all files with a specific file extension (in this case, I want to search for a file extension with .mymom) Then, I want to change the file extension with .ayy (This is the struggle part) I don't know if my code is wrong, or if it's just the end statement part.

You are getting the error, probably because you have missed a , between the source and destination file paths in the moveFile method
Use this code:
strFinalName = replace(objFile.name, "."&objFso.getExtensionname(objFile.name),".ayy")
objFSO.MoveFile objFile.Name,strFinalName

Related

how to prevent cached response from MSXML2.XMLHTTP, vbscript

I'm writing a wscript with vbscript in Windows 7.
The script needs to grab a file from an open ftp server, so I must use MSXML2.XMLHTTP object
The file I am grabbing is being cached in a sub folder of
C:\Users\user\AppData\Local\Microsoft\Windows\Temporary Internet Files\Content.IE5
The file that is cached has no "expires" value, and trying to set a value using setRequestHeader method doesn't seem to have any effect.
I will self-answer this in case anyone else needs this solution
this code snippet shows how to delete the cached files from the folder used by MSXML2.XMLHTTP, using the concept of the downloaded files will have the extension ".csv"
<job>
<script language="vbscript">
' use XMLHTTP to grab content from a URL
CMEURL = "ftp://ftp.cmegroup.com/pub/settle/nymex_future.csv"
' the downloaded files are being stored in a subfolder of this folder
filedownloadcachelocation = "C:\Users\user\AppData\Local\Microsoft\Windows\Temporary Internet Files\Content.IE5"
' delete the file before requesting a download
Dim fso
Set fso = CreateObject("Scripting.FileSystemObject")
Dim fileresults
Dim Directory
Set Directory = fso.GetFolder(filedownloadcachelocation)
Dim subfolders
Set subfolders = Directory.SubFolders
For Each Folder In subfolders
For Each FileName In Folder.Files
' only react if filename extension is csv
If InStr(FileName , ".csv") Then
fileresults = fileresults & FileName.Path & vbLf
' delete the file
If fso.FileExists(FileName.Path) Then
'WScript.Echo "deleting file " & FileName.Path
fso.DeleteFile FileName.Path
End If
End If ' csv ext
Next ' each file in folder
Next ' each folder in Content.IE5
WScript.Echo "files found that were deleted = " & VbLf & fileresults
' now continue with downloading the file using XMLHTTP

Can I list apps that don't have registry entries?

I've been working on a way to quickly and easily list all of the software installed on my machine. Once complete, I'd like to send it out to my group so that I can have everyone run it. Since the purpose of this exercise is generate a list of all of the applications that we absolutely require access to to our IT administrators, I don't want to miss anything important.
Up to this point, I've used code very similar to this - it looks in the registry at SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\ and Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\ and gives me all of the software that has been installed. However, a bunch of important programs are conspicuously absent (e.g. R, RStudio, SQL Developer), and I assume it's because they do not use Windows Installers.
This brings me to my question - is there a way I can list all of the programs that can be run on my machine (that have not impacted the registry)? Essentially, I think I want all of the non-system *.exe files, but that is probably oversimplifying things.
Anyone have any ideas? My code is VBS now, but I can muddle my way through most things.
If you want to find them all then you need to search every single file on your machine and check whether or not it has an executable extension. I'm reasonably confident that you are not going to want to do this.
I read your answer and laughed, since I was also "reasonably confident" that I did not want to go through all of the files on my (or anyone else's) machine. Once the laughing stopped, I realized that that's essentially what I had to do...
I've come up with something that works, and it now takes minutes to run (it took seconds to only check the registry), but it does work. I'm putting it here in case it can assist someone else, or maybe someone can find a way to make it more efficient. You need to supply some paths to folders where you want to look for exe files, and a file that you want to output to.
Thanks for reading.
On Error Resume Next
Folders = Array("C:\users\me","C:\SoftwareFolder1","C:\SoftwareFolder2","C:\SoftwareFolder3")
sFile="C:\myExeFiles.txt"
Set objFSO = CreateObject("Scripting.FileSystemObject")
Const ForReading = 1
Const ForWriting = 2
Const OverwriteIfExist = -1
Set fFile = objFSO.CreateTextFile(sFile, OverwriteIfExist, OpenAsASCII)
For Each x In Folders
Set objFolder = objFSO.GetFolder(x)
suckTheData objFSO, fFile, objFolder
Set objFolder = Nothing
Next
MsgBox("Done")
Set objFSO = Nothing
Sub suckTheData(objFSO, fFile, objFolder)
' *** STEP 1 *** 'Find files with a partiular extension in this folder
For Each objFile In objFolder.Files
If UCase(objFSO.GetExtensionName(objFile.Name))="EXE" Then
fFile.Write objFile & vbCrLf
If Err.Number <> 0 Then
fFile.Write "Error: " & objFile & " " & Err.Number & Err.Source & " " & Err.Description & vbCrLf
End If
End If
Next
Set objFile = Nothing
' *** STEP 2 *** 'Now that we've processed files, repeat for subdirectories
For Each subf In objFolder.SubFolders
'some folders can't/shouldn't be checked -
'16 is a normal folder, 32 is an archive, 1046 is symbolic, etc
If subf.Attributes ="16" Then
suckTheData objFSO, fFile, subf
End If
Next
Set subf = Nothing
End Sub

VBScript to move file with wildcard, if it exists

I am attempting to create a script that checks for the existence of archived eventlog files and, if any files exist, moves them to another folder. Running this script does nothing and gives no errors. I believe the wildcard in the If statement is what is giving me issues. I am new to vbscript, and scripting in general, and would appreciate some advice.
Set fso = CreateObject("Scripting.FileSystemObject")
If (fso.FileExists("d:\eventlogs\Archive*.evtx")) Then
FSO.CopyFile "d:\eventlogs\Archive*.evtx" , "d:\eventlogs\archive\"
FSO.Deletefile "d:\eventlogs\archive*.evtx"
End if
You can replicate a wild card search by using a combination of instr() and right(), or just multiple instr().
Set objFSO = CreateObject("Scripting.FileSystemObject")
objStartFolder = "d:\eventlogs\"
Set objFolder = objFSO.GetFolder(objStartFolder)
Set colFiles = objFolder.Files
For Each objFile in colFiles
if instr(objFile.Name,"Archive") <> 0 AND instr(objFile.Name,".evtx") <> 0 then
objFSO.MoveFile objFile.Name, "archive\" + objFile.Name
end if
Next
The appropriate approach of finding files with wildcards in VBScript:
Get the file collection from the containing folder
For each file in the filecollection:
Test the filename with a regular expression on a certain pattern
If the test passes, do some action with this file
Next file
Late answer, but might be useful because apparently nobody spotted the mistake.
From the VBScript documentation (script56.chm in my case), help page for CopyFile method says:
FileExists Method
Returns True if a specified file exists; False if it does
not.
object.FileExists(filespec)
Arguments
object
Required. Always the name of a FileSystemObject.
filespec
Required. The name of the file whose existence is to be determined. A complete path specification (either absolute or relative) must be provided if the file isn't expected to exist in the current folder.
Hence your expression fso.FileExists("d:\eventlogs\Archive*.evtx") returns False here; indeed there isn't any file named Archive*.evtx in your folder.
Either you remove your test, but you'll have to deal with the error the CopyFile method might generate, as doc says:
An error also occurs if a source using wildcard characters doesn't match any files.
As suggested by #automatedchaos in his answer https://stackoverflow.com/a/20907209/666414 you can also loop through files of the folder and decide what to do whenever the filename/extension matches your pattern.
Lastly, you can mix both solutions: loop through files of the folder, then set a flag to True and Exit Loop as soon as you encounter an expected file, then use the CopyFile method.
Like this:
With CreateObject("Scripting.FileSystemObject")
For Each objFile in .GetFolder("d:\eventlogs\").Files
If Left(objFile.Name, 7) = "Archive" And .GetExtensionName(objFile) = "evtx" Then
archiveFound = True
End If
Next
If archiveFound Then
.CopyFile "d:\eventlogs\Archive*.evtx", "d:\eventlogs\archive\"
.DeleteFile "d:\eventlogs\Archive*.evtx"
End If
End With
Note the wildcards work with the DeleteFile method too!

Renaming files in a loop - permission denied or the object invoked has disconnected from its clients errors

I am trying to include a renaming function to a loop which opens Word files and looks for tracked changes - if there are Tracked changes, I want to rename the file to get a _tracked_changes_ prefix.
However, I encounter a problem - I get a permission denied error, I am assuming because the file is open when I am trying to rename it. However, when I include the objFile.close function, the "object invoked has disconnected from its clients". I don't know how can I keep the reference of the objFile variable to the file I am processing and bypass the "permission denied" problem.
The code is:
Set fso = CreateObject("Scripting.FileSystemObject")
Set app = CreateObject("Word.Application")
app.Visible = false
app.DisplayAlerts = true
For Each objFile In fso.GetFolder(".").Files
If Lcase(fso.GetExtensionName(objFile)) = "docx" Then
<<<subprocedure that checks for tracked changes>>>
set objFile = app.Documents.Open(objFile.Path)
objFile.Name = "_tracked_changes_" & objfile.name
'Call check_tc
End If
Next
This naturally works fine if I don't have the set objFile = app.Documents.Open(objFile.Path) element, but I have to open the file to see if it has tracked changes.
First, change the set objFile = app.Documents.Open(objFile.Path) to something else besides objFile, as this is reusing the first objFile and that'd be why you're getting the disconnected error.
Then you should be able to close it.
So
set objWord = app.Documents.Open(objFile.Path)
objWord.close
Then
objFile.Name = "_tracked_changes_" & objfile.name

VB script - search for a file in directory

I am trying to write a VB script (having never attempted before) - I need it to search the folder'\file001\source$' - whilst in the folder search for all 'Update.exe'files - If this is done manually, in Windows it takes a long long time!
I would like all the files that it finds with this name - to be copied into a new folder.
Looking at various help forums I am getting more and more confused.
Below is what I have attempted:
Set fso = CreateObject("Scripting.FileSystemObject")
ShowSubfolders fso.GetFolder("\\file001\source$")
'foldername = "\file001\source$"
'filename = "Updater.exe"
Function ShowSubFolders(Folder)
For Each Subfolder in Folder.SubFolders
Wscript.Echo Subfolder.Path
ShowSubFolders Subfolder
Next
End Function
This is to search through a folder, recursively through the folders sub folders to find all files with this name.
I have also done research into -directory.getfiles. But have no clue if this is the right direction.
As a newbie to VB script, I have researched and attempted to play around with vb script, to get the function I desire. I would be grateful to any help I can get.
Again - my target is to - find all files within the given folder and subfolders with the name update.exe - and then to copy these files found into a new folder.
Thank you in advance.
If you only want to check the content of a single folder for the existence of a particular file you can do that like this:
Set fso = CreateObject("Scripting.FileSystemObject")
foldername = "\\file001\source$"
filename = "Update.exe"
If fso.FileExists(fso.BuildPath(foldername, filename)) Then
WScript.Echo filename & " exists."
End If
If you want to check the subfolders of foldername as well, you need to recurse into subfolders with something like this. You can either integrate the check from the above code sample in the loop over the subfolders, or add another loop over the files in the folder:
Set fso = CreateObject("Scripting.FileSystemObject")
CopyUpdater fso.GetFolder("\\file001\source$")
Sub CopyUpdater(fldr)
For Each f In fldr.Files
If LCase(f.Name) = "update.exe" Then
'copy file to somewhere else
End If
Next
For Each sf In fldr.SubFolders
CopyUpdater sf
Next
End Sub
See my question here, i benchmark three languages (vbscript also) which do a subdirectory traversal with full working samples and optimised for the language. benchmarks: does python have a faster way of walking a network folder?
Thats a good attempt . Read more on below link and understand things better .
Vbscript list all PDF files in folder and subfolders
VBScript to traverse through subdirectories
dim sFilename
Dim objDict
Set objDict=CreateObject("Scripting.Dictionary")
sFilename = ""
'root folder path where subfolder exists
fileLocation="C:\Users\u258251\Desktop\TestSubfolder"
Dim objFSO 'File System Object
Set objFSO = CreateObject("Scripting.FileSystemObject")
'Add all files with specific extention to dictonary
Call Recurse(fileLocation)
ItemArray = objDict.Items
'Loop through dictonary
For i = 0 To objDict.count -1
sFilename = sFilename & ItemArray(i) & VBCRLF
Next
msgbox(sFilename)
'find a specific file by name and return path
if objDict.Exists("DP103.txt") then
msgbox(objDict.Item("DP103.txt"))
end if
Sub Recurse(strFolderPath)
Dim objFolder
Set objFolder = objFSO.GetFolder(strFolderPath)
Dim objFile
Dim objSubFolder
For Each objFile In objFolder.Files
If (InStr(objFile.Name, ".") > 0) Then
'proceed if extention is .txt
If (LCase(Mid(objFile.Name, InStrRev(objFile.Name, "."))) = ".txt") Then
if objDict.Exists(objFile.Name)=false then
'add files and path to dictonary
objDict.Add objFile.Name,objfile.Path
End if
End if
End If
Next
For Each objSubFolder In objFolder.SubFolders
Call Recurse(objSubFolder.Path)
Next
End Sub

Resources