Visual Basic Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming Languages - MoreVisual Basic Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old December 13th, 2003, 06:08 PM
balzack balzack is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2003
Posts: 100 balzack User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 16 h 55 m 18 sec
Reputation Power: 6
need help writing vbscript to cleanup desktop

i'm trying to write a simple vb script that i can have run every night to cleanup my desktop, ie dump all the files on it into a folder called "desktop holder yesterday".

i'm having a problem because sometimes a file with the same name will exist in the "desktop holder yesterday" folder and i want to rename any that do exist to a different name, unique name. i was trying to use the date() to make the filename unique. if you know a better way that'd be great

the real problem is with the moveFile part, because they are string variables not strings and i cant get it to function properly.

can someone help me? just pay attention to the first IF part, cause once i fix that the ELSE will be the same fix.

here is my code:

Code:

Set objFSO = CreateObject("Scripting.FileSystemObject")
If objFSO.FolderExists("C:\Documents and Settings\wahooka\Desktop\Desktop Holder Yesterday") Then

	strComputer = "."
	Set objWMIService = GetObject("winmgmts:" _
	& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
	Set colFiles = objWMIService. _
	ExecQuery("Select * from CIM_DataFile where Path = '\\Documents and Settings\\wahooka\\Desktop\\'")

	For Each objFile in colFiles
		Wscript.Echo objFile.Name
		If objFSO.FileExists(objFile.Name) Then
			strFileName=objFile.Name
			;below i create a unique file name because the above one exists
			strFileNamenew=strFilename & Day(Date())
			strFileName = "C:\Documents and Settings\wahooka\Desktop\" & strFileName
			strFileNamenew="C:\Documents and Settings\wahooka\Desktop\Desktop Holder Yesterday\" & strFileNamenew
			,the problem is here, i dont know how to do this MoveFile with strings
			objFSO.MoveFile strFileName , strFileNamenew
		Else
			strFileName = "C:\Documents and Settings\wahooka\Desktop\" & strFileName
			strFileNamenew="C:\Documents and Settings\wahooka\Desktop\Desktop Holder Yesterday\" & strFileName
			objFSO.MoveFile strFileName , strFileNamenew
		End If

	Next

Else
	Set objFolder = objFSO.CreateFolder("C:\Documents and Settings\wahooka\Desktop\Desktop Holder Yesterday")

	strComputer = "."
	Set objWMIService = GetObject("winmgmts:" _
	& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
	Set colFiles = objWMIService. _
	ExecQuery("Select * from CIM_DataFile where Path = '\\Documents and Settings\\wahooka\\Desktop\\'")

	For Each objFile in colFiles
		Wscript.Echo objFile.Name

		If objFSO.FileExists(objFile.Name) Then
			strFileName=objFile.Name
			strFileNamenew=strFilename & Day(Date())
			strFileName = "C:\Documents and Settings\wahooka\Desktop\" & strFileName
			strFileNamenew="C:\Documents and Settings\wahooka\Desktop\Desktop Holder Yesterday\" & strFileNamenew
			objFSO.MoveFile strFileName , strFileNamenew
		Else
			strFileName = "C:\Documents and Settings\wahooka\Desktop\" & strFileName
			strFileNamenew="C:\Documents and Settings\wahooka\Desktop\Desktop Holder Yesterday\" & strFileName
			objFSO.MoveFile strFileName , strFileNamenew		
		End If
	Next

End If


thanks for any help!

Reply With Quote
  #2  
Old December 14th, 2003, 06:22 PM
balzack balzack is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2003
Posts: 100 balzack User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 16 h 55 m 18 sec
Reputation Power: 6
i figured it out after a long night.

in case anyone else might be interested, here is the code for a vbscript that i have scheduled to run nightly, and it cleans up all the crap i saved to my desktop and moves it into a folder on the desktop. much cleaner this way.

you'll have to change the folder names if you want to create/use a different one and also you'll have to adjust the folder with your user name, so it correctly points to your desktop folder

heres it is:

Code:

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set dayis = CreateObject("WbemScripting.SWbemDateTime")
Set monthis = CreateObject("WbemScripting.SWbemDateTime")
Set yearis = CreateObject("WbemScripting.SWbemDateTime")

dayis = Day(Date)
monthis = Month(Date)
yearis = Year(Date)

	rem check if the desktop holder yesterday folder exists, or create it
	If objFSO.FolderExists("C:\Documents and Settings\wahooka\Desktop\Desktop Holder Yesterday") Then
		rem do nothing
	Else
		rem create the folder
		Set objFolder = objFSO.CreateFolder("C:\Documents and Settings\wahooka\Desktop\Desktop Holder Yesterday")
	End If

	rem select all files in this location to loop thru
	strComputer = "."
	Set objWMIService = GetObject("winmgmts:" _
	& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
	Set colFiles = objWMIService. _
	ExecQuery("Select * from CIM_DataFile where Path = '\\Documents and Settings\\wahooka\\Desktop\\'")

	For Each objFile in colFiles

		rem this sets up the location to test and make sure there is not already a file in the yesterday desktop folder
		strtestlocation = objFile.Name
		objFSO.GetFile("" & strtestlocation & "")
		strFileNamenew="C:\Documents and Settings\wahooka\Desktop\Desktop Holder Yesterday\"
		strFileNamefilebase = objFSO.GetBaseName(strtestlocation)
		strFileNamefileext = objFSO.GetExtensionName(strtestlocation)
		strNewFileName = strFileNamenew & strFileNamefilebase & "." & strFileNamefileext

		rem if it finds a file with the exisiting name in the folder, then rename it with the date on the end of the filename
		If objFSO.FileExists(strNewFileName) Then
			strFileName=objFile.Name
			strFileNamenew="C:\Documents and Settings\wahooka\Desktop\Desktop Holder Yesterday\"

			objFSO.GetFile("" & strFileName & "")
			strFileNamefilebase = objFSO.GetBaseName(strFileName)
			strFileNamefileext = objFSO.GetExtensionName(strFileName)

			strNewFileName = strFileNamenew & strFileNamefilebase & " " & monthis & "-" & dayis & "-" & yearis & "." & strFileNamefileext
			objFSO.MoveFile "" & strFileName & "", "" & strNewFileName &""

		rem otherwise use the same file name
		Else
			strFileName=objFile.Name
			strFileNamenew="C:\Documents and Settings\wahooka\Desktop\Desktop Holder Yesterday\"
			
			objFSO.GetFile("" & strFileName & "")
			strFileNamefilebase = objFSO.GetBaseName(strFileName)
			strFileNamefileext = objFSO.GetExtensionName(strFileName)

			strNewFileName = strFileNamenew & strFileNamefilebase & "." & strFileNamefileext
			objFSO.GetFile("" & strFileName & "")
			
			objFSO.MoveFile "" & strFileName & "", "" & strNewFileName &""
		End If

	Next

Reply With Quote
  #3  
Old December 14th, 2003, 11:41 PM
cleverpig cleverpig is offline
Contributing User
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Jul 2003
Posts: 1,152 cleverpig User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 7
Send a message via MSN to cleverpig
I have read your code..Can u tell me what's the WbemScripting object and winmgmts object??And how to use them??

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming Languages - MoreVisual Basic Programming > need help writing vbscript to cleanup desktop


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

 Free IT White Papers!
 
How to Present Effectively Online
This white paper offers practical and actionable advice on the key steps that any presenter should consider as they plan and execute a Webinar or online meeting.

 
Open Source Security Myths
Open Source Software (OSS) is computer software whose source code is available to the general public with relaxed or non-existent intellectual property restrictions (or arrangement such as the public domain), and is usually developed with the input of many contributors.

 
Power and Cooling Capacity Management for Data Centers
This paper describes the principles for achieving power and cooling capacity management.

 
Scalable, Fault-Tolerant NAS for Oracle - The Next Generation
For several years NAS has been evolving as a storage alternative for Oracle databases, and for good reason: NAS is quite often the simplest, most cost-effective storage approach for Oracle. Learn about the benefits that HP's approach to scalable NAS brings to Oracle environments in this comprehensive white paper.

 
Understanding Web Application Security Challenges
This white paper discusses many common threats and preventive measures for Web application security, and explains what you can do to help protect your organization.

 

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway
Stay green...Green IT