I am writing a Lua function to delete a folder using Luacom in Windows (version 7 onwards and I can't dictate the version). The folder path is specified in UTF-8 and will contain non-ASCII characters, so os.remove, io.whatever, Penlight and lfs will not work. So far I have (using Luacom to access the Windows com model):
function delFolder(sPath, bForce)
--sPath is a fully specified folder path
--bForce is a Boolean indicating whether the folder should be
--deleted even if it contains read-only files
require('luacom')
fso = luacom.CreateObject("Scripting.FileSystemObject")
--code in here to test that the folder exists,
--and return an error if it does not
fso:DeleteFolder(sPath, bForce)
end
My problem is that, if bForce = false, and the folder is effectively read-only, the operation errors out. I need to be able to test for this situation and return an error instead of attempting the operation.
One possibility is to manipulate the Luacom error handling not to abort on error, and test the last error after the operation:
luacom.config.abort_on_error = false
luacom.config.last_error = nil
fso:DeleteFolder(sPath, bForce)
if luacom.config.last_error then
--return error indicating that the folder cannot be deleted
end
but is there a simpler way, using the com model or some other alternative available in Lua?
Reference for FileSystemObject
Related
This code failed because the ../data/housing/ directory didn't exist:
function FetchHousingData(url::String, path::String)
println("FetchHousingData " * Dates.format(now(), "HH:MM:SS") )
HTTP.download(url, path)
println("done " * Dates.format(now(), "HH:MM:SS") )
end
housingUrl = "https://raw.githubusercontent.com/ageron/handson-ml2/master/datasets/housing/housing.tgz"
housingPathFile = "../data/housing/housing.tgz"
FetchHousingData(housingUrl, housingPathFile)
Is there a way to have HTTP.download() create a missing directory? I haven't been able to find the docs for HTTP.download.
You can get the docs from the REPL as usual by entering help mode (press ? at the REPL prompt):
help?> HTTP.download
download(url, [local_path], [headers]; update_period=1, kw...)
Similar to Base.download this downloads a file, returning the filename. If the local_path:
• is not provided, then it is saved in a temporary directory
• if part to a directory is provided then it is saved into that directory
• otherwise the local path is uses as the filename to save to.
When saving into a directory, the filename is determined (where possible), from the rules of the HTTP.
• update_period controls how often (in seconds) to report the progress.
• set to Inf to disable reporting
• headers specifies headers to be used for the HTTP GET request
• any additional keyword args (kw...) are passed on to the HTTP request.
There doesn't seem to be a kwarg in HTTP.download to create directories, but you might be interested in isdir:
help?> isdir
isdir(path) -> Bool
Return true if path is a directory, false otherwise.
Examples
≡≡≡≡≡≡≡≡≡≡
julia> isdir(homedir())
true
julia> isdir("not/a/directory")
false
See also isfile and ispath.
and mkdir:
help?> mkdir
mkdir(path::AbstractString; mode::Unsigned = 0o777)
Make a new directory with name path and permissions mode. mode defaults to 0o777, modified by the current file creation mask. This function never creates more than one directory. If the directory already exists, or some intermediate
directories do not exist, this function throws an error. See mkpath for a function which creates all required intermediate directories. Return path.
Examples
≡≡≡≡≡≡≡≡≡≡
julia> mkdir("testingdir")
"testingdir"
julia> cd("testingdir")
julia> pwd()
"/home/JuliaUser/testingdir"
I prefer to use mkpath (also mentioned in the docs showed by Nils) as it creates parent directories and does not produce an error if the directory already exists:
housingPathFile = "../data/housing/housing.tgz"
mkpath(dirname(housingPathFile))
Command Prompt its not working since i am using a generic path to open a excel file. Here is the error message:
T:\PointOfSale\Projects\Automated Testing\TASWeb\TP\TP_Branch>ruby -rubygems Tes
tTP_UK.rb
TestTP_UK.rb:19:in 'method_missing': (in OLE method `Open': )(WIN32OLERuntimeEr
ror)
OLE error code:800A03EC in Microsoft Excel
'./../../../MasterFile.xls' could not be found. Check the spelling of the
file name, and verify that the file location is correct.
If you are trying to open the file from your list of most recently used files, m
ake sure that the file has not been renamed, moved, or deleted.
HRESULT error code:0x80020009
Exception occurred.
from TestTP_UK.rb:19:in `'
enter code here'
Generic path code
excel = WIN32OLE::new("excel.Application")
path = "#{File.dirname(__FILE__)}/../../../MasterFile.xls"
workbook = excel.Workbooks.Open(path)
worksheet = workbook.WorkSheets(1) # Get first workbook
site = worksheet.Range('A2').Value # Get the value at cell in worksheet.
workbook.Close
excel.Quit
Any Ideas
I believe you need to use an absolute path rather than a relative path when opening the file:
path = File.expand_path("../../../../MasterFile.xls", __FILE__)
Note that you will also need an additional '..' when using expand_path, since the first '..' is going back from the file.
I have written following code in vb.net to delete the file.
If File.Exists(strPath & "\ReportEng.ini") = True Then
File.SetAttributes(strPath & "\ReportEng.ini", FileAttributes.Normal)
File.Delete(strPath & "\ReportEng.ini")
End If
File.Copy("\\192.168.0.1\SAP_Shared\AddonExtra\ReportEng.ini", strPath & "\ReportEng.ini")
This code works perfectly in windows xp. But in Windows 7,I can not delete it. This OS is hectic OS from developer's point of view. Some or other problem occurs and Microsoft has not considered the developer while building this OS.
How do I delete file in Windows 7 ?
It's so easy to do so;
If My.Computer.FileSystem.FileExists("C:\somefile.ext") Then 'Check whether file exists
My.Computer.FileSystem.DeleteFile("C:\somefile.ext") 'Delete the file!
End If
Have a nice day!
You don't need to delete the file: there is an overload File.Copy Method (String, String, Boolean) which allows overwriting.
You didn't say what error you get. I suspect it is because the user doesn't have write access to the directory. You should probably be using a subdirectory of the directory returned by Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) or maybe .LocalApplicationData, and definitely not the directory containing the program.
Also, using Path.Combine(strPath, "ReportEng.ini") is how you're meant to combine paths - it'll take care of, e.g., the trailing path separator for you.
The preferred method for interfacing with the Windows file system uses the following namespace:
Imports Microsoft.VisualBasic.FileIO.FileSystem
To delete a file:
Dim FileLocation As String = strPath & "\ReportEng.ini"
If Not GetDirectoryInfo(FileLocation).Exists Then
GetFileInfo(FileLocation).Delete()
End If
This code works on Windows XP at home but fails at work on 64bit Windows 7. The loop isn't entered even once although there are more than 50 files in the supplied folder. Not only it doesn't enter, it also returns ERROR_NO_MORE_FILES for GetLastError. Why?
string dir = "d:\\validfolder";
WIN32_FIND_DATA ffd;
HANDLE h = FindFirstFile(dir.c_str(), &ffd);
while(FindNextFile(h, &ffd))
{
// some operation
}
DWORD dw = GetLastError();// returns ERROR_NO_MORE_FILES
I tried Wow64DisableWow64FsRedirection but that has no effect.
You need to add a file wildcard to your dir:
string dir = "d:\\validfolder\\*";
For it to list the files in a directory. Otherwise you are only asking for information about the directory itself.
At least that's how I read the documentation for FindFirstFile
To examine a directory that is not a root directory, use the path to
that directory, without a trailing backslash. For example, an argument
of "C:\Windows" returns information about the directory "C:\Windows",
not about a directory or file in "C:\Windows". To examine the files
and directories in "C:\Windows", use an lpFileName of "C:\Windows*".
I don't know why it's working for you on XP
This code is incorrect in a number of ways.
You must check the return value of FindFirstFile. If the call to FindFirstFile succeeds then you already have the first file in ffd. As your code stands, you throw away the first file. So you need to re-jig your loop logic to account for that. Naturally, if GetLastError returns ERROR_NO_MORE_FILES then that means the search has exhausted all files.
So, what is probably happening is you ask for the first file matching the search string "d:\\validfolder". This is returned in ffd after the call to FindFirstFile. You then ignore that information and ask for the next match. But there is no subsequent match since there is only one object matching "d:\\validfolder" since you included no wildcards in your search pattern.
This code will behave exactly the same on XP as it does on Windows 7 and I suspect that you are not running the same code on both systems.
If you want to enumerate the contents of the folder then you need to search for "d:\\validfolder\\*". Something like this:
string dir = "d:\\validfolder\\*";
WIN32_FIND_DATA ffd;
HANDLE h = FindFirstFile(dir.c_str(), &ffd);
BOOL success = h<>INVALID_HANDLE_VALUE;
while(success)
{
// do something with ffd
success = FindNextFile(h, &ffd));
}
I'm not understanding this behavior. Maybe someone can explain to me why my current working directory is not what I expect.
On my desktop, I have a folder called STKGui:
C:\Documents and Settings\Lauren\Desktop\STKGui
Located in that directory are the following files: gui.html, style.css, save.html, load.html Within STKGui there are also the following directories: Images, Scripts, and SaveData. Scripts contains various .vbs files, including gui.vbs.
I start with gui.html. I click a button which takes me to load.html. load.html uses scripts from Scripts\gui.vbs. One of the functions loads a database, and to do so I provide the location of the database: C:\Documents and Settings\Lauren\Desktop\STKGui\SaveData\SaveData.accdb Of course I want to use a relative file path instead of a fixed path. My initial attempt to load the database failed; it was trying to load from C:\Documents and Settings\Lauren\Desktop\SaveData\SaveData.accdb. So to troubleshoot I printed out the current working directory; much to my chagrin it was C:\Documents and Settings\Lauren\Desktop
I don't understand why my desktop is my current working directory. Shouldn't it be where the file is running from? I figured it would be either C:\Documents and Settings\Lauren\Desktop\STKGui (the location of load.html) OR C:\Documents and Settings\Lauren\Desktop\STKGui\Scripts (the location of gui.vbs which contains the function that's trying to load the database/printing debug messages of the current working directory).
Can someone explain why the current working directory is what it is, or better yet tell me how to get what I really want, which is the location of the files executing? (I don't care if it's the main STKGui folder or the scripts folder--as long as it's within the application's directory structure I can work with it!)
EDIT (7/14/10 4:02 pm EDT):
Various attempts at printing the current working directory or grabbing files based on what I -thought- was the relative path from my executing script have resulted in my desktop's path instead of the path of the executed script. I stumbled across this link: http://leereid.wordpress.com/2008/03/19/vbscript-current-directory-or-folder/ but none of the solutions are working for me, as I get run-time errors regarding the Wscript object. So while I don't know if any of the solutions on the aforementioned link will produce different results, if someone can help me get at least one of them working so I can find out that may be a step in the right direction.
One of the solutions, reproduced below:
Set oShell = CreateObject("WScript.Shell")
Set ofso = CreateObject("Scripting.FileSystemObject")
oShell.CurrentDirectory = ofso.GetParentFolderName(Wscript.ScriptFullName)
produces the following error:
Object required: 'Wscript' line: 659 char: 1
with line 659 being:
oShell.CurrentDirectory = ofso.GetParentFolderName(Wscript.ScriptFullName)
For Server-Side:
You should be using Server.MapPath() to get your "working directory". For instance, if you want to get the path to your database file in C:\Documents and Settings\Lauren\Desktop\STKGui\SaveData\SaveData.accdb, your app root being C:\Documents and Settings\Lauren\Desktop\STKGui, you would use Server.MapPath("SaveData\SaveData.accdb").
For Client-Side:
Upon closer examination and digging up some memories, I realized that MapPath is only available from the Server class. Instead, you need to create a file system object like this:
''get fs object
Set objFSO = CreateObject("Scripting.FileSystemObject")
''get actual file using path relative to calling vbs file
Set objFile = objFSO.GetFile("SaveData\SaveData.accdb")
''get path to the database
set sPathToDatabase = objFSO.GetAbsolutePathName(objFile)
In case it helps, here is a great resource for working with the file system in vbScript: http://www.activexperts.com/activmonitor/windowsmanagement/adminscripts/filesfolders/files/
This solution was NOT ideal, but what I ended up doing was parsing the url in my browser to get the directory.
guiPath = Mid(location.PathName, 2, len(location.PathName))
Set regExp = New RegExp
regExp.IgnoreCase = False
regExp.Global = True
regExp.Pattern = ".*/"
Set matchCollection = regExp.Execute(guiPath)
Set match = matchCollection(0)
guiPath = match.value
regExp.Pattern = "%20"
guiPath = regExp.Replace(guiPath, " ")
systemsDBPath = guiPath & "SaveData\SaveData.accdb"
Like I said, less than ideal. May not even work once I'm working with the application this will be running in. But I couldn't find a better way.