Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
I have a list of about 50 files throughout different folders and I back them up very regularly by adding that day's date to the files. Not all the files are back up at the same time. Here's an example:
C:\Intel\Logs\AcerDT.pdf.2020-05-05
C:\Intel\Logs\IntelChipset.log.2020-05-05
C:\Intel\Logs\IntelGFX.log
C:\Intel\Logs\IntelSSDTDK.log.2020-05-05
C:\Intel\Logs\IntelSSDToolbox.log
I wrote a VBScript to delete all the files with that date. It works great if all the files had the extension, ".2020-05-05". The problem is if the file in the list does not exist, as in, it does not have ".2020.-05-05", it throws me an error message and exit. I don't want this. I want the script to check the list, if the file exists, then delete it, if the file does NOT exist then check the next one. Is this possible to do?
Here's my code:
Dim objFSO : Set objFSO = CreateObject("Scripting.FileSystemObject")
arrList = Array(_
"C:\Intel\Logs\AcerDT.pdf", _
"C:\Intel\Logs\IntelChipset.log", _
"C:\Intel\Logs\IntelGFX.log", _
"C:\Intel\Logs\IntelSSDTDK.log", _
"C:\Intel\Logs\IntelSSDToolbox.log" _
)
Function CleanUp(arrList, value)
Dim sFile, myFile
For each sFile in arrList
myFile = sFile & "." & value
If objFSO.FileExists(sFile) Then
objFSO.DeleteFile(myFile)
Else
Wscript.Echo "NO File: " & sFile
End if
Next
End Function
CleanUp arrList, "2020-05-09"
Thanks
Thank you Kul-Tigin
When I replaced sFile with myFile as you had indicated, it resolved the issue. I can't believe I didn't see it.
Solution:
If objFSO.FileExists(myFile) Then
objFSO.DeleteFile(myFile)
Else
...
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 days ago.
Improve this question
I'm trying to make a program BUT whenever i run this .VBS file it says line 16 has an error, iv tried most things like putting it into a different directory, but it didn't work... rewording. renaming the file. AND just making it put the file into the designated place.
I'm expecting it to Copy the program to the C:\Windows\ directory, but its not realy copying it. iv also tried using "MoveFile" but it still didn't work.
THE CODE:
DIM FSO, MyFile
Set oShell = CreateObject( "WScript.Shell" )
''' runs file
CreateObject("Wscript.Shell").Run """SSS.bat""", 1, True
''' Alert Box
z=MsgBox("Finished.", 0+64, "Windows Alert!!!")
WScript.Sleep(1)
CreateObject("Wscript.Shell").Run """111.bat""", 1, True
CreateObject("Wscript.Shell").Run """file.bat""", 1, True
'''copy file to Windows
FSO.CopyFile "program.txt","C:\Windows\"
FSO.MoveFolder "C:\Windows\program.txt","C:\Windows\System32\program.exe"
CreateObject("Wscript.Shell").Run """C:\Windows\System32\program.exe""", 1, True
The error is likely happening because you are trying to use the FSO.CopyFile method before you've created the FSO object. To fix the error, you should add the following line of code before using FSO.CopyFile:
Set FSO = CreateObject("Scripting.FileSystemObject")
This question already has an answer here:
How to Verify if file exist with VB script
(1 answer)
Closed 1 year ago.
I'm struggling with correct syntax for setting the path in:
if (fso.FileExists("myfile.txt")) then ....
I use this:
set fso = WScript.CreateObject("Scripting.FileSystemObject")
Mypath = fso.GetAbsolutePathName(".")
if (fso.FileExists("myfile.txt")) then...
as i understand fso.GetAbsolutePathName(".") knows the path from where my script was launched
and when tested i can see the correct path with:
call MsgBox(Mypath)
for example the MsgBox is showing c:\users\user\desktop
that's where the script was launched and where i create my files.
I cannot use directly a path because it needs to be dynamic, files will be created and replaced
if exists at the same path the script was launched.
I hope it was clear enough, and that someone can answer this!
I'v read a lot of examples but none are covering this particular need.
thanks
Dim FSO 'As FileSystemObject
Set FSO = CreateObject("Scripting.FileSystemObject")
If (FSO.FileExists("C:\Foobar.txt") Then
{statements}
End If
Set FSO = Nothing
This question already has answers here:
How can I change extended file properties using vba
(3 answers)
Closed 6 years ago.
I'm looking for some solution how to change folder property - "Author".
I've found "Shell" solution how to get this info:
Dim sFile As Variant
Dim oShell: Set oShell = CreateObject("Shell.Application")
Dim oDir: Set oDir = oShell.Namespace("c:\foo")
For Each sFile In oDir.Items
Debug.Print oDir.GetDetailsOf(sFile, 9)
Next
but I need to change this value.
Have you got any experience with it?
You may use ChDir:
https://msdn.microsoft.com/en-us/library/office/gg264410.aspx
For more information see help from vba editor
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 2 years ago.
Improve this question
Need help with VB script to zip a file in a folder and give the zip file created the name of the original name of the file. Like if there is a trn file named abc.trn then want to create a zip file named abc.zip. Please help if anyone have any idea
Yes and no. According to this answer on SO it is not possible using VBscript alone.
There are VBA methods to zip and unzip using the windows built in
compression as well, which should give some insight as to how the
system operates. You may be able to build these methods into a
scripting language of your choice.
However, you can use an "implementation-dependent behavior of the Windows shell" (again, quoted from the same source)
Dim fso, winShell, MyTarget, MySource, file
Set fso = CreateObject("Scripting.FileSystemObject")
Set winShell = createObject("shell.application")
MyTarget = Wscript.Arguments.Item(0)
MySource = Wscript.Arguments.Item(1)
Wscript.Echo "Adding " & MySource & " to " & MyTarget
'create a new clean zip archive
Set file = fso.CreateTextFile(MyTarget, True)
file.write("PK" & chr(5) & chr(6) & string(18,chr(0)))
file.close
winShell.NameSpace(MyTarget).CopyHere winShell.NameSpace(MySource).Items
do until winShell.namespace(MyTarget).items.count = winShell.namespace(MySource).items.count
wscript.sleep 1000
loop
Set winShell = Nothing
Set fso = Nothing
I am very new to vb script and i need a script to delete couple of thrid level subfolders based on starting name _SA and 2 days old
example
C:\abc\user1\temp\ _SA123
c:\abc\user2\temp_SA2345
c:\abc\user3\temp_SA4567
I want to delete the folder starting with _SA older than 2 days and I have 50+ users folder. please hlep
Thanks,
Chilli
Based upon the example data, this should work though I'd consider this the 4th level:
Edit: Because you asked nicely, I updated this to build a log in a variable that will list the path of the folder deleted, the date it was created, and the date you deleted it. This information shows up in a msgbox, but you could easily modify to print the data to a file instead.
Dim rootFolder
Dim fld
Dim subFld
Dim subsubFld
Dim Log
Set fso = CreateObject("Scripting.FileSystemObject")
Set rootFolder = fso.GetFolder("C:\abc\")
For Each fld In rootFolder.SubFolders
For Each subFld In fld.SubFolders
For Each subsubFld In subFld.SubFolders
If Len(subsubFld.Name) >= 3 Then
If Left(subsubFld.Name, 3) = "_SA" And subsubFld.DateCreated < Now() - 2 Then
Log = subsubFld.Path & ", Created " & subsubFld.DateCreated & ",Deleted" & Now & vbNewLine
subsubFld.Delete
End If
End If
Next
Next
Next
MsgBox Log
'Or you could print the log to a file.
It has no error trapping, (other than making sure the folder name is atleast 3 characters long) and will get permission denied if you don't have permission.
Note: the code I posted has indentations, it's just not display for some reason. If you want to see the indented code, hit the edit button.