我有这个代码来递归删除文件和目录.它工作正常,但有一点问题.如果$ path =/var/www/foo /它会删除foo中的所有内容,但不会删除foo.我也想删除foo目录.任何的想法?
public function delete($path) {
if(!file_exists($path)) {
throw new RecursiveDirectoryException('Directory doesn\'t exist.');
}
$directoryIterator = new DirectoryIterator($path);
foreach($directoryIterator as $fileInfo) {
$filePath = $fileInfo->getPathname();
if(!$fileInfo->isDot()) {
if($fileInfo->isFile()) {
unlink($filePath);
}
else if($fileInfo->isDir()) {
if($this->emptyDirectory($filePath)) {
rmdir($filePath);
}
else {
$this->delete($filePath);
rmdir($filePath);
}
}
}
}
}
Run Code Online (Sandbox Code Playgroud) 尝试添加使用FTP以及该文件夹中包含的所有子文件夹和文件删除文件夹的功能。
我已经建立了一个递归函数来做到这一点,我觉得逻辑是正确的,但仍然行不通。
我做了一些测试,如果路径只是一个空文件夹或一个文件,我可以在第一次运行时删除,但是如果它是一个包含一个文件的文件夹或一个包含一个空子文件夹的文件夹,则无法删除。因此遍历文件夹并使用该函数删除似乎是一个问题。
有任何想法吗?
function ftpDelete($directory)
{
if(empty($directory))//Validate that a directory was sent, otherwise will delete ALL files/folders
return json_encode(false);
else{
global $conn_id;
# here we attempt to delete the file/directory
if( !(@ftp_rmdir($conn_id,$directory) || @ftp_delete($conn_id,$directory)) )
{
# if the attempt to delete fails, get the file listing
$filelist = @ftp_nlist($conn_id, $directory);
# loop through the file list and recursively delete the FILE in the list
foreach($filelist as $file)
ftpDelete($file);
#if the file list is empty, delete the DIRECTORY we …Run Code Online (Sandbox Code Playgroud) 如何删除指定目录下的所有子目录?
目录是c:/files/
我想删除其内的所有子目录(示例):
c:/files/something/something/something/
c:/files/another-something/
Run Code Online (Sandbox Code Playgroud)
所以最终 c:/files/ 仍然存在(为空且没有子目录)。
rmdir()只删除给定路径中的最后一个目录...所以我猜我必须循环?:/
感谢所有帮助。
(PS:子目录不包含任何文件)
如何使用单个命令删除多个目录?是否可以在一个班轮中进行?如果是,请帮忙.
/osmf/mgmt/scheduler>ls -lrt
total 22
drwx------ 2 root root 12288 Mar 26 2009 lost+found
drwxr-xr-x 4 ctmagent controlm 1024 May 24 2010 ctmagent
drwxrwxrwx 3 edwprod edw 1024 Dec 1 09:53 edi
drwxrwxrwx 120 edwprod edw 5120 Dec 27 09:37 edw
/osmf/mgmt/scheduler>
Run Code Online (Sandbox Code Playgroud)
我可以删除edi并edw使用单个命令吗?
我需要删除包含多个子目录和文件的目录.谷歌搜索和寻找的人的页面之后rmdir和rm我有两个选择:
$ rm -rf <folder_name> 和 $ \rm -rf <folder_name>
这两者有何不同?
我有一个运行Android 4.1.1的Verizon三星Galaxy Note 2,带有SuperSU.我有一个应用程序,FltPlanMobile已经损坏.我可以删除该应用程序,但其配置文件仍然落后,所以我也想删除它们.但出于某种原因,它不会消失.这是我正在尝试的内容(抱歉内联但是当我尝试创建代码块时,所有行都在一起运行):
C:\ Program Files\Android\android-sdk-windows\platform-tools> adb shell
shell @ android:/ $ su
苏
root @ android:/ #mount -o rw,remount/system
mount -o rw,remount/system
root @ android:/#cd/storage/sdcard0
cd/storage/sdcard0
root @ android:/ storage/sdcard0 #ls -l ls -l
drwxrwxr-x root sdcard_rw 2014-01-06 12:07 CallRecorder
drwxrwxr-x root sdcard_rw 2014-01-06 12:08 DCIM
drwxrwxr-x root sdcard_rw 2014-01-06 12:15 FltPlanMobile
...
root @ android:/ storage/sdcard0#rm -r FltPlanMobile
rm -r FltPlanMobile
rm对FltPlanMobile失败,权限被拒绝
255 | root @ android:/ storage/sdcard0#
我还尝试使用RootExplorer,FileExpert删除此目录,并直接从手机本身的终端仿真器窗口发出命令.什么都行不通.但是,如果我做mkdir foo,那么rm foo工作正常.
这里有别的奇怪 - 我可以做一个chmod 777 FltPlanMobile而不会出错.但是当我再次使用-l时,权限没有改变.
我没有得到它 …
我正在尝试将 Makefile 移植到 Windows(使用 GNU Make)。我在删除目录时遇到问题。我发现了关于如何有条件地删除目录的这个问题(使用命令行删除目录及其文件,但如果它不存在则不抛出错误),但是我在尝试使用该解决方案时遇到错误,大约转换为The use of ""doc\html"" is syntactically impermissible in this context
导致它的片段是这样的:
if exists "doc\html\" rmdir /Q /S doc\html
我也试过
cmd /c if exists "doc\html" cmd /c rmdir /Q /S doc\html
和
cmd /c if exists "doc\html\" cmd /c rmdir /Q /S doc\html.
我也试过了rmdir /Q /S doc\html 2>nul。这有点工作。错误被重定向,但 Make 仍然抛出错误并停止。
我怎样才能使这项工作?
我尝试在测试之前创建一些目录树,然后删除它:
require('should');
var fs = require('fs');
var rmdir = require('rmdir');
describe('FsWatcher', function() {
before(function(done) {
console.log("before");
fs.mkdir('tmp');
fs.mkdir('tmp/css');
fs.mkdir('tmp/js');
fs.mkdir('tmp/lib');
fs.open('tmp/index.htm', 'w');
// (...)
console.log("after_before");
done();
});
describe('single file', function() {
it('should succed', function(done) {
(1).should.equal(1);
done();
});
});
after(function() {
console.log("after");
rmdir('tmp', function() {
console.log("rmdir!");
});
console.log("after_end");
});
});
Run Code Online (Sandbox Code Playgroud)
console.logs 的顺序没问题:
mocha `find tests -name '*.js'`
before
after_before
?after
after_end
? 1 test complete (6 ms)
Run Code Online (Sandbox Code Playgroud)
但它不会调用rmdir().同样的rmdir在它结束时起作用before():
describe('FsWatcher', function() {
before(function(done) {
console.log("before");
fs.mkdir('tmp');
fs.mkdir('tmp/css');
fs.mkdir('tmp/js'); …Run Code Online (Sandbox Code Playgroud) 什么是删除目录的Perl方法,然后是所有空的 父目录,直到第一个非空目录?换句话说,可以使用什么而不是:
system 'rmdir', '-p', $directory;
Run Code Online (Sandbox Code Playgroud)
哪个,d首先删除d,然后c,然后b,但不是a,因为a仍然包含x,像这样:
a
a/b
a/b/c
a/b/c/d
a/x
Run Code Online (Sandbox Code Playgroud)
导致
a
a/x
Run Code Online (Sandbox Code Playgroud)
这不是内置的rmdir,因为它只能删除一个单一的目录.(doc)
它不是finddepth ( sub {rmdir}, '.' ),使用File::Find,因为它删除了孩子,而不是父母.(doc)
这也不是模块的remove_tree功能File::Path,因为它不仅删除子目录而且删除文件.(doc)
注意,remove_tree并finddepth在Bash的相反方向工作rmdir --parent.
rmdir ×10
php ×4
delete-file ×2
rm ×2
shell ×2
unix ×2
android ×1
bdd ×1
denied ×1
directory ×1
exists ×1
ftp ×1
if-statement ×1
javascript ×1
linux ×1
makefile ×1
mocha.js ×1
perl ×1
permissions ×1
recursion ×1
testing ×1
unlink ×1
while-loop ×1