我有一个我的文件夹结构预先设置的统一包,但是意味着是空的,为了导出文件夹我把文件“Delete This File.txt”放到结构中的每个底部文件夹中以允许统一到导出文件夹,但我想写一个在启动时运行的脚本删除这些文件,但我似乎无法让脚本找到各个文件夹中的所有文件。我该怎么做?
using UnityEngine;
using UnityEditor;
using System.IO;
[InitializeOnLoad]
public class Startup {
static Startup()
{
foreach (string file in Directory.GetFiles("Assets/")) {
if (file == "Delete This File.txt") {
Debug.Log (file);
} else {
// Do Nothing
}
}
}
}
答案 0 :(得分:1)
您的代码中需要进行一些更改
[InitializeOnLoad]
public class Startup {
static Startup()
{
// unfortunately this is not available in NET 3.5
// foreach (string file in Directory.EnumerateFiles.....
foreach (string file in Directory.GetFiles("Assets", "Delete This File.txt", SearchOption.AllDirectories))
{
Debug.Log (file);
// File.Delete(file);
}
}
此方法使用Directory.GetFiles
的重载,该重载采用初始目录,要搜索的模式以及搜索根的所有子目录的选项。生成的字符串是要删除的完整文件名。
此外,在.NET 4.0中,Directory.EnumerateFiles有一个更好的选项,如the MSDN docs所述
EnumerateFiles和GetFiles方法的区别如下:当你 使用EnumerateFiles,您可以开始枚举名称集合 在整个收藏品归还之前;当你使用GetFiles时,你 必须等待返回整个名称数组才能返回 访问数组。因此,当您使用许多文件和 目录,EnumerateFiles可以更有效。
答案 1 :(得分:0)
这可以通过Directory.GetFiles
,Directory.GetDirectories
和FileInfo
类的组合来完成。我修改了Microsoft的RecursiveFileProcessor代码example并添加了FileInfo
和if
语句,以便找到所有名为" Delete This File.txt
"的文件。并删除它们。
using UnityEngine;
using UnityEditor;
using System.IO;
[InitializeOnLoad]
public class Startup
{
static Startup()
{
ProcessDirectory("Assets/");
}
public static void ProcessDirectory(string targetDirectory)
{
// Process the list of files found in the directory.
string[] fileEntries = Directory.GetFiles(targetDirectory);
foreach (string filePath in fileEntries)
{
FileInfo fileInfo = new FileInfo(filePath);
if (fileInfo.Name == "Delete This File.txt")
{
ProcessFile(filePath);
}
}
// Recurse into subdirectories of this directory.
string[] subdirectoryEntries = Directory.GetDirectories(targetDirectory);
foreach (string subdirectory in subdirectoryEntries)
{
ProcessDirectory(subdirectory);
}
}
// Insert logic for processing found files here.
public static void ProcessFile(string path)
{
Debug.Log("Processed file " + path);
//Delete File
File.Delete(path);
}
}
答案 2 :(得分:0)
After discovering that Directory.EnumerateFiles wasn't an option for me I tried the same method with the get files, and found a solution that works. This code not only deletes the text file but the meta file that is associated with it, counting the number of times a text file was deleted and informing me how many were deleted in the log.
using UnityEngine;
using UnityEditor;
using System.IO;
/* Project Name : Ember Guard
* Script Name : RemoveDeleteFile
* Script Path : Assets/Editor/RemoveDeleteFile.cs
* Script Author : FaalFaazDov || Raisltin M. Thoreson
* Created On : 29/08/2016 14:24
* Modified On : N/A
* Version : 0.0.1Alpha
*/
/*************************************************************************
*
* Biophase Entertainment
* __________________
*
* [2016] Biophase Entertainment
* All Rights Reserved.
*
* NOTICE: All information contained herein is, and remains
* the property of Biophase Entertainment and its suppliers,
* if any. The intellectual and technical concepts contained
* herein are proprietary to Biophase Entertainment
* and its suppliers and may be covered by Canadian and Foreign Patents,
* patents in process, and are protected by trade secret or copyright law.
* Dissemination of this information, reproduction of this material or attempting
* to read the content of these files is strictly forbidden unless prior written
* permission is obtained from Biophase Entertainment.
*
*/
[InitializeOnLoad]
public class Startup {
static Startup()
{
int x = 0;
string path = Application.dataPath;
string[] files = Directory.GetFiles (path, "Delete This File.txt", SearchOption.AllDirectories);
foreach (string f in files) {
File.Delete (f);
File.Delete (f + ".meta");
x++;
}
string fm;
if (x == 1) {
fm = "file";
} else {
fm = "files";
}
Debug.Log ("Deleted " + x + " " + fm + ".");
}
}