|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
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! |
|
#2
|
|||
|
|||
|
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
|
|
#3
|
|||
|
|||
|
I have read your code..Can u tell me what's the WbemScripting object and winmgmts object??And how to use them??
|
![]() |
| Viewing: Dev Shed Forums > Programming Languages - More > Visual Basic Programming > need help writing vbscript to cleanup desktop |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|
|