This question already has answers here:
Copy a file from one folder to another using vbscripting
(5 answers)
Closed 2 years ago.
I want to make an automatic backup of my excel files using vbscript.
It works to copy the entire folder but I want to copy only the xlsx files.
Here is the code until now:
Dim objFSO, objFolder, evrFiles
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set evrFiles = objFolder.Files
For Each evrFile in evrFiles
If InStr(1, evrFile.Name, ".xlsx", vbBinaryCompare) > 0 Then
objFSO.CopyFile "C:\Users\Home\Desktop\vbs\" & evrFile.Name, "E:\test2"
End If
Next
WScript.Quit
It throws error on line 5 char 1 "Object required: " "
Any ideas?
LE: I have also tried:
Dim objFSO, objFolder
Set wshNetwork = CreateObject("WScript.Network")
strUser = wshNetwork.Username
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.GetFolder("C:\Users\" & strUser & "\Desktop\vbs")
Set evrFiles = objFolder.Files
For Each evrFile in evrFiles
If InStr(1, evrFile.Name, ".xlsx", vbBinaryCompare) > 0 Then
objFSO.CopyFile "C:\Users\" & strUser & "\Desktop\vbs\" & evrFile.Name, "E:\test2"
End If
Next
WScript.Quit
But this one gives me "Permission denied on line 9 char 3"
This one works(to copy the entire folder) but I want only the excel files.
Dim objFSO
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set wshNetwork = CreateObject("WScript.Network")
strUser = wshNetwork.Username
objFSO.CopyFolder"C:\Users\" & strUser & "\Desktop\vbs","E:\test2"
You dont need to iterrate each file, you could do something like:
Dim objFSO
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set wshNetwork = CreateObject("WScript.Network")
strUser = wshNetwork.Username
objFSO.CopyFile "C:\Users\" & strUser & "\Desktop\vbs\*.xlsx", "E:\test2\"
You may also want to set the overwrite flag when doing the copy, it there will be existing files already in the destination folder.
objFSO.CopyFile "C:\Users\" & strUser & "\Desktop\vbs\*.xlsx","E:\test2", True
Related
I have this script to allow me to insert text into a text file but I need it to be at the start of the text file. This script currently adds this to the end of the .txt file.
And I am new to trying these things out myself
Option Explicit
Dim objFSO, objFolder, objShell, objTextFile, objFile
Dim strDirectory, strFile, strText
strDirectory = "c:\scripts"
strFile = "\csv.txt"
strText = "sep=|"
Set objFSO = CreateObject("Scripting.FileSystemObject")
If objFSO.FolderExists(strDirectory) Then
Set objFolder = objFSO.GetFolder(strDirectory)
Else
Set objFolder = objFSO.CreateFolder(strDirectory)
WScript.Echo "Just created " & strDirectory
End If
If objFSO.FileExists(strDirectory & strFile) Then
Set objFolder = objFSO.GetFolder(strDirectory)
Else
Set objFile = objFSO.CreateTextFile(strDirectory & strFile)
Wscript.Echo "Just created " & strDirectory & strFile
End If
set objFile = nothing
set objFolder = nothing
Const ForAppending = 8
Set objTextFile = objFSO.OpenTextFile _
(strDirectory & strFile, ForAppending, True)
objTextFile.WriteLine(strText)
objTextFile.Close
WScript.Quit
You can read the file's content into a string, add your string in front of that and write everything back to the same file.
Dim sFileText
Dim sPrependText
Const ForReading = 1, ForWriting = 2
' Open file For Reading and Read All content to a variable
Set objTextFile = objFSO.OpenTextFile (strDirectory & strFile, ForReading, True)
sFileText = objTextFile.ReadAll
objTextFile.Close
' Prepend text in front of file's content
sPrependText = "sep=|"
sFileText = sPrependText & sFileText
' Open file For Writing and write text variable
Set objTextFile = objFSO.OpenTextFile (strDirectory & strFile, ForWriting, True)
objTextFile.Write sFileText
objTextFile.Close
The problem is that I tried to make a file on somebody else's desktop but I don't know what to write in the users.
I tried "C:\Users\" & strUser & "\desktop\Test"
Set fso = CreateObject("Scripting.filesystemobject")
fso.Createfolder "C:\Users\" & strUser & "\desktop\Test"
error: Can't find the file location.
Use the special folder Shell function to return the path to the desktop.
set objShell = Wscript.CreateObject("Wscript.Shell")
strDesktopPath = objShell.SpecialFolders("Desktop")
Set fso = CreateObject("Scripting.filesystemobject")
fso.Createfolder strDesktopPath & "\Test"
I think your code will work if you simply add a \ at the end of your path:
Set fso = CreateObject("Scripting.filesystemobject")
fso.Createfolder "C:\Users\" & strUser & "\desktop\Test\"
Hi I'm trying to copy a file from source to target folder. But I'm getting an error "Bad File name or number" when im running it.
Here is my code:
Option Explicit
Dim srcFolder, trgFolder
srcFolder = "\\sunpowercorp.com\spap\SPMM-QA\Public-Read_Write\SPMM QA Documents\Dominic Yumul\Toshiba Monthly Quality Report"
trgFolder = "http:\\dms\departments\QUALITY\Quality Ops in the Box\Quality Ops in the Box library\025 SPMM QA Staff Documents\Toshiba Monthly Quality Report"
CopyFilesAndFolders srcFolder, trgFolder
WScript.Quit
Sub CopyFilesAndFolders(ByVal strSource, ByVal strDestination)
Dim ObjFSO, ObjFolder, ObjSubFolder, ObjFile, files
Dim TargetPath
Set ObjFSO = CreateObject("scripting.filesystemobject")
'connecting to the folder where is going to be searched
Set ObjFolder = ObjFSO.GetFolder(strSource)
TargetPath = Replace (objFolder.path & "\", strSource, strDestination,1,-1,vbTextCompare)
If Not ObjFSO.FolderExists (TargetPath) Then ObjFSO.CreateFolder (TargetPath)
Err.clear
On Error Resume Next
'Check all files in a folder
For Each objFile In ObjFolder.files
If Err.Number <> 0 Then Exit For 'If no permission or no files in folder
On Error goto 0
If CheckToCopyFile (objFile.path, TargetPath & "\" & objFile.name) Then
objFSO.copyfile objFile.path, TargetPath & "\" & objFile.name, True
End If
Next
'Recurse through all of the subfolders
On Error Resume Next
Err.clear
For Each objSubFolder In ObjFolder.subFolders
If Err.Number <> 0 Then Exit For 'If no permission or no subfolder in folder
On Error goto 0
'For each found subfolder there will be searched for files
CopyFilesAndFolders ObjSubFolder.Path & "\", TargetPath & ObjSubFolder.name & "\"
Next
Set ObjFile = Nothing
Set ObjSubFolder = Nothing
Set ObjFolder = Nothing
Set ObjFSO = Nothing
End Sub
Function CheckToCopyFile(ByVal strSourceFilePath, ByVal strDestFilePath)
Dim oFSO, oFile, SourceFileModTime, DestFileModTime
CheckToCopyFile = True
Set oFSO = CreateObject("scripting.filesystemobject")
If Not oFSO.FileExists (strDestFilePath) Then Exit Function
Set oFile = oFSO.GetFile (strSourceFilePath)
SourceFileModTime = oFile.DateLastModified
Set oFile = Nothing
Set oFile = oFSO.GetFile (strDestFilePath)
DestFileModTime = oFile.DateLastModified
Set oFile = Nothing
If SourceFileModTime =< DestFileModTime Then CheckToCopyFile = False
Set oFSO = Nothing
End Function
I do not know what line I am getting the error.
On Windows Vista and newer you should be able to map SharePoint libraries to drive letters using WebDAV as described in this blog post:
Set net = CreateObject("WScript.Network")
net.MapNetworkDrive "X:", "\\sp.example.org#SSL\site\Shared Documents\"
Then copy your files to the mapped drive using FileSystemObject methods.
When I run my vbscript, it says(In Windows Script Host):
C:\Users\admin\Desktop\Test.vbs
Line: 34
Char:1
Error: The process cannot access the file because it is being used by another process.
Code: 80070020
Source: (null)
How would I be able to fix this? Also here's the script...
Set objFSO = CreateObject("Scripting.FileSystemObject")
Dim objFSO, objFolder, objShell, objFile
Dim strDirectory, strFile
strDirectory = "c:\Folder"
strFile = "\Hidden.bat"
If objFSO.FolderExists(strDirectory) Then
Set objFolder = objFSO.GetFolder(strDirectory)
Else
Set objFolder = objFSO.CreateFolder(strDirectory)
End If
If objFSO.FileExists(strDirectory & strFile) Then
Set objFolder = objFSO.GetFolder(strDirectory)
Else
Set objFile = objFSO.CreateTextFile(strDirectory & strFile)
End If
set objFolder = nothing
set objFile = nothing
Const fsoForAppend = 8
Set objFSO = CreateObject("Scripting.FileSystemObject")
Dim objTextStream
Set objTextStream = objFSO.OpenTextFile("C:\Folder\Hidden.bat", fsoForAppend)
objTextStream.WriteLine "attrib ""Folder"" +s +h"
Set objShell = WScript.CreateObject("WScript.Shell")
objShell.Run """C:\Folder\Hidden.bat"""
Set objShell = Nothing
Without creating any batch file to hide your folder :
Option Explicit
Dim objFSO,objFolder,strDirectory,Command,Result,objShell
Set objFSO = CreateObject("Scripting.FileSystemObject")
strDirectory = "C:\Folder"
If objFSO.FolderExists(strDirectory) Then
Set objFolder = objFSO.GetFolder(strDirectory)
Else
Set objFolder = objFSO.CreateFolder(strDirectory)
End If
set objFolder = nothing
Command = "Cmd /c Attrib +s +h "& DblQuote(strDirectory) &""
Set objShell = CreateObject("WScript.Shell")
Result = objShell.Run(Command,0,True)
Set objShell = Nothing
'****************************************************************
Function DblQuote(str)
DblQuote = Chr(34) & Str & Chr(34)
End Function
'****************************************************************
Closes an open TextStream file.
object.Close
From Help.
You need to close it after writing to it before using it.
New to VBScript and having a problem grasping this concept.
This is the code:
Set WshNetwork = WScript.CreateObject("WScript.Network")
strCompName = WshNetwork.Computername
Wscript.Echo WshNetwork.Username >j:\strCompName.txt
WScript.Quit()
Basically I want to the username dumped to a text file and the text file should be named with the name of the computer. I've tried putting the strCompName in quotes, single quotes, parenthesis with no success.
Here is the code that you can use. You need to use FileSystemObject. The FileSystemObject is used to gain access to a computer's file system. It can create new files and access existing ones.
Set WshNetwork = WScript.CreateObject("WScript.Network")
Set objFSO = CreateObject("Scripting.FileSystemObject")
strCompName = WshNetwork.Computername
'writing to file
outFile="c:\TEMP\" & strCompName & ".txt"
Set objFile = objFSO.CreateTextFile(outFile,True)
objFile.Write WshNetwork.Username & vbCrLf
objFile.Close
Set objFile = Nothing
Set objFSO = Nothing
Set WshNetwork = Nothing
WScript.Quit()
Save this in .vbs file and run and you will get a text file with computer name in TEMP folder (Change the path if you like).
This code should work. This code opens the file and appends it if the file exists or creates a file and writed to it if it does not exist.
'constants
Const ForReading = 1, ForWriting = 2, ForAppending = 8
Const TristateUseDefault = -2, TristateTrue = -1, TristateFalse = 0
'Load domain, username, & computer variables
Set oShell = CreateObject( "WScript.Shell" )
sDomain = oShell.ExpandEnvironmentStrings( "%USERDOMAIN%" )
sUserName = oShell.ExpandEnvironmentStrings( "%USERNAME%" )
sComputer = oShell.ExpandEnvironmentStrings( "%COMPUTERNAME%" )
'Setup filesystemobject
Set oFSO=CreateObject("Scripting.FileSystemObject")
'Check to see if file exists. If exists open it forAppending
'else create file and write to it.
outFile="c:\export\" & sComputer & ".txt"
If oFSO.FileExists(outFile) Then
Set objFile = oFSO.OpenTextFile(outFile, ForAppending, True, TristateTrue)
Else
Set objFile = oFSO.CreateTextFile(outFile,True)
End If
'write to file
objFile.WriteLine sDomain & "\" & sUsername & " - " & Now
'clean up objects
objFile.Close
Set objFile = Nothing
Set oFSO = Nothing
Set oShell = Nothing