copy file |
Author: |
Message: |
whiz
Senior Member
Posts: 568 Reputation: 8
– / – /
Joined: Nov 2008
|
RE: RE: copy file
quote: Originally posted by matty
I did a little testing and noticed a few things.
- Firstly FindFirstFileW and FindNextFileW will find . and .. as well
- I forgot to append the path with slashes
js code: RemoveDirectory('C:\\Documents and Settings\\__________\\My Documents\\New Folder', true);
function RemoveDirectory(sPath, bFirstFolder) {
sPath='\\\\?\\'+sPath;
var WIN32_FIND_DATA = Interop.Allocate(592);
var hSearch = Interop.Call('kernel32', 'FindFirstFileW', sPath+'\\*', WIN32_FIND_DATA);
var hResult = -1;
var sFile;
while(hResult != 0){
sFile = WIN32_FIND_DATA.ReadString(44);
if (sFile !== '.' && sFile !== '..') {
if(!(WIN32_FIND_DATA.ReadDWORD(0) & 0x10 /* FILE_ATTRIBUTE_DIRECTORY */)){
Debug.Trace(sPath+sFile);
Interop.Call('kernel32', 'DeleteFileW', sPath+'\\'+sFile);
} else {
Debug.Trace(sPath+sFile);
>>> RemoveDirectory(sPath+'\\'+sFile false); <<<
if ( Interop.Call('kernel32', 'RemoveDirectoryW', sPath+'\\'+sFile) === 0 ) TraceWin32Error()
}
}
hResult = Interop.Call('kernel32', 'FindNextFileW', hSearch, WIN32_FIND_DATA)
}
Interop.Call('kernel32', 'FindClose', hSearch);
if (bFirstFolder === true) Interop.Call('kernel32', 'RemoveDirectoryW', sPath)
}
function TraceWin32Error(){
var LastError = Interop.GetLastError();
if(LastError != 0) {
var MsgBuffer = Interop.Allocate(1024);
Interop.Call("Kernel32", "FormatMessageW", 0x1000, 0, LastError, 0, MsgBuffer, 1024, 0);
Debug.Trace(MsgBuffer.ReadString(0));
}
}
No idea if this will work I would have to do a bit more troubleshooting as to why it doesn't.
The highlighted line... should it be like this (with a comma)?
js code: RemoveDirectory(sPath+'\\'+sFile, false);
Even then...
quote: Originally posted by JavaScript Debugger
code: \\?\C:\Documents and Settings\__________\My Documents\My Downloads\Test\
The debug line worked, although I don't know what's with the "\\?\" bit.
js code: Debug.Trace(sPath+sFile);
However, nothing in the folder was deleted. The folder remained, and so did the files inside. Although... the "TraceWin32Error()" wasn't called. I suppose that's a good thing.
|
|
07-15-2009 06:31 PM |
|
|
matty
Scripting Guru
Posts: 8336 Reputation: 109
39 / /
Joined: Dec 2002
Status: Away
|
RE: copy file
Take a look at this: http://msdn.microsoft.com/en-us/library/aa365488%28VS.85%29.aspx
Anyways it looks like I completly screwed it up that time. I will have another stab at it.
js code: RemoveDirectory('C:\\Documents and Settings\\User\\My Documents\\Test');
function RemoveDirectory(sPath) {
var FO_DELETE = 0x3
var FOF_ALLOWUNDO = 0x40;
var SHFILEOPSTRUCT = Interop.Allocate(30);
var strPath = Interop.Allocate(sPath.length*2+2);
strPath.WriteString(0, sPath);
with (SHFILEOPSTRUCT) {
WriteDWORD(0, 0);
WriteDWORD(4, FO_DELETE);
WriteDWORD(8, strPath.DataPtr);
//WriteWORD(16, FOF_ALLOWUNDO); // If you want the files to be sent to the recycle bin uncomment the following
}
if ( Interop.Call('shell32', 'SHFileOperationW', SHFILEOPSTRUCT) > 0 ) TraceWin32Error();
}
function TraceWin32Error(){
var LastError = Interop.GetLastError();
if(LastError != 0) {
var MsgBuffer = Interop.Allocate(1024);
Interop.Call("Kernel32", "FormatMessageW", 0x1000, 0, LastError, 0, MsgBuffer, 1024, 0);
Debug.Trace(MsgBuffer.ReadString(0));
}
}
This post was edited on 07-15-2009 at 07:58 PM by matty.
|
|
07-15-2009 07:57 PM |
|
|
whiz
Senior Member
Posts: 568 Reputation: 8
– / – /
Joined: Nov 2008
|
RE: copy file
quote: Originally posted by matty
js code: function RemoveDirectory(sPath) {
var FO_DELETE = 0x3
var FOF_ALLOWUNDO = 0x40;
var SHFILEOPSTRUCT = Interop.Allocate(30);
var strPath = Interop.Allocate(sPath.length*2+2);
strPath.WriteString(0, sPath);
with (SHFILEOPSTRUCT) {
WriteDWORD(0, 0);
WriteDWORD(4, FO_DELETE);
WriteDWORD(8, strPath.DataPtr);
//WriteWORD(16, FOF_ALLOWUNDO); // If you want the files to be sent to the recycle bin uncomment the following
}
if ( Interop.Call('shell32', 'SHFileOperationW', SHFILEOPSTRUCT) > 0 ) TraceWin32Error();
}
function TraceWin32Error(){
var LastError = Interop.GetLastError();
if(LastError != 0) {
var MsgBuffer = Interop.Allocate(1024);
Interop.Call("Kernel32", "FormatMessageW", 0x1000, 0, LastError, 0, MsgBuffer, 1024, 0);
Debug.Trace(MsgBuffer.ReadString(0));
}
}
The folder and files remain, along with two errors. One in the debugger...
quote: Originally posted by JavaScript Debugger
code: The handle is invalid.
...and a Windows alert (title: "Error Deleting File or Folder", message: "Cannot delete file: Cannot read from the source file or disk.").
EDIT: No... you don't need trailing slashes. Let me try again.
EDIT 2: It works! And it has a Windows confirm dialog box. Thank you!
This post was edited on 08-10-2009 at 07:32 PM by whiz.
|
|
07-15-2009 08:14 PM |
|
|
CookieRevised
Elite Member
Posts: 15517 Reputation: 173
– / /
Joined: Jul 2003
Status: Away
|
RE: copy file
quote: Originally posted by whiz
although I don't know what's with the "\\?\" bit.
In a very simple nutshell, that is a special indication, prefix if you will, to tell Windows that a (very) long path name will follow.
More and proper details can be found on the MSDN library.
Also see the very first link in that article about the RemoveDirectory API, which Matty pointed to earlier:quote: In the ANSI version of this function, the name is limited to MAX_PATH characters. To extend this limit to 32,767 wide characters, call the Unicode version of the function and prepend "\\?\" to the path. For more information, see Naming a File.
Anyways, the point is that it is not wrong. In fact, it is mandatory to be there for many Unicode APIs if you don't want the run into the default path length limitation (which is normally just 260 characters).
This post was edited on 07-15-2009 at 11:37 PM by CookieRevised.
.-= A 'frrrrrrrituurrr' for Wacky =-.
|
|
07-15-2009 11:23 PM |
|
|
Pages: (3):
« First
«
1
2
[ 3 ]
Last »
|
|
|