Bad filename or number : vbs error - vbscript

sry guys m new to programming. I was trying to make a vbscript to cope a vbs file from current location to system startup folder. But m getting the error bad file name or number. but when i give path manually it works like a charm. The path my code is self picking is also correct. Cant understand what is the problem. Please help me. Here is my code.
Set objShell = Wscript.CreateObject("Wscript.Shell")
strMyPath = objShell.SpecialFolders("Startup")
wscript.echo strPath
wscript.echo strMyPath
'Const strMyPath = "C:\Users\Bilal\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup\"
Const SourceFile = "abc.vbs"
Set fso = CreateObject("Scripting.FileSystemObject")
'Check to see if the file already exists in the destination folder
If fso.FileExists(strMyPath) Then
'Check to see if the file is read-only
If Not fso.GetFile(strMyPath).Attributes And 1 Then
'The file exists and is not read-only. Safe to replace the file.
fso.CopyFile SourceFile, strMyPath, True
Else
'The file exists and is read-only.
'Remove the read-only attribute
fso.GetFile(strMyPath).Attributes = fso.GetFile(strMyPath).Attributes - 1
'Replace the file
fso.CopyFile SourceFile, strMyPath, True
'Reapply the read-only attribute
fso.GetFile(strMyPath).Attributes = fso.GetFile(strMyPath).Attributes + 1
End If
Else
'The file does not exist in the destination folder. Safe to copy file to this folder.
fso.CopyFile SourceFile, myStrPath, True
End If
Set fso = Nothing

Ok. So it should look like this:
Set objShell = Wscript.CreateObject("Wscript.Shell")
strPath = objShell.SpecialFolders("Startup")
strMyPath = strPath&"\"
Const SourceFile = "abc.vbs"
strMyPath = strMyPath & SourceFile
Set fso = CreateObject("Scripting.FileSystemObject")
'Check to see if the file already exists in the destination folder
If fso.FileExists(strMyPath) Then
'Check to see if the file is read-only
If Not fso.GetFile(strMyPath).Attributes And 1 Then
'The file exists and is not read-only. Safe to replace the file.
fso.CopyFile SourceFile, strMyPath, True
Else
'The file exists and is read-only.
'Remove the read-only attribute
fso.GetFile(strMyPath).Attributes = fso.GetFile(strMyPath).Attributes - 1
'Replace the file
fso.CopyFile SourceFile, strMyPath, True
'Reapply the read-only attribute
fso.GetFile(strMyPath).Attributes = fso.GetFile(strMyPath).Attributes + 1
End If
Else
'The file does not exist in the destination folder. Safe to copy file to this folder.
fso.CopyFile SourceFile, strMyPath, True
End If
Set fso = Nothing

To make
If fso.FileExists(strMyPath) Then
'work', strMyPath must contain a valid file specification. As far as I can see, in your code it contains the path to the (destination?) folder.
Use properly named variable (names)s that make clear whether they hold folder or file specs.

Related

Overwrite files when user open files with write access

I am writing a VBScript to copy file from source to target. It works copy from source to target but if we are trying to overwrite the file to target and if any user open that same file in target it's not overwriting it.
Note: the user open the file has write access.
Could someone please help on this? Below is the function but have to delete file that are also open in destination.
Sub Clear_All_Files_And_SubFolders_In_Folder()
'Delete all files and subfolders
'Be sure that no file is open in the folder
Dim FSO As Object
Dim MyPath As String
Set FSO = CreateObject("scripting.filesystemobject")
MyPath = "C:\Users\Ron\Test" '<< Change
If Right(MyPath, 1) = "\" Then
MyPath = Left(MyPath, Len(MyPath) - 1)
End If
If FSO.FolderExists(MyPath) = False Then
MsgBox MyPath & " doesn't exist"
Exit Sub
End If
On Error Resume Next
'Delete files
FSO.DeleteFile MyPath & "\*.*", True
'Delete subfolders
FSO.DeleteFolder MyPath & "\*.*", True
On Error GoTo 0
End Sub

Unable to use the values in vbscript (for file copy) fetched from an XML file

What we are trying to achieve: To read directory paths from an XML file and perform a copy and paste operation. The script works if we remove the last line: fso.CopyFolder Directory, t. But it fails when we keep this line. The script is able to read the values from the XML file.
Error occurs at line no: 19
i.e.: For each child in objRoot.childNodes, saying “Object Required”
Option Explicit
set fso = CreateObject("Scripting.FileSystemObject")
dim fso, objDoc, objRoot, child, s, t, WshShell, filesys, ObjShell,objDox
Set objDoc = CreateObject("MSXML.DOMDocument")
objDoc.Async = False
objDoc.Load "location.xml"
objDoc.validateOnParse=False
Set objRoot = objDoc.documentElement
Dim CurrentDirectory, Directory
CurrentDirectory = fso.GetAbsolutePathName(".")
For Each child in objRoot.childNodes
s = child.getAttribute("Source")
t = child.getAttribute("Destination")
Directory = CurrentDirectory & "\" & s
Next
fso.CopyFolder Directory, t
Try Changing
fso.CopyFolder Directory, t
to
fso.CopyFolder Directory, t & "\"
because when copying or moving to a folder you have to end the destination folder with a "\"

VBScript to Rename File with Specific Prefix in Folder

I have successfully created a VBScript that renames a file as required when it is the only file in the folder. I cannot figure out how to have the script search past the most recent file.
Option Explicit
Dim fso, folder, file, tmFile
Dim folderName
folderName = "\\pcc\Int\PC\Inbox\"
Set fso = CreateObject("Scripting.FileSystemObject")
Set folder = fso.GetFolder(folderName)
Set tmFile = Nothing
For each file In folder.Files
If (tmFile is Nothing) Then
Set tmFile = file
Exit For
End IF
Next
If InStr(tmfile.name, "TM") Then
TmFile.Name = Replace(tmFile.Name, ".txt", "A.txt")
End if
The above script correctly renames the file.
Here are a few modifications I have tried to go through all of the files in the folder to search for the file that has the prefix TM. This will always be the only file with the TM prefix.
For Each InStr(tmFile.name, "TM") Then
tmFile.Name = Replace(tmFile.Name, ".txt", "A.txt")
Exit for
and
If tmFile.fileexists(tmFile.name, "TM") Then
tmFile.Name = Replace(tmFile.Name, ".txt", "A.txt")
End if
You are close with your instr(), it's just that you need to put that test within your already existing For loop:
Option Explicit
Dim fso, folder, file, tmFile
Dim folderName
folderName = "\\pcc\Int\PC\Inbox\"
Set fso = CreateObject("Scripting.FileSystemObject")
Set folder = fso.GetFolder(folderName)
For each file In folder.Files
If instr(file, "TM") > 0 THEN
file.name = replace(file.name, ".txt", "A.txt")
End IF
Next
I've removed the tmfile variable since it's simply not needed here.

comparing/copying largest files to new folder

What I wish to do is:
Copy files from a variety of sub-folders under a single main folder to a destination folder.
Three options when copying:
If no file in destination folder exists then copy.
If file exists, copy over if filesize is larger than destination file.
If file exists and both are the same filesize compare date/time and copy over if most recent.
Here is my pseudocode so far:
Dim filesys, strSourceFile, strDestFolder, strDestFile
Set filesys = CreateObject("Scripting.FileSystemObject")
strSourceFile = S:\SoCal\Section_2\*\Autogen\texture\*.agn
strDestFolder = F:\ADDON_SCENERY\simwestSOCAL\texture
strDestFile = F:\ADDON_SCENERY\simwestSOCAL\texture\*.agn
COPY each file in strSourceFolder
If IsEmpty (SourceFile, DestFolder)
Else If (SourceFile FileSize > DestFile)
Else If (SourceFile DateTime > DestFile DateTime)
Then 'keep/copy most recent file
End if
Am I on the right track?
Do I need to add a Loop?
Can one compare file sizes? All my research has found nothing yet on this.
Can I compare Date and Time against files?
As an update to my original post... (hope I am following forum rules correctly),
I have spent the last several weeks non-stop just reading-reading-reading and testing-failure-testing. I am happy to say (and a little proud), that I have completed my very first script... and it appears to work as planned but for just one file. I now need to convert this to work on all files inside my 'sourcefolder'.
I am a bit "brain dead" from this so any direction on converting this would be most appreciated. I know I need loops but what type and where? Do I rename everything referring to a file to a folder or use '*.txt' for files? In the meantime I will keep studying.
Here is my script (yea, lot's of MsgBox's so I could follow along the script path):
dim dFolder
dFolder = "S:\Scripting Workfolder\destfolder\"
dim dFile
dFile= "S:\Scripting Workfolder\destfolder\File 1.txt"
dim sFile
sFile = "S:\Scripting Workfolder\sourcefolder\File 1.txt"
Set fso = CreateObject("Scripting.FileSystemObject")
'Check to see if the file already exists in the destination folder
If Not fso.FileExists(dFile) Then
MsgBox "File does not exist - will copy over to dFolder"
fso.CopyFile sFile, dFolder, true
Elseif fso.FileExists(dFile) Then
MsgBox "File already exist in destination folder determine largest"
ReplaceIfLarger sFile, dFile
End If
Sub ReplaceIfLarger(sFile, dFile)
const overwrite_existing = true
dim objFSO
set objFSO = createobject("Scripting.FileSystemObject")
dim objSourceFile
set objSourceFile = objFSO.GetFile(sFile)
'dim kbSourceSize
kbSourceSize = objSourceFile.size
dim objTargetFile
set objTargetFile = objFSO.GetFile (dFile)
'dim kbTargetSize
kbTargetSize = objTargetFile.size
If kbSourceSize > kbTargetSize Then
MsgBox "Source file is LARGER and will overwrite to dest folder"
objFSO.CopyFile objSourceFile.Path, objTargetFile.Path, overwrite_existing
ElseIf kbSourceSize < kbTargetSize Then
MsgBox "Source file is smaller - Will not overwrite to dest folder"
Else
ReplaceIfNewer sFile, dFile
End If
End Sub
Sub ReplaceIfNewer(sFile, dFile)
MsgBox "Both files exist and are the same size. Keep newest file"
const overwrite_existing = true
dim objFSO
set objFSO = createobject("Scripting.FileSystemObject")
dim dtmSourceFile
set dtmSourceFile = objFSO.GetFile(sFile)
dim dtmTargetFile
set dtmTargetFile = objFSO.GetFile(dFile)
If (dtmSourceFile.DateLastModified > dtmTargetFile.DateLastModified) then
MsgBox "Source File is Newer than Target File - Overwrite Target file"
objFSO.CopyFile dtmSourceFile.Path, dtmTargetFile.Path, overwrite_existing
Else
MsgBox "Source File is Older than Target File - Will not overwrite file"
End If
End Sub

Make a directory and copy a file

In VBS how do you make a directory and then copy a file into it?
Id like to make a folder in the root of C e.g. C:\folder and then copy a file from \server\folder\file.ext into that new folder
Use the FileSystemObject object, namely, its CreateFolder and CopyFile methods. Basically, this is what your script will look like:
Dim oFSO
Set oFSO = CreateObject("Scripting.FileSystemObject")
' Create a new folder
oFSO.CreateFolder "C:\MyFolder"
' Copy a file into the new folder
' Note that the destination folder path must end with a path separator (\)
oFSO.CopyFile "\\server\folder\file.ext", "C:\MyFolder\"
You may also want to add additional logic, like checking whether the folder you want to create already exists (because CreateFolder raises an error in this case) or specifying whether or not to overwrite the file being copied. So, you can end up with this:
Const strFolder = "C:\MyFolder\", strFile = "\\server\folder\file.ext"
Const Overwrite = True
Dim oFSO
Set oFSO = CreateObject("Scripting.FileSystemObject")
If Not oFSO.FolderExists(strFolder) Then
oFSO.CreateFolder strFolder
End If
oFSO.CopyFile strFile, strFolder, Overwrite
You can use the shell for this purpose.
Set shl = CreateObject("WScript.Shell")
shl.Run "cmd mkdir YourDir" & copy "

Resources