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

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

Related

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

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

open and write 2 files

I need create one script for open one file (Server.txt → content all servers names) and create logfile with ServerName (example: Server1.txt). After that I need WriteLine in this logfile result of the script retrieve registry values.
I have one script working retrieve all registry values but I need to create each FileLog with ServerName.
I think cannot use two Opentextfile before close one.
Can we help me?
This is code I'm using for test:
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("Servers.txt")
Do Until objFile.AtEndOfStream
strComputer = objFile.ReadLine
Set objFile2 = objFSO.CreateTextFile(strComputer & ".txt",True)
set objHtml=objFSO.OpenTextFile(objFile2,8,true)
objHtml.WriteLine Now() & VbTab & RegResultQuery
objHtml.Close
Loop
You can call OpenTextFile twice, but you can't open the same file twice without closing it first. From what I see you don't need to open the file twice, though. CreateTextFile already returns a handle to the file, so you can simply use objFile2 throughout the loops.

File wont move to folder vbs

So im writing a script that drops a file folder then moves that file to the folder it dropped it self. Well the folder drops fine but the file wont move. Can some see whats wrong with my code? Or give me a better way to move the file. I also get no error message about trying to move the file.
Dim folder,fso,filsys,C
Set fso = CreateObject("Scripting.filesystemObject")
Set folder = fso.GetSpecialFolder(1)
Set wshshell = CreateObject("wscript.shell")
Set filesys = CreateObject("scripting.filesystemobject")
Set objfso = CreateObject("Scripting.filesystemObject")
Set c = fso.GetFile(Wscript.scriptFullname)
On Error Resume NEXT
Set objFolder = objFSO.CreateFolder("C:\55egr932ntn7mk23n124kv1053bmoss5")
If Err.Number<>0 Then
End If
WScript.Sleep 3000
C.Move ("C:\552ntn7mk23n124kv1053bmoss5\File.exe") (folder&"\File.exe")
And I have a program I use that turns the VBS into and EXE so you see the "file.exe" which really is the .VBS itself
I'm not familiar with this syntax, but the line below looks like it's expecting the folder variable to be a string.
C.Move ("C:\552ntn7mk23n124kv1053bmoss5\File.exe") (folder&"\File.exe")
Earlier in code it looks as though you're setting folder as an object.
Set folder = fso.GetSpecialFolder(1)
You might not get the error you mentioned in your comment if you convert folder to a string.
~~
Another thing to try is the following code:
Set fso = CreateObject("Scripting.filesystemObject")
Set folder = fso.GetSpecialFolder(1)
Alert (folder&"\File.exe")
(I'm not sure if it's "Alert" or "Msgbox" or something else.) That test will show you whether the file path makes sense. If you get an error on line 3 of that test, try converting folder to a string before your Alert (or Msgbox).

How to delete files from zip with VBScript

Im very new to VBScript (this is my first time ever using it). So far I've copied and altered my way into getting as far as I am.
I have an APK file that is too big for my needs. So what I've been doing is manually changing it to a zip and then deleting a couple images from it then renaming it back to an APK. Im trying to automate that with a VBScript. So far I have
Set oShell = CreateObject("WScript.Shell")
Set fso = CreateObject("Scripting.FileSystemObject")
sFolder = WORKSPACE & "\temp\" & app & "\build\" & PLATFORMSUBFOLDER & "\dist\" & app & "\bin"
oShell.CurrentDirectory = sFolder
'Make the Apk a Zip.
fso.MoveFile apkfile, zipApk
This is all working and I can see in Windows Explorer that the APK changes to a zip like I want it to. So Im wondering if there is any quick ways to just go in and delete a couple files without extracting the whole thing?
If not is there an easy way to extract the files and parse them at the same time?
Ive looked here Extract files from ZIP file with VBScript
but cant seem to get it working. I keep getting error "Object required: 'objShell.Names(...)'" Any hints to why this is happening?
Use the MoveHere method to move an item out of the zip file:
zipfile = "C:\path\to\your.zip"
fname = "some.file"
dst = "C:\some\folder"
Set app = CreateObject("Shell.Application")
For Each f In app.NameSpace(zipfile).Items
If f.Name = fname Then
app.Namespace(dst).MoveHere(f)
End If
Next
Then delete the files from the dst folder:
Set fso = CreateObject("Scripting.FileSystemObject")
fso.DeleteFile fso.BuildPath(dst, fname), True

VBS: Permission denied when deleting a file or a folder

This is my code, which first checks for a folder which contains the installer, if found, runs the uninstall and deletes the uninstall.exe if it still exists. Lastly, it deletes the folder itself.
Dim objFSO
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set wshshell = wscript.CreateObject("WScript.Shell")
If objFSO.FolderExists("C:\Installer_3_00_00") Then
Set objFolder = objFSO.GetFolder("C:\Installer_3_00_00")
if objFSO.FileExists("C:\Installer_3_00_00\uninstall.exe") Then
Wshshell.run "C:\Installer_3_00_00\uninstall.exe -q"
End if
if objFSO.FileExists("C:\Installer_3_00_00\uninstall.exe") Then
Set objFile=objFSO.GetFile("C:\Installer_3_00_00\uninstall.exe")
objFile.Delete True
End if
objFolder.Delete True
Else
End If
Set objFSO = Nothing
The problem is: It says Permission denied trying to delete a file or folder. I cross checked by deleting manually and it worked. I have searched for similar problems in this forum but none of which helped me to solve this particular issue.
Any suggestions will be appreciated.
Thanks
P.s I tried formatting my code here, but still I was not able to format it correctly.
Your problem is most likely caused by the (un)installer still running when you try to delete it, because this call:
Wshshell.run "C:\Installer_3_00_00\uninstall.exe -q"
returns immediately without waiting for the program to finish. Change that line into this:
Wshshell.Run "C:\Installer_3_00_00\uninstall.exe -q", 0, True

Resources