I'm trying to copy a line of text from a .txt -> paste into another file and save. The code I have keeps giving me errors at the paste section. I am completely new at this and learning as I go. My main goal is to paste the info after Host= in another file. But I need to get this down first.
Here is my code so far
///OPEN FILE and READ
Set objFileToRead = CreateObject("Scripting.FileSystemObject").OpenTextFile("C:\vnc\vnc.txt",1)
strFileText = objFileToRead.ReadAll()
objFileToRead.Close
' ///PASTE
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFileToWrite = objFSO.OpenTextFile("c:\vnc\testfile.vnc", 2)
objFileToWrite.Write strFileText
objFileToWrite.Close
This works for me:
'//OPEN FILE and READ
Set objFileToRead = CreateObject("Scripting.FileSystemObject").OpenTextFile("C:\vnc.txt",1)
strFileText = objFileToRead.ReadAll()
objFileToRead.Close
' ///PASTE
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFileToWrite = objFSO.OpenTextFile("c:\vnc.vnc", 2)
objFileToWrite.Write strFileText
objFileToWrite.Close
The only difference I made was remove the subfolder, and put in the root of C: The script worked.
I manually created both the source file and target file. If both files exist, and are not locked (as if you had it open / locked in another application), then the permissions of that VNC folder must be the issue.
If still your file permission denied you to write then you have to do change the security of that file using right click of mouse, and update advanced setting.
Related
I have vbscript application that run in a server and I tried run that application in my local computer. The problem is I want to create a text file in server directory but I always end up with an error something like "disk is not ready". When I check, it is because in my local computer, there is only 1 partition Drive C: and for some reason the application try to make the textfile in my local directory. And if change the path to drive C: it works and the file is existed in my local directory. So what am I doing wrong? I want the text file is created in server directory.
Here's part of my code :
Dim fso, MyFile
Set fso = CreateObject("Scripting.FileSystemObject")
Set MyFile = fso.CreateTextFile("D:\tesfolder\SomeText.txt", True)
MyFile.WriteLine("This is a test.")
MyFile.Close
Set MyFile = Nothing
Set fso = Nothing
please help me
Set fso = CreateObject("Scripting.FileSystemObject")
Set MyFile = fso.CreateTextFile("\\127.0.0.1\C$\SomeText.txt", True)
MyFile.WriteLine("This is a test.")
MyFile.Close
The above needs Admin because it uses the Admin only share (present on all computers) c$ and writes to the root of C: drive.
The format is
\\ServerName(orIP)\ShareName\Folder\File.ext
An admin would
\\Computer\D$\tesfolder\SomeText.txt
For a user substitute their share name for D$.
I have a text file C:\user\test.txt with the text "This is a test", and I want to physically open the file using VBScript (as if i were to click Accesories → Notepad). I want to see the file open on my screen so I can visually read the text.
Now, for some reason I can't. This is what I'm trying (I tried with and without the "textfile.close" line, and yes, the file exists in that path):
dim FS1, textfile
Const ForReading = 1
set FS1 = CreateObject("Scripting.FileSystemObject")
set textfile = FS1.OpenTextFile("C:\user\test.txt", ForReading, True)
textfile.close
What am I missing? I have no issue creating, writing, or appending... but I just can't open it!
If you want to open the file in Notepad:
With CreateObject("WScript.Shell")
.Run "notepad.exe c:\user\test.txt"
End With
You can also just run the file and it will open in your default txt editor (whatever Windows file association you have established for extension txt).
With CreateObject("WScript.Shell")
.Run "c:\user\test.txt"
End With
Are you reading the file?
You should be doing something like
' Open the file for input.
Set MyFile = fso.OpenTextFile(FileName, ForReading)
' Read from the file and display the results.
Do While MyFile.AtEndOfStream <> True
TextLine = MyFile.ReadLine
Document.Write TextLine & "<br />"
Loop
MyFile.Close
https://msdn.microsoft.com/en-us/library/314cz14s(v=vs.84).aspx
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).
I want to append a line to C:\Windows\System32\drivers\etc\hosts using VBScript. I tried to read this file first using this code:
Set filestreamIN = CreateObject("Scripting.FileSystemObject").OpenTextFile("C:\Windows\System32\drivers\etc\hosts",2,true)
file = Split(filestreamIN.ReadAll(), vbCrLf)
for i = LBound(file) to UBound(file)
msgbox file(i)
Next
filestreamIN.Close()
Set filestreamIN = Nothing
But I got an error in the second line: Bad file mode. I ran it using this command:
cscript "D:\Project\AXA\AXADEPROJ-867\add host.vbs"
with cmd being run as an administrator. Any help would be great.
Open the file for appending, and simply output what you want. It will automatically be appended.
Set oFSO = CreateObject("Scripting.FileSystemObject")
Set oHosts = oFSO.GetFile("C:\Windows\System32\drivers\etc\hosts")
WScript.Echo oHosts.attributes
Set fileAPPEND = _
oFSO.OpenTextFile("C:\Windows\System32\drivers\etc\hosts", 8, true)
fileAPPEND.Write("192.168.0.1 MyMachine")
fileAPPEND.Close()
Set fileAPPEND = Nothing
Set oHosts = Nothing
Set oFSO = Nothing
Of course that does not address a potential issue of appending data that is already in the file.
If you want to read the file first, open it for reading, read the data, close it, then re-open it for appending and make your changes. There is no need to open it for writing.
If you want to edit the file, read it in, close it, reopen it for writing, and write out the edited data.
C:\Windows\System32\drivers\etc is a directory.
here is the bat file inc case you need
type "%windir%\system32\drivers\etc\hosts" | find /i "WEBSITE1" || echo 10.0.0.0 WEBSITE1 >> "%windir%\system32\drivers\etc\hosts"
type "%windir%\system32\drivers\etc\hosts" | find /i "SERVER1" || echo 10.0.0.0 SERVER1 >> "%windir%\system32\drivers\etc\hosts"
I'm trying to create a .zip file from an existing folder using JScript and it seems that my copyHere function is not copying to the .zip folder. Instead I get a popup box titled 'Compressed (zipped) Folder Error' with the message 'file not found or no read permissions' even though I have read/write privileges on the file according to the value of my file.attributes property (32).
Here is the script I'm using:
//Get commman line arguments
var objArgs = WScript.Arguments;
var zipPath = objArgs(0);
var sourcePath = objArgs(1);
//Create empty ZIP file and open for adding
var fso = new ActiveXObject("Scripting.FileSystemObject");
var file = fso.CreateTextFile(zipPath, true);
// Create twenty-two byte "fingerprint" for .zip
file.write("PK");
file.write(String.fromCharCode(5));
file.write(String.fromCharCode(6));
file.write('\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0');
var objShell = new ActiveXObject("shell.application");
var zipFolder = new Object;
zipFolder = objShell.NameSpace(zipPath);
sourceItems = objShell.NameSpace(sourcePath).items();
if (zipFolder != null)
{
zipFolder.CopyHere(sourceItems);
WScript.Sleep(1000);
}
Now the CopyHere function works for copying the contents of the sourcePath to a normal folder but when I try to create a .zip file and copy the contents to that, nothing happens. Any ideas on why copyHere is not copying the contents of the sourcePath to the .zip?
An Example for calling this script would be:
cscript win-zip.js C:\desired\zip\file.zip C:\path\to\source\folder
And the desired outcome would be that file.zip was created and now contains the contents of the source folder. Could this be a problem with permissions? What might cause this behavior?
Side Note, using a vbScript and the same commands I can successfully create and populate a .zip, so why doesn't it work using jscript!
Set objArgs = WScript.Arguments
ZipFile = objArgs(0)
SourceFolder = objArgs(1)
' Create empty ZIP file and open for adding
CreateObject("Scripting.FileSystemObject").CreateTextFile(ZipFile, True).Write "PK" & Chr(5) & Chr(6) & String(18, vbNullChar)
Set zip = CreateObject("Shell.Application").NameSpace(ZipFile)
' Get items in source folder
Set sourceItems = CreateObject("Shell.Application").NameSpace(SourceFolder).Items
' Add all files/directories to the .zip file
zip.CopyHere(sourceItems)
WScript.Sleep 1000 'Wait for items to be copied
Any helpful comments are greatly appreciated, thanks!
I encountered the same problem (file not found or no read permissions' even though I have read/write privileges on the file according to the value of my file.attributes property).
The problem disapeared as soon as I found and suppress a 0 length file somewhere in the directory to be copied with the copyhere method.
As Raymond said, the problem was that I had an open reference to the .zip folder in the file variable that I created (which had a lock on the folder so I could not copy any contents to it). The solution is to call
file.Close();
after writing to the file, that way we can access the file to copy contents to it :)