FileSystemObject.CopyFile is not able to copy file from one folder to another - vbscript

I have a piece of VBS code where a file(C:\test.txt) should be copied to a newly created temporary folder. But it fails to copy the file. Weird thing is that the same function works fine when I have wild characters in the source parameter (C:\ *est.txt). Any advice would be really helpful.
set fso = createobject("Scripting.FileSystemObject")
if fso.FileExists(src_temp) then
'src_temp contains the file path.
'Path of the temporary folder
Set tmp_fld = fso.GetSpecialFolder(TemporaryFolder)
tmp_fld = tmp_fld & "\OldFiles_tmp"
'Create the temporary folder if does not exist
If Not fso.FolderExists(tmp_fld) Then
fso.CreateFolder(tmp_fld)
End If
'Copy the files to temporary path
On Error Resume Next
fso.CopyFile src_temp, tmp_fld, True 'last parameter is set as true for overwriting the existing
On Error Goto 0
End If
I have verified if destination temporary folder is created and also the path and other stuffs. How can a wild character in the path makes the CopyFile work and same does not work for complete file name. Also how to solve this issue?

Put a backslash (\) at the end of the destiny folder:
tmp_fld = tmp_fld & "\OldFiles_tmp\" 'note the \
I tested and it worked for me. But, strangely, the wildcard thing was not working for me in the original source code.

Related

Srcipt vbs, moveFolder return Permission denied

Option Explicit
dim fso
Set fso = CreateObject("Scripting.FileSystemObject")
If fso.FolderExists("C:\Users\michal\Desktop\mv_files_backup") then
fso.MoveFolder "C:\Users\michal.glowacki\Desktop\mv_files_backup\*.*", "\\192.168.10.245\backup\servers\backup_server"
Else
wscript.echo "doesn't exist"
End If
When I try run this script I have error:
Permission denied 800A0046
but when I change MoveFolder to CopyFolder script works correctly. Why I can't use function MoveFolder ?
From MSDN:
If source contains wildcards or destination ends with a path separator
( \ ), it is assumed that destination specifies an existing folder in
which to move the matching files. Otherwise, destination is assumed to
be the name of a destination folder to create. In either case, three
things can happen when an individual folder is moved:
If destination does not exist, the folder gets moved. This is the
usual case.
If destination is an existing file, an error occurs.
If destination is a directory, an error occurs.
An error also occurs if a wildcard character that is used in source
doesn't match any folders. The MoveFolder method stops on the first
error it encounters. No attempt is made to roll back any changes made
before the error occurs.
I've marked in bold the parts that explain your problem. Because your destination doesn't end with a trailing backslash, your script is attempting to create the folder, and failing. Try it with a \ added to the end of the destination and report back if it still doesn't work.
Try this:
fso.MoveFolder "C:\Users\michal.glowacki\Desktop\mv_files_backup", "\\192.168.10.245\backup\servers\backup_server\"

Replace a text file which may or may not contain a space in file name

I want to replace a file named with different name using VBScript. The input file name may or may not contain blank spaces.
Set objFSO = CreateObject("Scripting.FileSystemObject")
' First parameter: original location\file
' Second parameter: new location\file
objFSO.CopyFile "D:\Development\abc def.txt", "D:\Development\xyz.txt"
Perhaps surprisingly, CopyFile creates a copy of the source file. To rename a file you could use MoveFile, but the usual approach is to simply change the name of the file:
Set fso = CreateObject("Scripting.FileSystemobject")
fso.GetFile("D:\Development\abc def.txt").Name = "xyz.txt"
Edit: If you actually mean to replace one file with another file, you can do so with CopyFile by setting the third parameter (overwrite) to True, as #Lankymart pointed out in the comments.
fso.CopyFile "D:\Development\abc def.txt", "D:\Development\xyz.txt", True
If you don't want to keep the source file you need to delete it after the copy operation (VBScript doesn't allow moving a file over an existing file). Alternatively you can delete the destination file first and then move or rename the source file.

QTP: How to copy file from onle location and paste it into another and verify file count at the end

I need to write a vbscript for Copying files from local directory(Windows) to another(shared drive) and verify the counts at the end to make sure everything copied successfully. Any ideas on what the script will look like?
Here is what I recorded using GUI UFT:
SystemUtil.Run "C:\Users\Downloads"
Window("Documents").WinObject("Items View").WinList("Items View").Activate "Unified Functional Testing"
Window("Documents").WinObject("Items View").WinList("Items View").Select "APITest1"
Window("Documents").WinObject("ShellView").WinMenu("ContextMenu").Select "Copy"
Window("Documents").Restore Window("Documents").WinTreeView("WinTreeView").Select "Desktop;This PC;Downloads"
Window("Documents").WinObject("ShellView").WinMenu("ContextMenu").Select "Paste"
To copy files from one foler to another, Why do you record using QTP/UFT? The script recorded by QTP will not be reliable. (might not work everytime.) QTP supports VBScript. It is easy to copy files from one folder to another folder using VBScript.
To copy all files from temp1 to temp2 folder - just these 2 lines will do
Set oFSO = CreateObject("Scripting.FileSystemObject")
oFSO.CopyFile "C:\vIns\temp1\*.*" , "C:\vIns\temp2\" , TRUE
Once files are moved, You want to compare the files count. (I assume temp2 folder was empty before copying the files)
iTemp1Count = oFSO.getFolder("C:\vIns\temp1\").Files.Count
iTemp2Count = oFSO.getFolder("C:\vIns\temp2\").Files.Count
If iTemp1Count = iTemp2Count Then
Msgbox "all files are copied"
Else
Msgbox "Something is wrong!!!"
End If

VBScript to move file with wildcard, if it exists

I am attempting to create a script that checks for the existence of archived eventlog files and, if any files exist, moves them to another folder. Running this script does nothing and gives no errors. I believe the wildcard in the If statement is what is giving me issues. I am new to vbscript, and scripting in general, and would appreciate some advice.
Set fso = CreateObject("Scripting.FileSystemObject")
If (fso.FileExists("d:\eventlogs\Archive*.evtx")) Then
FSO.CopyFile "d:\eventlogs\Archive*.evtx" , "d:\eventlogs\archive\"
FSO.Deletefile "d:\eventlogs\archive*.evtx"
End if
You can replicate a wild card search by using a combination of instr() and right(), or just multiple instr().
Set objFSO = CreateObject("Scripting.FileSystemObject")
objStartFolder = "d:\eventlogs\"
Set objFolder = objFSO.GetFolder(objStartFolder)
Set colFiles = objFolder.Files
For Each objFile in colFiles
if instr(objFile.Name,"Archive") <> 0 AND instr(objFile.Name,".evtx") <> 0 then
objFSO.MoveFile objFile.Name, "archive\" + objFile.Name
end if
Next
The appropriate approach of finding files with wildcards in VBScript:
Get the file collection from the containing folder
For each file in the filecollection:
Test the filename with a regular expression on a certain pattern
If the test passes, do some action with this file
Next file
Late answer, but might be useful because apparently nobody spotted the mistake.
From the VBScript documentation (script56.chm in my case), help page for CopyFile method says:
FileExists Method
Returns True if a specified file exists; False if it does
not.
object.FileExists(filespec)
Arguments
object
Required. Always the name of a FileSystemObject.
filespec
Required. The name of the file whose existence is to be determined. A complete path specification (either absolute or relative) must be provided if the file isn't expected to exist in the current folder.
Hence your expression fso.FileExists("d:\eventlogs\Archive*.evtx") returns False here; indeed there isn't any file named Archive*.evtx in your folder.
Either you remove your test, but you'll have to deal with the error the CopyFile method might generate, as doc says:
An error also occurs if a source using wildcard characters doesn't match any files.
As suggested by #automatedchaos in his answer https://stackoverflow.com/a/20907209/666414 you can also loop through files of the folder and decide what to do whenever the filename/extension matches your pattern.
Lastly, you can mix both solutions: loop through files of the folder, then set a flag to True and Exit Loop as soon as you encounter an expected file, then use the CopyFile method.
Like this:
With CreateObject("Scripting.FileSystemObject")
For Each objFile in .GetFolder("d:\eventlogs\").Files
If Left(objFile.Name, 7) = "Archive" And .GetExtensionName(objFile) = "evtx" Then
archiveFound = True
End If
Next
If archiveFound Then
.CopyFile "d:\eventlogs\Archive*.evtx", "d:\eventlogs\archive\"
.DeleteFile "d:\eventlogs\Archive*.evtx"
End If
End With
Note the wildcards work with the DeleteFile method too!

Renaming folders with VBScript

I need to be able to often renaming multiple folders in ASP. None of the methods that I've found are working for me. Here is the current method that I am trying-
Set FS = CreateObject("Scripting.FileSystemObject")
FS.MoveFolder "/images/715", "/images/V14"
This, as well as others that I've tried, always gives me a "path not found" error. I know that the directory that the script is in has these folders in it because I've been renaming them manually. Does the script need to know the full path? What if I don't know what the full path may be?
Update:
The script runs in a directory named "/ifp". I also tried this, with and without the "/ifp" and both forward and back slashes, and it also gave path not found. Once this works on my testing server I send it to the guy with the production server so I can't use any static directory names other than the "/images/????"
Dim sCurPath
sCurPath = CreateObject("Scripting.FileSystemObject").GetAbsolutePathName(".")
FS.MoveFolder sCurPath & "\ifp\images\715", sCurPath & "\ifp\images\V14"
Use absolute paths (C:\images\715). It would look like this:
Set FS = CreateObject("Scripting.FileSystemObject")
FS.MoveFolder "C:\images\715", "C:\images\V14"
If you are using a -nix system then keep in mind that the first / is the root directory - ensure you do indeed have an images folder in the root directory path.
Also note that in order to rename the folder there cannot be trailing \'s after the folder names and the script must be run on the local machine.
If you keep getting path errors, you're passing it incorrectly somehow. Build in some error checking to see what is going wrong:
Dim tmpPath = "\ifp\images\715"
Dim newPath = "\ifp\images\V14"
If FS.FolderExists(sCurPath & tmpPath) Then
Response.Write("The folder exists.")
FS.MoveFolder sCurPath & tmpPath, sCurPath & newPath
Else
Response.Write("The folder " & sCurPath & tmpPath & " does not exist.")
End If

Resources