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

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.

Related

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.

Open a file with notepad and save it, using vbscript

My problem is that I want to open a file in notepad and then save it from notepad, not manually but using vbscript (the reason I want this is to overcome some encoding issues). Until now I have found a relevant article which should solve my problem, but it doesn't work. It has this code:
Dim notepad, wndNotepad, strDesktop
Set notepad = Sys.Process("notepad")
Set wndNotepad = notepad.Window("Notepad")
' Open a file in Notepad
wndNotepad.MainMenu.Click("File|Open...")
notepad.Window("#32770", "Open").OpenFile "C:\Program Files\SmartBear\TestComplete 12\install.txt"
' Save the file to the desktop
strDesktop = WshShell.SpecialFolders("Desktop")
wndNotepad.MainMenu.Click "File|Save as..."
notepad.Window("#32770", "Save As").SaveFile strDesktop & "\install.txt"
The problem is that vbscript can't recognize the Sys in the second line (Sys.Process). I assume that in this line a process for notepad should be created (like here) and then something like an object of this process to be returned to the variable notepad (something I don't know if and how can be achived) in order to be used in the third line (notepad.Window("Notepad")).
If anyone has any idea how the code should be in order to work, I would really appreciate the help. Also any other suggestions on how to solve this problem (or any ideas about if it can actually be solved) are very welcome. Thank you for your time.
EDIT
As I said in the comments below, the above code needs the TestComplete software which is expensive. So if anyone has any idea for resolving my issue in another way (other software, other code, other programming language) I would be glad to learn it.
I think you need to copy a file from one path to another. Please refer the below code for copying Files,
Function CopyFile()
strSourceFile = "c:\.....\source.txt"
strDestFile = "c:\.....\dest.txt"
Set fso = CreateObject("Scripting.FileSystemObject")
'Check to see if the file is read-only
If Not fso.GetFile(DestinationFile).Attributes And 1 Then
'The file exists and is not read-only. Safe to replace the file.
fso.CopyFile strSourceFile, strDestFile, strOverWrite
Else
'The file exists and is read-only.
'Remove the read-only attribute
fso.GetFile(DestinationFile).Attributes = fso.GetFile(DestinationFile).Attributes - 1
'Replace the file
fso.CopyFile strSourceFile, strDestFile, strOverWrite
'Reapply the read-only attribute
fso.GetFile(DestinationFile).Attributes = fso.GetFile(DestinationFile).Attributes + 1
End If
End Function

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.

Using VBScript to examine properties of files within a zip file

I'm trying to use VBScript to examine the contents of several hundred .zip files. Essentially what I want to do is run through each .zip and find all of the files wihtin that zip file. For each one of these files within the zip, I want to record some information about it to an Oracle database. That information being: file name and file modified date.
So far, my solution has been extracting each zips folder structure to a temp folder then running through the temp folder with an fso object. However, this has been proven to be very slow.
Is there a way to accoplish this without unziping the zip files?
Ouch man. I have never heard of vbscript zip object. But it has been a long time since I have done vbscript. Is there anyway you can avoid it?
I did some googling for you. I did find this: http://www.example-code.com/vbscript/zip_List.asp Chilkat has done a lot of stuff I thought not possible. This gives me the impression - that what you are trying to do is not going to be painless.
If given the problem you have I would find a different solution than vbscript. But if you pull-it-off I would vote for you to be mayor of vb land
You can do it in place with Shell Objects. But it will be just as slow, maybe. If just name and date Explorer may get it direct from the zip directory (at the end of the file so the whole file still needs to be read).
This copies items in a folder to another folder. A zip file is a folder so it will copy in and copy out.
To Zip
Set objShell = CreateObject("Shell.Application")
Set Ag=Wscript.Arguments
set WshShell = WScript.CreateObject("WScript.Shell")
Set SrcFldr=objShell.NameSpace(Ag(1))
Set DestFldr=objShell.NameSpace(Ag(0))
Set FldrItems=SrcFldr.Items
DestFldr.CopyHere FldrItems, &H214
Msgbox "Finished"
To Unzip (note SrcFolder and DestFolder are reversed)
Set objShell = CreateObject("Shell.Application")
Set Ag=Wscript.Arguments
set WshShell = WScript.CreateObject("WScript.Shell")
Set DestFldr=objShell.NameSpace(Ag(1))
Set SrcFldr=objShell.NameSpace(Ag(0))
Set FldrItems=SrcFldr.Items
DestFldr.CopyHere FldrItems, &H214
Msgbox "Finished"
To Create a blank zip. (I should have used an ADODB binary stream rather than an FSO text stream, but it shouldn't matter)
Set Ag=Wscript.Arguments
Set fso = CreateObject("Scripting.FileSystemObject")
Set ts = fso.OpenTextFile(Ag(0), 8, vbtrue)
BlankZip = "PK" & Chr(5) & Chr(6)
For x = 0 to 17
BlankZip = BlankZip & Chr(0)
Next
ts.Write BlankZip

File wont move to folder vbs

So im writing a script that drops a file folder then moves that file to the folder it dropped it self. Well the folder drops fine but the file wont move. Can some see whats wrong with my code? Or give me a better way to move the file. I also get no error message about trying to move the file.
Dim folder,fso,filsys,C
Set fso = CreateObject("Scripting.filesystemObject")
Set folder = fso.GetSpecialFolder(1)
Set wshshell = CreateObject("wscript.shell")
Set filesys = CreateObject("scripting.filesystemobject")
Set objfso = CreateObject("Scripting.filesystemObject")
Set c = fso.GetFile(Wscript.scriptFullname)
On Error Resume NEXT
Set objFolder = objFSO.CreateFolder("C:\55egr932ntn7mk23n124kv1053bmoss5")
If Err.Number<>0 Then
End If
WScript.Sleep 3000
C.Move ("C:\552ntn7mk23n124kv1053bmoss5\File.exe") (folder&"\File.exe")
And I have a program I use that turns the VBS into and EXE so you see the "file.exe" which really is the .VBS itself
I'm not familiar with this syntax, but the line below looks like it's expecting the folder variable to be a string.
C.Move ("C:\552ntn7mk23n124kv1053bmoss5\File.exe") (folder&"\File.exe")
Earlier in code it looks as though you're setting folder as an object.
Set folder = fso.GetSpecialFolder(1)
You might not get the error you mentioned in your comment if you convert folder to a string.
~~
Another thing to try is the following code:
Set fso = CreateObject("Scripting.filesystemObject")
Set folder = fso.GetSpecialFolder(1)
Alert (folder&"\File.exe")
(I'm not sure if it's "Alert" or "Msgbox" or something else.) That test will show you whether the file path makes sense. If you get an error on line 3 of that test, try converting folder to a string before your Alert (or Msgbox).

Resources