I want to create a vbscript that uses robocopy, which is fine, but I was hoping you can provide me the most elegant way to do this...
Copy all contents (Files and folders) of User Directory to this location EXCEPT copy AppData directory (Files and folders) to a different location AND copy Desktop directory to a different location
If FSO.folderExists(SOURCE & strAccount & "\AppData") Then
oShell.Run "robocopy " & appDataSource & " " & appDatastrDestination & appDatastrSwitches
Else
oShell.Run "robocopy " & strSource & " " & strDestination & strSwitches
End If
If you want to use all VBScript, you should be able to work with this. Edit - Added subs to reduce code.
On Error Resume Next
strSourceProfile = "C:\Users\NewUser"
strBaseFolder1 = "C:\Temp\"
strBaseFolder2 = "C:\Temp\Backup\"
strDestFolder1 = "C:\Temp\Backup\Profile\"
strDestFolder2 = "C:\Temp\Backup\Desk\"
strDestFolder3 = "C:\Temp\Backup\App\"
Set objFSO = CreateObject("Scripting.FileSystemObject")
Const OverWriteFiles = True
' make sure target folders exist
ChkFolder strBaseFolder1
ChkFolder strBaseFolder2
ChkFolder strDestFolder1
ChkFolder strDestFolder2
ChkFolder strDestFolder3
For Each objFolder In objFSO.GetFolder(strSourceProfile).SubFolders
If objFolder.Name <> "Desktop" And objFolder.Name <> "AppData" Then
CopyToTarg objFolder.Path, strDestFolder1
End If
Next
For Each objFolder In objFSO.GetFolder(strSourceProfile).SubFolders
If objFolder.Name = "Desktop" Then
CopyToTarg objFolder.Path, strDestFolder2
End If
Next
For Each objFolder In objFSO.GetFolder(strSourceProfile).SubFolders
If objFolder.Name = "AppData" Then
CopyToTarg objFolder.Path, strDestFolder3
End If
Next
Sub ChkFolder(strFolder)
If Not(objFSO.FolderExists(strFolder)) Then
objFSO.CreateFolder(strFolder)
End If
End Sub
Sub CopyToTarg(strSource , strTarget)
objFSO.CopyFolder strSource , strTarget , OverWriteFiles
End Sub
Related
I'm fairly new to vbscript and programming! As I've already mentioned in the title, I've written (or at least I've tried) a vbscript which should copy and rename all files in C:\test\ and the subfolders of C:\test\ to a another Folder, named C:\test1.
Here's what I've got so far:
Dim objStartFolder, objtargetFolder, objDateCreatedName
Set objFSO = CreateObject("Scripting.FileSystemObject")
objStartFolder = "C:\test"
objtargetFolder = "C:\test1"
Set objFolder = objFSO.GetFolder(objStartFolder)
WScript.Echo objFolder.Path
Set colFiles = objFolder.Files
For Each objFile In colFiles
WScript.Echo objFile.Name
Next
WScript.Echo
ShowSubFolders objFSO.GetFolder(objStartFolder)
Sub ShowSubFolders(Folder)
For Each Subfolder In Folder.SubFolders
WScript.Echo Subfolder.Path
Set objFolder = objFSO.GetFolder(Subfolder.Path)
Set colFiles = objFolder.Files
For Each objFile In colFiles
Set objDateCreatedName = objFSO.GetFile(objStartFolder)
WScript.Echo objDateCreatedName.DateCreated
WScript.Echo "I'm going to copy " & objFolder.Path & objFile.Name & " to " & objtargetFolder & objtargetFolder & objFile.Name & "."
Next
WScript.Echo
ShowSubFolders Subfolder
Next
End Sub
It would be really nice if you could help me and if you need more Information I'll make sure to deliver them.
If every file should go into the same destination folder you could simply do something like this:
Set fso = CreateObject("Scripting.FileSystemObject")
Function Pad(s)
Pad = Right("00" & s, 2)
End Function
Sub CopyFiles(fldr, dst)
'Copy all files from fldr to destination folder and append the date (in ISO
'format) to the name. Overwrite existing files.
For Each f In fldr.Files
created = Year(f.DateCreated) & "-" & Pad(Month(f.DateCreated)) & "-" & _
Pad(Day(f.DateCreated))
newname = fso.GetBaseName(f) & "_" & created & "." & fso.GetExtensionName(f)
f.Copy fso.BuildPath(dst, newname), True
Next
'Recurse into subfolders.
For Each sf In fldr.SubFolders
CopyFiles sf, dst
Next
End Sub
CopyFiles fso.GetFolder("C:\test"), "C:\test1"
I want to move the file from the directory (2) in the subdirectory (1) (after deleting all files in subfolders (1)) to a subdirectory (1) and delete the folder (2)
For example:
I have a folder in disk:
D:/ABC/123/"big" + (with a.jpg;b.jpg;c.jpg..ect)/1.jpg,2.jpg;3.jpg ..ect..
D:/ABC/456/"big" + (with d.jpg;e.jpg;f.jpg..ect)/4.jpg;5.jpg;6.jpg ..ect..
D:/ABC/789/"big" + (with g.jpg;h.jpg;k.jpg..ect)/5.jpg;6.jpg;7.jpg ..ect..
I want delete (a.jpg;b.jpg;c.jpg..ect) in "123" then move (1.jpg,2.jpg;3.jpg ..ect..) in subfolders "big" to "123" then delete "big"
loop all for subfolders in "ABC"
I try :
Dim fso, shl, curdir, folder, file, newfoldername, newfolderpath,subb
Set fso = CreateObject("Scripting.FileSystemObject")
Set shl = CreateObject("WScript.Shell")
curdir = shl.CurrentDirectory
newfoldername = "big"
Sub curdir(Folder)
For Each folder In fso.GetFolder(curdir).SubFolders
For Subfolder in Folder.SubFolders
set subb = fso.GetFolder(curdir).Subfolders
For Each file In folder.Files
MoveFile file.Path, Folder.SubFolders
Next
Next
Next
End Sub
Sub MoveFile(source, destination)
On Error Resume Next
fso.CopyFile source & "\", destination, True ' true = overwrite
If Err Then
WScript.Echo "Error copying " & source & " to " & destination & ": " & Err.Description
WScript.Quit
Else
fso.DeleteFile source, True
WScript.Echo "Delete"
End If
On Error GoTo 0
End Sub
I'm a beginner, help me :)
Set objFSO = CreateObject("Scripting.FileSystemObject")
For Each objFolder In objFSO.GetFolder("D:\ABC").SubFolders
' Delete all of the files in this folder...
For Each objFile In objFolder.Files
objFile.Delete
Next
' Now move all of the files from the "big" subfolder to this folder...
For Each objFile In objFSO.GetFolder(objFolder.Path & "\big").Files
objFSO.MoveFile objFile.Path, objFolder.Path & "\" & objFile.Name
Next
' Finally, delete the "big" subfolder...
objFSO.DeleteFolder objFolder.Path & "\big"
Next
Can anybody give me any pointers on the below script I'm trying to create please, basically I'm trying search for certain folders under a parent but the general child structure is roughly the same, if exists then copy certain extensions, but not all extensions exist in all the child folders
When I run the Script I'm not getting any files copied into the "harvest" folder or any child Folders created under the Harvest folder, both Folders and files all exist when Testing this script
Any Help Much appreciated
advancedPath = InputBox("Type the Application Sharepoint")
advancedDBPath = InputBox("Type the Database Folder")
harvestFolder = InputBox("Type the Harvest Folder")
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set wshShell = WScript.CreateObject( "WScript.Shell" )
' Check if Harvest folder exist if not create
If Not objFSO.FolderExists(harvestFolder) Then
objFSO.CreateFolder(harvestFolder)
End If
'Extensions to copy
extStr = Array("*.ini", "*.pf","*.bat","*.admin","*.st","*.ver","*.propath")
If objFSO.FolderExists(advancedPath & "oalive80") Then
' Check if Oalive80 exist
If objFSO.FolderExists(advancedPath & "oalive80") = TRUE Then
objFSO.CreateFolder(harvestFolder & "oalive80")
objFSO.CopyFile advancedPath & "oalive80" & "\" & extStr, harvestFolder & "oalive80", false
objFSO.CopyFile advancedPath & "oalive80\oahfb10" & "\" & extStr, harvestFolder & "oalive80", false
End If
' Check if Oatest80 exist
If objFSO.FolderExists(advancedPath & "oatest80") = TRUE Then
objFSO.CreateFolder(harvestFolder & "oatest80")
objFSO.CopyFile advancedPath & "oatest80" & "\" & extStr, harvestFolder & "oatest80", false
End If
' Check if oplive exist
If objFSO.FolderExists(advancedPath & "oplive") = TRUE Then
objFSO.CreateFolder(harvestFolder " "oplive")
objFSO.CopyFile advancedPath & "oplive\gclib\pf" & "\" & extStr, harvestFolder & "oplive" false
End If
' Check if Live DB folder exist
If objFSO.FolderExists(advancedDBPath & "oalive80") = TRUE Then
objFSO.CreateFolder(harvestFolder & "LiveDB")
objFSO.CopyFile advancedDBPath & "oalive80\oa_data" & "\" & extStr, harvestFolder & "LiveDB", false
End If
End If
You should probably use a loop for the array, as shown below, for each of your copy code
For iCt = 0 To UBound(extStr)
strExtn = extStr(iCt)
objFSO.CopyFile advancedPath & "oalive80" & "\" & strExtn , harvestFolder & "oalive80", false
Next
Maybe use a function like shown below for your code:
Private Sub BulkCopyFile(ByVal objFSO As Scripting.FileSystemObject, ByVal strSourceFolder As String, ByVal strDestinationFolder As String)
extStr = Array("*.ini", "*.pf","*.bat","*.admin","*.st","*.ver","*.propath")
For iCt = 0 To UBound(extStr)
strExtn = extStr(iCt)
objFSO.CopyFile strSourceFolder & strExtn, strDestinationFolder, False
Next
End Sub
Example usage for your code
' Check if Oalive80 exist
If objFSO.FolderExists(advancedPath & "oalive80") = True Then
objFSO.CreateFolder (harvestFolder & "oalive80")
Call BulkCopyFile(objFSO, advancedPath & "oalive80" & "\", harvestFolder & "oalive80")
Call BulkCopyFile(objFSO, advancedPath & "oalive80\oahfb10" & "\", harvestFolder & "oalive80")
End If
I am trying to ZIP up a folder in VBScript and it doesn't seem to work. I'm certain I am creating the header file correctly.
It creates the actual file correctly, just doesn't zip the folder.
Anyone got any ideas:
Sub ArchiveFolder (folder)
Dim fso, wShell, sApp, zipFile
Set fso = CreateObject("Scripting.FileSystemObject")
Set wShell = CreateObject("WScript.Shell")
Set sApp = CreateObject("Shell.Application")
Set zipFile = fso.CreateTextFile(folder & ".zip")
' Write zip file header.
zipFile.Write "PK" & Chr(5) & Chr(6) & String(18, 0)
zipFile.Close
sApp.NameSpace(folder & ".zip").CopyHere folder
End Sub
The answer I found here. The magic is in the last Do..Loop where the script wait the Shell to do it job.
ArchiveFolder "sub\foo.zip", "..\baz"
Sub ArchiveFolder (zipFile, sFolder)
With CreateObject("Scripting.FileSystemObject")
zipFile = .GetAbsolutePathName(zipFile)
sFolder = .GetAbsolutePathName(sFolder)
With .CreateTextFile(zipFile, True)
.Write Chr(80) & Chr(75) & Chr(5) & Chr(6) & String(18, chr(0))
End With
End With
With CreateObject("Shell.Application")
.NameSpace(zipFile).CopyHere .NameSpace(sFolder).Items
Do Until .NameSpace(zipFile).Items.Count = _
.NameSpace(sFolder).Items.Count
WScript.Sleep 1000
Loop
End With
End Sub
Check your argument. folder must be the path to the object you want to put into the zip file. If it's a folder object you have to use folder.Path, because the default method of folder objects is Name, and CopyHere can't find the object with just the name.
You could add some debugging statements to your function to check that:
WScript.Echo TypeName(folder)
If fso.FolderExists(folder) Then
WScript.Echo folder & " exists."
Else
WScript.Echo folder & " doesn't exist."
End If
you could call an external zip file via %comspec%
oShell.Run "%comspec% /c c:\windows\7za.exe a " & oFile &".zip " & oFile & " -tzip",,True
Source http://www.scriptlook.com/zip-large-files-in-a-directory-2/
Is it possible to rename a file in a folder to its folder name using vbs? I have the following script which I am just using MsgBox at this time for debugging before I implement the renaming. for some reason tho ObjFolder doesnt change.
Option Explicit
Dim strFolderToSearch, objFSO, objRootFolder, objFolder, colSubfolders, strOutput, objStartFolder, colFiles, objFile
strFolderToSearch = "D:\Shared\Films"
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objRootFolder = objFSO.GetFolder(strFolderToSearch)
Set colSubfolders = objRootFolder.SubFolders
For Each objFolder in colSubfolders
objStartFolder = objFolder
Set objFolder = objFSO.GetFolder(objStartFolder)
Set colFiles = objFolder.Files
For Each objFile in colSubfolders
MsgBox objFile.name & "," & objFolder.name
Next
Next
I admit that I can't follow the tangle of your folders, subfolders, and files. But if you want to rename files in a folder, use this stratege:
Dim sDName : sDName = "FancyRename"
Dim sDName2 : sDName2 = "," & sDName
Dim oFile, sNewName
For Each oFile In goFS.GetFolder(goFS.BuildPath("..\testdata", sDName)).Files
If 0 = Instr(oFile.Name, sDName2) Then
sNewName = Replace(oFile.Name, ".", sDName2 & ".")
Else
sNewName = Replace(oFile.Name, sDName2, "")
End If
WScript.Echo oFile.Name, "=>", sNewName
oFile.Name = sNewName
Next
output of running this three times:
that.txt => that,FancyRename.txt
this.txt => this,FancyRename.txt
that,FancyRename.txt => that.txt
this,FancyRename.txt => this.txt
that.txt => that,FancyRename.txt
this.txt => this,FancyRename.txt
UPDATE
How about: Given a folder D and a file name F (e.g. someavi.avi), rename all (existing) Fs in D and its sub folders to "subfoldername.avi", unless such a file already exists:
recursiveRename goFS.GetFolder("..\testdata\FancyRename"), "someavi", "avi"
Sub recursiveRename(oDir, sFiNa, sExt)
WScript.Echo "Looking into", oDir.Path
Dim sOFiNa : sOFiNa = sFiNa & "." & sExt
Dim sOFSpec : sOFSpec = goFS.BuildPath(oDir.Path, sOFiNa)
Dim sNFSpec
If goFS.FileExists(sOFSpec) Then
WScript.Echo "found ", sOFSpec
sNFSpec = goFS.BuildPath(oDir.Path, oDir.Name & "." & sExt)
If goFS.FileExists(sNFSpec) Then
WScript.Echo "found ", sNFSpec, "- can't rename"
Else
WScript.Echo "found no", sNFSpec, "- will rename"
goFS.MoveFile sOFSpec, sNFSpec
End If
Else
WScript.Echo "found no", sOFSpec
End If
Dim oSubF
For Each oSubF In oDir.SubFolders
recursiveRename oSubF, sFiNa, sExt
Next
End Sub
sample output:
Looking into M:\lib\kurs0705\testdata\FancyRename
found no M:\lib\kurs0705\testdata\FancyRename\someavi.avi
Looking into M:\lib\kurs0705\testdata\FancyRename\subfa
found no M:\lib\kurs0705\testdata\FancyRename\subfa\someavi.avi
Looking into M:\lib\kurs0705\testdata\FancyRename\subfc
found M:\lib\kurs0705\testdata\FancyRename\subfc\someavi.avi
found no M:\lib\kurs0705\testdata\FancyRename\subfc\subfc.avi - will rename
Looking into M:\lib\kurs0705\testdata\FancyRename\subfb
found M:\lib\kurs0705\testdata\FancyRename\subfb\someavi.avi
found M:\lib\kurs0705\testdata\FancyRename\subfb\subfb.avi - can't rename
UPDATE II
Changed specs: rename .avi to folder name, if there is exactly one .avi
recursiveRename03 goFS.GetFolder("..\testdata\FancyRename")
Sub recursiveRename03(oDir)
WScript.Echo "Looking into", oDir.Path
Dim sNFSpec : sNFSpec = goFS.BuildPath(oDir.Path, oDir.Name & ".avi")
If goFS.FileExists(sNFSpec) Then
WScript.Echo "found ", sNFSpec, "- can't rename"
Else
Dim oOFile : Set oOFile = Nothing
Dim oFile
For Each oFile In oDir.Files
If "avi" = goFS.GetExtensionName(oFile.Name) Then
If oOFile Is Nothing Then
Set oOFile = oFile
Else
WScript.Echo "Found second avi", oFile.Name
Set oOFile = Nothing
Exit For
End If
End If
Next
If oOFile Is Nothing Then
WScript.Echo "not exactly one avi found"
Else
WScript.Echo "found ", oOFile.Name, "- will rename"
oOFile.Name = oDir.Name & ".avi"
End If
End If
Dim oSubF
For Each oSubF In oDir.SubFolders
recursiveRename03 oSubF
Next
End Sub
UPDATE III
If you use a global FSO or pass an FSO to the Subs/Functions needing
it, you avoid its repetitive re-creation.
If you pass a folder/file object instead of a string to the
Subs/Functions dealing with such objects, you can access their
properties/methods immediately/for free (no need to reclaim/get back
info by string operations).
If you rename a file, you must check whether there is a file having
the new name (it's not sufficient to check whether the file you work
with doesn't have the new name).
Idealistically, your script should have the following features:
Recursion - For traversing folders that are 1-n deep from D:\Shared\Films
Rename file function - For renaming match files according to your rule.
I wrote the following script that features the following routines:
RenameAllVideos(strFolder) - this will recursively search subfolders
RenameVideo(strFileName) - will rename a match video file using your rule
Here's my script:
Option Explicit
Call RenameAllVideos("D:\Shared\Films")
Sub RenameAllVideos(strFolder)
Dim fso, file, folder
Set fso = CreateObject("Scripting.FileSystemObject")
' Check for AVIs to rename.
For Each file in fso.GetFolder(strFolder).Files
If Right(file.Name, 4) = ".avi" Then
Call RenameVideo(strFolder & "\" & file.Name)
End If
Next
' Check for SubFolders to recurse into.
For Each folder in fso.GetFolder(strFolder).SubFolders
Call RenameAllVideos(strFolder & "\" & folder.Name)
Next
End Sub
Sub RenameVideo(strFileName)
Dim fso, strExt, strFolder, strNewFileName
Set fso = CreateObject("Scripting.FileSystemobject")
' Note the extension (should be avi)
strExt = fso.GetExtensionName(strFileName)
' Derive the full path to the folder.
strFolder = fso.GetParentFolderName(strFileName)
' Derive the new filename.
strNewFileName = strFolder & "\" & fso.GetBaseName(strFolder) & "." & strExt
' Do the rename.
If strFileName <> strNewFileName Then
WScript.Echo "Renaming " & strFileName & " to " & strNewFileName
fso.MoveFile strFileName, strNewFileName
End If
End Sub