How to delete file of source after copying in vbscript? - vbscript

I am developing function that copy file from Temp Folder to Drive C.
After copying, I would like to delete file in Temp Folder.
I tried following codes but can't delete file.Please explain it to me.
sample codes:
Set objFSO = CreateObject("Scripting.FileSystemObject")
File = file of Temp Folder
objFSO.CopyFile File, "C:\"
objFSO.DeleteFile(File)
OR
Set objFSO = CreateObject("Scripting.FileSystemObject")
File = file of Temp Folder
objFSO.CopyFile File, "C:\"
Set delFileName = objFSO.GetFile(File)
delFileName.Delete delFileName

Copying a file from one location to C:\ and then deleting the version in the original location is the same as moving the file, so do that instead:
Dim objFSO : Set objFSO = CreateObject("Scripting.FileSystemObject")
objFSO.MoveFile File, "C:\"
Set objFSO = Nothing
If you really really want to do it in the way you've described then:
Dim objFSO : Set objFSO = CreateObject("Scripting.FileSystemObject")
On Error Resume Next
objFSO.CopyFile File, "C:\"
If Err.Number = 0 Then objFSO.DeleteFile File
On Error Goto 0
Set objFSO = Nothing
will do the trick.

Related

Change DateLastModified property of Folder to match file within

I have a number of folders, each with files inside the folder.
The structure looks something like this:
Folder.No.1
Folder_No_2
Folder No 3
and the files within are something like:
Folder.No.1\My.Movie.1.mp4
Folder.No.1\My.Movie.1.txt
Folder_No_2\My_Movie_2.mp4
Folder_No_2\My_Movie_2.jpg
Folder_No_2\My_Movie_2.txt
Folder No 3\My Movie 3.mp4
As you can see, some folders contain . in the name, some contain _ and some contain spaces.
The one consistent factor is that each folder will always contain an .mp4 file, regardless of anything else.
Therefore, how can I change the Date Modified date/time of the folder to match that of the .avi file contained within the folder? Can I do this by copying the DateLastModified from the file inside (the child) to the parent folder using VBScript?
So far I am working on something like this:
Dim objShell, objFolder, objFile
Set objFile = CreateObject("Scripting.FileSystemObject")
Set objShell = CreateObject("Shell.Application")
Set objFolder = objShell.NameSpace(strDir)
Set strDir = objFile.GetFolder("C:\Temp")
For Each objFile In objFolder
If UCase(objFolder.GetExtensionName(objFile.Name)) = "MP4" Then
objFolder.Items.Item(strDir).ModifyDate = DateLastModified
WScript.Echo objFolder.Name
End If
Next
but it fails when calling from command line with: cscript CopyDateToParent.vbs
Can anyone please help to correct this to make it work?
Try my code :
StrFolder="C:\Users\admin\Desktop\" 'Your folder
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objShell = CreateObject("Shell.Application")
Set objFolder = objFSO.GetFolder(StrFolder)
Set Folder = objShell.NameSpace(StrFolder)
Set colFiles = objFolder.Files
For Each objFile in colFiles
Name=objFile.Name
strExtension = LCase(objFSO.GetExtensionName(Name))
If strExtension = "mp4" Then
Set objFolderItem = Folder.ParseName(Name)
objFolderItem.ModifyDate = DateLastModified 'Example : "01/01/2008 8:00:00 AM"
End If
Next

VBS Tells me object doesn't support this property

I seriously don't know what's wrong, can anyone help:
Dim objFSO, objFolder, objFile, objNewFolder
' Create the file system object/Assing the system object to a variable
Set objFSO = CreateObject("Scripting.FileSystemObject")
' Get the folder we want to copy files from
Set objFSO = objFSO.GetFolder("C:\Test")
' Create a new folder called Test2
Set objNewFolder = objFSO.CreateFolder("C:\Test\Test2")
' For each file in the folder, copy the file to the destination
For Each objFile In objFolder.Files
objFile.Copy "C:\Test2"
Next
It tells me that:
vbs object doesn't support this property or method: 'CreateFolder'
The problem is that you are reassigning objFSO to become the Folder object returned here:
Set objFSO = objFSO.GetFolder("C:\Test")
After this line, objFSO is no longer a Scripting.FileSystemObject, its a Scripting.Folder object. You need to change your code to this:
Set objFolder = objFSO.GetFolder("C:\Test")

Unzip files using wildcards

I am basically trying to loop through my working folder to pick up any files that begin with 'Health' and unzip them in the same folder. The problem I have is that the code runs but I'm not getting any unzipped files nor am I getting any errors, can someone please help?
Sub Unzip()
Set fso = CreateObject("Scripting.FileSystemObject")
For Each f In fso.GetFolder("C:\Users\Jimbo\Documents\Process\_ThisWeek").Files
If LCase(fso.GetExtensionName(f)) = "zip" And Left(f.Name, 3) = "Health" Then
Unzip f.Path, "C:\Users\Jimbo\Documents\Process\_ThisWeek"
End If
'If the extraction location does not exist create it
Set fso = CreateObject("Scripting.FileSystemObject")
If NOT fso.FolderExists(f.path) Then
fso.CreateFolder(f.path)
End If
'Extract the contants of the zip file
set objShell = CreateObject("Shell.Application")
set FilesInZip = objShell.NameSpace(f.name).items
objShell.NameSpace(f.path).CopyHere(f.name)
Set fso = Nothing
Set objShell = Nothing
Next
End Sub

Create backup folder with several directories and files

I am new at this and I am trying to get a VBScript (Because the aplicattion only works with this) tha makes a backup of several directories and files...
EXample:
Check if Folder1 exist... (It may be on c:\ or another drive)
If donĀ“t exist the finish
If exist than should create a Bck Folder
THan Backup the folders that are inside folder1 (Folder2, Folder3)
Also backup all the files that are *.mds, *.vbs inside Folder4
The script have to maintain the struture...
And after that delete all folder1
This is what I have so far:
IF NOT EXIST "%INSTALLDIR%\Folder1\" GOTO ENDPROG
mkdir "%INSTALLDIR%\BCK\"
mkdir "%INSTALLDIR%\BCK\DADOS\"
mkdir "%INSTALLDIR%\BCK\IMAGEM\"
mkdir "%INSTALLDIR%\BCK\CONFIG\"
mkdir "%INSTALLDIR%\BCK\OFFBck"
copy "%INSTALLDIR%\Folder1\Dados*.MDB" "%INSTALLDIR%\BCK\dados\"
copy "%INSTALLDIR%\Folder1\Dados*.MDD" "%INSTALLDIR%\BCK\dados\"
copy "%INSTALLDIR%\Folder1\Dados*.VEI" "%INSTALLDIR%\BCK\dados\"
copy "%INSTALLDIR%\Folder1\Imagem*.*" "%INSTALLDIR%\BCK\Imagem\"
copy "%INSTALLDIR%\Folder1*.cfg" "%INSTALLDIR%\BCK\Config\"
copy "%INSTALLDIR%\Folder1\OFFbck*.ZIP" "%INSTALLDIR%\BCK\OFFbck\"
copy "%INSTALLDIR%\Folder1\Folder1\OFFbck*.ZIP" "%INSTALLDIR%\BCK\OFFbck\"
rmdir "%INSTALLDIR%\Folder1" /s /q
:ENDPROG
Can anyone give me a hand on this?
I already did this.....But i give an error if the folder already exist for the backup....but I cannot start to copy....
Set objFSO = CreateObject("Scripting.FileSystemObject")
If objFSO.FolderExists("C:\Folder1") Then
Set objFSO = CreateObject("Scripting.FileSystemObject")
Const OverwriteExisting = TRUE
Set objFolder = objFSO.CreateFolder("C:\BCK")
Set objFolder = objFSO.CreateFolder("C:\BCK\Imagem")
Set objFolder = objFSO.CreateFolder("C:\BCK\dados")
Set objFolder = objFSO.CreateFolder("C:\BCK\config")
Set objFolder = objFSO.CreateFolder("C:\BCK\off")
Else
End If
Thanks in advance
I just created this....
Set objFSO = CreateObject("Scripting.FileSystemObject")
If objFSO.FolderExists("C:\audatex") Then
Const OverwriteExisting = True
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.CreateFolder("C:\BCK")
Set objFolder = objFSO.CreateFolder("C:\BCK\Imagem")
Set objFolder = objFSO.CreateFolder("C:\BCK\dados")
Set objFolder = objFSO.CreateFolder("C:\BCK\WTB")
Set objFolder = objFSO.CreateFolder("C:\BCK\CFG")
Set objFolder = objFSO.CreateFolder("C:\BCK\config")
Set objFolder = objFSO.CreateFolder("C:\BCK\offdaten")
Set objFSO = CreateObject("Scripting.FileSystemObject")
objFSO.CopyFile "C:\Audatex\offdaten\*.zip" , "c:\BCK\Offdaten\" , OverwriteExisting
Set objFSO = CreateObject("Scripting.FileSystemObject")
objFSO.CopyFile "C:\Audatex\Dados\*.vei" , "c:\BCK\dados\" , OverwriteExisting
Set objFSO = CreateObject("Scripting.FileSystemObject")
objFSO.CopyFile "C:\Audatex\Dados\*.mdd" , "c:\BCK\dados\" , OverwriteExisting
Set objFSO = CreateObject("Scripting.FileSystemObject")
objFSO.CopyFile "C:\Audatex\*.cfg" , "c:\BCK\CFG\" , OverwriteExisting
Set objFSO = CreateObject("Scripting.FileSystemObject")
objFSO.CopyFile "C:\Audatex\Dados\*.mdb" , "c:\BCK\dados\" , OverwriteExisting
Set objFSO = CreateObject("Scripting.FileSystemObject")
objFSO.CopyFolder "C:\Audatex\Imagem" , "c:\BCK\Imagem" , OverwriteExisting
Set objFSO = CreateObject("Scripting.FileSystemObject")
objFSO.CopyFile "C:\Audatex\WTB\*.wtb" , "c:\BCK\WTB\" , OverwriteExisting
Set objFSO = CreateObject("Scripting.FileSystemObject")
objFSO.CopyFile "C:\Audatex\WTB\*.dtb" , "c:\BCK\WTB\" , OverwriteExisting
End IF
But know I have the following problems: If there is nothing on the folders i got an error and i cannot overwrite the folders
Thanks
You can copy files with particular extensions that start with a specific string like this:
For Each f In objFSO.GetFolder("C:\Folder1").Files
ext = LCase(objFSO.GetExtensionName(f))
If (ext = "mdb" Or ext = "mdd" Or ext = "vei") And LCase(Left(f.Name, 5)) = "dados" Then
f.Copy "C:\BCK\dados\"
End If
Next
Another (perhaps simpler) option would be to check the file name with a regular expression:
Set re = New RegExp
re.Pattern = "^dados.*\.(mdb|mdd|vei)$"
re.IgnoreCase = True
For Each f In objFSO.GetFolder("C:\Folder1").Files
If re.Test(f.Name) Then f.Copy "C:\BCK\dados\"
Next

VBScript current directory + sub directory?

I am trying to get the path of a file that is within a sub-directory of the current directory in VBScript. The following does not seem to work?
currentDirectory = left(WScript.ScriptFullName,(Len(WScript.ScriptFullName))-(len(WScript.ScriptName)))
FileToCopy = currentDirectory & "\test\user.js"
Here is the entire code:
Set oFSO = CreateObject("Scripting.FileSystemObject")
strFolder = oFSO.GetParentFolderName(WScript.ScriptFullName)
FileToCopy = oFSO.BuildPath(strFolder, "unproxy\user.js")
''# get AppdataPath
Set WshShell = CreateObject("WScript.Shell")
Set WshSysEnv = WshShell.Environment("PROCESS")
AppdataPath = WshSysEnv("APPDATA")
FoxProfilePath = AppdataPath & "\Mozilla\Firefox\Profiles\"
'"# is firefox and user.js present?
if oFSO.FolderExists(FoxProfilePath) AND oFSO.FileExists(FileToCopy) Then
''# copy user.js in all profilefolders to get around those random profile names =)
For Each ProfileFolder In oFSO.GetFolder(FoxProfilePath).Subfolders
oFSO.GetFile(FileToCopy).Copy ProfileFolder & "\" & FileToCopy, True
Next
End If
'"# clean up
Set oFSO = Nothing
Set WshShell = Nothing
Set WshSysEnv = Nothing
I recommend using FileSystemObject when dealing with file paths:
Set oFSO = CreateObject("Scripting.FileSystemObject")
strFolder = oFSO.GetParentFolderName(WScript.ScriptFullName)
FileToCopy = oFSO.BuildPath(strFolder, "test\user.js")
Edit: The problem is in this line of your script:
oFSO.GetFile(FileToCopy).Copy ProfileFolder & "\" & FileToCopy, True
Since FileToCopy contains a full file name, when you concatenate it with ProfileFolder you get an invalid file name, like this:
C:\Documents and Settings\username\Application Data\Mozilla\Firefox\Profiles\mlreq6kv.default\D:\unproxy\user.js
Change this line to the one below, and your script should work fine. (Note: the trailing path separator at the end of ProfileFolder is required to indicate that the profile folder, e.g. mlreq6kv.default, is indeed a folder and not a file.)
oFSO.GetFile(FileToCopy).Copy ProfileFolder & "\", True
You can get the current directory with :
Set WSHShell = WScript.CreateObject("WScript.Shell")
WScript.Echo WshShell.CurrentDirectory

Resources