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

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.

Related

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

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.

Compare two files in two different folders and replace it by the newer

With this VBScript code I was able to copy files. If the file exists it does nothing, if not it will copy the files needed.
Dim Photo
SourceFolder = "C:\Photo1"
DistinationFolder = "C:\Photo2"
Set ObjPhoto = CreateObject("Scripting.FileSystemObject")
For Each Photo In ObjPhoto.GetFolder( SourceFolder).Files
If Not ObjPhoto.FileExists(ObjPhoto.BuildPath(DistinationFolder, Replace(Photo.Name, ".jpg", ".bmp"))) Then
photo.Copy ObjPhoto.BuildPath(DistinationFolder, Photo.Name), True
End If
Next
I want to compare the files if the source files also exists in the destination folder and replace it by the newer.
If you want to overwrite based on the last modified date, then the File object has the property you want: DateLastModified. (You can check all properties of the File object here.)
You already have access to the source file objects (your code's Photo variable) so you just need to get the target's file object.
Something like this should work:
Dim Photo
Dim targetFile, bmpTargetFilename, jpgTargetFilename
SourceFolder = "C:\Photo1"
DistinationFolder = "C:\Photo2"
Set ObjPhoto = CreateObject("Scripting.FileSystemObject")
For Each Photo In ObjPhoto.GetFolder(SourceFolder).Files
bmpTargetFilename = ObjPhoto.BuildPath(DistinationFolder, Replace(Photo.Name, ".jpg", ".bmp"))
jpgTargetFilename = ObjPhoto.BuildPath(DistinationFolder, Photo.Name)
If ObjPhoto.FileExists(bmpTargetFilename) Then
' Get the target file object
Set targetFile = ObjPhoto.GetFile(jpgTargetFilename)
' Now compare the last modified dates of both files
If Photo.DateLastModified > targetFile.DateLastModified Then
Photo.Copy jpgTargetFilename, True
End If
Else
Photo.Copy jpgTargetFilename, True
End If
Next
A couple of notes:
It seems you are checking for the existence of a .BMP file yet copying a .JPG file, so I made it explicit by using two variables.
I am also assuming you want to compare the JPG files, since those are the ones being copied.

Need to write filenames to a text file

I am very new to VB Scripting and I am looking to see if there is a way to look at a directory get the file names and then write those file names to a text file. I would think that the Path.GetFileName Method would work, but I can't seem to get it to work. Maybe I am using it the wrong way.
Here is a simple script that will echo the filenames in the "C:\Windows\" directory.
Set fs = CreateObject("Scripting.FileSystemObject")
'Log file name
Set logFile = fs.OpenTextFile("fileNameLogs.txt", 2, True)
'Directory you want listed
Set folder = fs.GetFolder("c:\windows\")
Set files = folder.Files
For Each file in files
wscript.echo file.name
logFile.writeline(file.name)
Next
logFile.close

How to parse one text file to multiple files based on data contained within

I have an enormous text file that I'd like to parse into other files - I think the only thing I have on this box (company computer) to use is VBS, so here's my question:
I have text file with a bunch of network configurations that looks like this:
"Active","HostName","IPAddress"
!
.......
end
This repeats itself throughout the file, but obviously for each configuration different data will occur within the "..." It always says "Active" for each configuration as well.
I want to create save files of the type HostName.cfg for each configuration and save all of the text between and including the ! and "end" . The line with the three quoted attributes doesn't need to be copied.
I'm still learning VBS so I'd appreciate any help in the matter. Thanks!
Here are some useful file reading functions and statements:
Freefile
Returns an integer which you should assign to a variable and then use in an Open statement.
Open <string> For Input As <integer>
Opens the specified file using the specified file handle. Don't use the same file handle twice without closing it in between.
Line Input #<integer>, <variable>
Reads one line of the file into a string.
Input #<integer>, <variable>, <variable>, <variable>
Reads one line of the file delimited by commas into several variables.
I once had to read a text file while omitting starting and ending lines. Though I didn't need to write it anywhere, FSO is simple enough that you will be able to figure it out. Here's some code for reading a file and link for writing to file via FSO: link
'Create a FSO object and open the file by providing the file path
Dim fso, f, filePath, line
filePath = "test.txt"
Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.OpenTextFile(filePath)
'Skip through the first two lines
f.readline
f.readline
'Read till the end of file, if the line is not "end", split it based on ","
while not f.AtEndOfStream
line = f.readline()
if line <> "end" then
arr = split(line, ",")
'Write the arr to file
end if
wend
f.Close

creation of a text file in a specified location and remove the old one

I need to create a text file "setup.txt" in the location C:\Documents and Settings\All Users\Application Data\xerox\setapp in VB script
the location C:\Documents and Settings\All Users\Application Data is common apllication data folder here we can
use word "CSIDL_COMMON_APPDATA" or &H23& for that.Here the other which i need to take care is if any setup.txt file are
already there in that location i need to remove that old one i need to insert the new "setup.txt" which contatin blank
values means new one
I am new to this vbscript ,and i wanted an optimized code to acheive that functionality
You could open the file for writing and write a blank line, this will create the new text file and overwrite any previous versions.
strFilename = " C:\Documents and Settings\All Users\Application Data\xerox\setapp.txt"
Set objFile = CreateObject("Scripting.FileSystemObject").OpenTextFile(strFilename,2,true)
'Write stuff to the file here
objFile.Close
Set objFile = nothing
It's not clear if your script is creating the log file, if not and you want to copy the file and overwrite any previous ones you could do this (setting the last argument in the CopyFile method to true will overwrite older versions).
Set fso = CreateObject("Scripting.FileSystemObject")
fso.CopyFile Source, Destination, true
Set fso = Nothing
If you just want to delete the previous file if it exists you could do this (here setting the last argument in the DeleteFile method to true will force deletion of the file).
Set fso = CreateObject("Scripting.FileSystemObject")
If fso.FileExists(file) Then
fso.DeleteFile file, true
End If
Set fso = Nothing
Wow, VBScript, it was a long time ago ... But I'll give it a shot.
What you need, if I understand you correctly, is to use the FileSystemObject it contains methods for deleting, creating and copying text files.
I hope this at least gives you some pointers to get you started.

Resources