Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Need help with my VBScript (working with files)

Status
Not open for further replies.
Mar 11, 2012
4
US
I have been working on this problem for about 2 days now, and I did find a way around it; however its really bugging me that I cant figure out how to script this the way that I want to.

Without getting into to much detail about why Im working on this. I use Altiris to capture and deploy images at work, and a recent update broke the way that unattend files are copied to the deployed images.

So I need a VBScript that removes unattend files from the reference computer. I have about 15 different unattend.xml files and they will need to be removed from both the C:\Windows\Panther & C:\Windows\System32\sysprep folders.

I tried looping through all the files in these folders looking for xml extensions. This caused some problems as there are other xml files in these directories that I dont want removed.

I finally ended up with the following code that uses 2 arrays to store the exact names of the unattend.xml's and the other stores the 2 paths that I want these files removed from.

Again while this works perfectly well, it really bugs me to not be able to do exactly what I want to. Please help if you can.

'START CODE THAT I AM NOT HAPPY WITH
Dim objFSO
Set objFSO = CreateObject("Scripting.FileSystemObject")

Dim x(13,0)
x(0,0)="ClassroomBootCampUnattendx64.xml"
x(1,0)="ClassroomRenderFarmUnattendx64.xml"
x(2,0)="ClassroomServerUnattendx32.xml"
x(3,0)="ClassroomServerUnattendx64.xml"
x(4,0)="ClassroomUnattendx64.xml"
x(5,0)="Unattend.xml"
x(6,0)="Untitled.xml"
x(7,0)="VMwareClassroomUnattendx64.xml"
x(8,0)="AdminUnattendx32.xml"
x(9,0)="AdminUnattendx64.xml"
x(10,0)="SecurityUnattendx32.xml"
x(11,0)="SecurityUnattendx64.xml"
x(12,0)="VMwareAdminUnattendx32.xml"
x(13,0)="VMwareAdminUnattendx64.xml"

Dim y(1,0)
y(0,0)="C:\Windows\Panther\"
y(1,0)="C:\Windows\System32\sysprep\"

for i=0 to 13
for j=0 to 0
for k=0 to 1
for l=0 to 0
var = (y(k,l)) & (x(i,j))
If objFSO.FileExists(var) then
objFSO.DeleteFile(var)
end if
next
next
next
next

'END CODE THAT I AM NOT HAPPY WITH





 
I notice that all the files you want to remove have either "unattend" or "untitled" in the file name. If the files you need to keep do not have these in the file names, you could simply test for these 2 words and delete the file if it contains one of them.

In the code you have shown, I see no compelling reason to use 2 dimensional arrays. Unless they are used elsewhere in your script you can simplify a bit by using 1 dimensional arrays.
Code:
Dim objFSO
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    
Dim x(13)
x(0)="ClassroomBootCampUnattendx64.xml"
x(1)="ClassroomRenderFarmUnattendx64.xml"
x(2)="ClassroomServerUnattendx32.xml"
x(3)="ClassroomServerUnattendx64.xml"
x(4)="ClassroomUnattendx64.xml"
x(5)="Unattend.xml"
x(6)="Untitled.xml"
x(7)="VMwareClassroomUnattendx64.xml"
x(8)="AdminUnattendx32.xml"
x(9)="AdminUnattendx64.xml"
x(10)="SecurityUnattendx32.xml"
x(11)="SecurityUnattendx64.xml"
x(12)="VMwareAdminUnattendx32.xml"
x(13)="VMwareAdminUnattendx64.xml"

Dim y(1)
y(0)="C:\Windows\Panther\"
y(1)="C:\Windows\System32\sysprep\"

for i=0 to 13
    for j=0 to 1
        var = y(j) & x(i)
        If objFSO.FileExists(var) then
          objFSO.DeleteFile(var)
        end if
    next
next
 
Hey jges,

Thanks for the reply. I didnt even think about it but your right the 1d array will work just fine.
Your other comment about testing for unattend in the file name is what I am struggling with.
I cant seem to figure out how to add a wildcard into the logic.

I have a batch file for something else that goes something like this.
If C:\*test*.vbs exists then del C:\*test*.vbs

This deletes all vbscript files from the root of C whos filenames contain "test"

I want to do something similar here look for "*unattend*.xml"

Ive tried the following code and it works aslong as you are explicit with the whole path (no wildcards)

strFile="C:\Windows\Panther\Unattendx64.xml"
SET objFSO = CreateObject("Scripting.FileSystemObject")

IF objFSO.FileExists(strFile) THEN
Wscript.Echo "The file was found."
ELSE
Wscript.Echo "Sorry, The file was NOT found."
END IF
 
Perhaps something like this...
Code:
Option Explicit

Dim strPath(1)
Dim FSO
Dim FLD
Dim myfile
Dim myFolder
Dim fileExt

    'Define file path.
	strPath(0) = "C:\Windows\Panther\"
	strPath(1) = "C:\Windows\System32\sysprep\"

    'Create the instance of the FSO.
    Set FSO = CreateObject("Scripting.FileSystemObject")

	For Each myFolder in strPath
		Set FLD = FSO.GetFolder(myFolder)
		'Loop through each file in the folder.
		For Each myfile in FLD.Files
			fileExt = FSO.GetExtensionName(myfile)
			if instr(1,myfile.name, "unattend",1) > 0 and LCase(fileExt) = "xml" then
				'report name of file found, change this to delete file as necessary
				msgbox(myfile.path)
			end if
		Next
	Next
 
Hey jges,

Thank you so much for all your help. Your last post was exactly what I was looking for. It worked flawlessly.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top