Copy folder contents to a created .zip file: 'file not found or no read permissions' - windows

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 :)

Related

how to prevent cached response from MSXML2.XMLHTTP, vbscript

I'm writing a wscript with vbscript in Windows 7.
The script needs to grab a file from an open ftp server, so I must use MSXML2.XMLHTTP object
The file I am grabbing is being cached in a sub folder of
C:\Users\user\AppData\Local\Microsoft\Windows\Temporary Internet Files\Content.IE5
The file that is cached has no "expires" value, and trying to set a value using setRequestHeader method doesn't seem to have any effect.
I will self-answer this in case anyone else needs this solution
this code snippet shows how to delete the cached files from the folder used by MSXML2.XMLHTTP, using the concept of the downloaded files will have the extension ".csv"
<job>
<script language="vbscript">
' use XMLHTTP to grab content from a URL
CMEURL = "ftp://ftp.cmegroup.com/pub/settle/nymex_future.csv"
' the downloaded files are being stored in a subfolder of this folder
filedownloadcachelocation = "C:\Users\user\AppData\Local\Microsoft\Windows\Temporary Internet Files\Content.IE5"
' delete the file before requesting a download
Dim fso
Set fso = CreateObject("Scripting.FileSystemObject")
Dim fileresults
Dim Directory
Set Directory = fso.GetFolder(filedownloadcachelocation)
Dim subfolders
Set subfolders = Directory.SubFolders
For Each Folder In subfolders
For Each FileName In Folder.Files
' only react if filename extension is csv
If InStr(FileName , ".csv") Then
fileresults = fileresults & FileName.Path & vbLf
' delete the file
If fso.FileExists(FileName.Path) Then
'WScript.Echo "deleting file " & FileName.Path
fso.DeleteFile FileName.Path
End If
End If ' csv ext
Next ' each file in folder
Next ' each folder in Content.IE5
WScript.Echo "files found that were deleted = " & VbLf & fileresults
' now continue with downloading the file using XMLHTTP

open and save a xltm file

I've used some code in VBScript to open a .xlsm file and save that file. Now I want to do the same thing as a .xltm file. I've tried to open the xltm file with script, it works fine. While saving that file, it refers the default location and default extension. I need to save the newly opened file with the extension ".xlsm" in the specified location. I don't know how to proceed. Please help me to solve this.
Set objExcel = CreateObject("Excel.Application")
Set WBTestFile = objExcel.Workbooks.Open(WScript.Arguments(0))'SourceFile
WBTestFile.save
WBTestFile.close
objExcel.Workbooks.Open(WScript.Arguments(0))
Here, I am passing the filename (with path) as an argument. I need to open the newly saved ".xlsm" file in the last statement. Argument: "c:\test\book1.xltm", my newly created file want to be saved in the location "C:\test\" with the extension "xlsm".
The Save method saves the file as-is. To save it in a different format you need to use the SaveAs method with the correct file format argument (in this case a macro-enabled Open XML template):
filename = WScript.Arguments(0)
templatename = Replace(filename, ".xlsm", ".xltm")
Set xl = CreateObject("Excel.Application")
Set wb = xl.Workbooks.Open(filename)
wb.SaveAs templatename, 53 'save as template
wb.Close
xl.Quit
To create a new workbook from an existing template you need to use the Add method with the path to the template as argument and then save the file as a macro-enabled Open XML workbook:
template = WScript.Arguments(0)
filename = Replace(template, ".xltm", ".xlsm")
Set xl = CreateObject("Excel.Application")
Set wb = xl.Workbooks.Add(template)
wb.SaveAs filename, 52 'save as workbook
wb.Close
xl.Quit

Visual basic extract folder names and compare them with active directory users

im a newbie in vb script writing and I want to extract folder names and compare them with active directory users I started with the following code:
Set fs = CreateObject("Scripting.FileSystemObject")
'Log file name
Set logFile = fs.OpenTextFile("fileNameLogs.txt", 2, True)
'Directory you want listed
Set folder = fs.GetFolder("\\server\Data\Users")
Set files = folder.files
For Each file in files
logFile.writeline(file.name)
Next
logFile.close
This script only extracts the file names not the folder names.
Can any one help me continue and extract the folder names instead of the file names so I can compare them active directory.
You are on the right track just use the .SubFolders object collection to iterate through the Folder objects in the current Folder object similar to the approach you have used for the File objects.
Option Explicit
Dim fs, logFile
Dim folder, subFolders, subFolder
Dim files, file
Set fs = CreateObject("Scripting.FileSystemObject")
'Log file name
Set logFile = fs.OpenTextFile("fileNameLogs.txt", 2, True)
'Directory you want listed
Set folder = fs.GetFolder("\\server\Data\Users")
'Write out sub folders
Set subFolders = folder.SubFolders
For Each subFolder In subFolders
logFile.Writeline(subFolder.Name)
Next
Set subFolders = Nothing
'Write out files
Set files = folder.files
For Each file In files
logFile.Writeline(file.Name)
Next
Set files = Nothing
logFile.Close
'Clean-up and reclaim memory
Set logFile = Nothing
Set folder = Nothing
Set fs = Nothing

List sub directories of a folder in APPDATA using Windows JScript

I'm trying to edit a json file which is located inside a folder in AppData\Roaming.
The file path is AppData\Roaming\Myapp\RANDOM_CRAP\settings.json
RANDOM_CRAP is just a random folder name which is different for every machine.
In order to open this file for writing, I first tried to get it's file path, like so:
function getAppData() {
var oShell = new ActiveXObject("WScript.Shell");
var strValue = oShell.RegRead("HKEY_CURRENT_USER\\Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\User Shell Folders\\AppData");
return strValue;
}
Problem is, the value stored under that registry key is %USERPROFILE%\AppData\Roaming which doesn't seem to open with:
var folder = fso.GetFolder(getAppData());
(Throws Path not found error)
Can I get to the APPDATA path in another way?
Getting to AppData path is easy with the ExpandEnvironmentStrings Method.
Party time:
var WshShell = WScript.CreateObject("WScript.Shell");
WScript.Echo("WinDir is " + WshShell.ExpandEnvironmentStrings("%AppData%"));
Good luck.

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

Resources