如何在每个包含jpg文件的目录和子目录中创建文件夹?

时间:2019-05-16 21:50:23

标签: c# for-loop directory

我需要能够在主路径的每个目录和子目录中创建一个文件夹

但是我不知道如何获取包含文件的每个路径,因此我可以在其中创建一个文件夹...实际上,我正在获取所有执行此操作的目录:

    using System;
    using System.IO;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using ImageMagick;
    using System.Collections;

    namespace ImgOptimization
    {
        class Program
        {
            static void Main(string[] args)
            {


                string path = "mypath";



                if (System.IO.Directory.Exists(path))
                {

                    string[] subdirectorios = System.IO.Directory.GetFiles(path, "*.jpg", SearchOption.AllDirectories);

                    string carpetas = null;



                    for (int i = 0; i < subdirectorios.Length; i++)
                    {

                        carpetas = subdirectorios[i];


                            string newpathMobile = "newpath"


                            if (!System.IO.Directory.Exists(newpath))
                            {
                                System.IO.Directory.CreateDirectory(newpath);
                            }

                    }
                }
}
}

但是我不知道如何在循环期间在每个路径中创建一个文件夹,是否知道如何实现?

2 个答案:

答案 0 :(得分:0)

您可以像这样递归地浏览所有子目录

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;

namespace TestSubDirectories
{
    class Program
    {

        static string NewFolderName = "NewFolder";
        static void Main(string[] args)
        {

            string path = "C:\\testfolder";

            if (Directory.Exists(path))
                CreateSubFolderIn(path);
        }


        static void CreateSubFolderIn(string path)
        {
            foreach (string SubDirectory in Directory.GetDirectories(path))
            {
                CreateSubFolderIn(SubDirectory);
            }

            if(!Directory.Exists(path + "\\" + NewFolderName))
                Directory.CreateDirectory(path + "\\" + NewFolderName);

        }

    }
}

答案 1 :(得分:0)

一种非常简单的方法是使用<?php echo " <h3 class='text-center'>Table Name</h3> <table class='table table-bordered'> <tr> <th>#</th> <th>Title</th> <th>Score</th> </tr> <tbody class='row_position'>"?> <?php require('db_config.php'); $tablename = $_SESSION['username'] . "_laliga"; $_SESSION['tablename'] = $tablename; $sql = "SELECT * FROM $tablename ORDER BY position_order"; $users = $mysqli->query($sql); while($user = $users->fetch_assoc()){ $sql1 = "SELECT `position_order` FROM '".$tablename."' WHERE `title` LIKE '".$user['title']."' "; $sql2 = "SELECT `position_order` FROM `laliga` WHERE `title` LIKE '".$user['title']."' "; $position1 = $mysqli->query($sql1); $position2 = $mysqli->query($sql2); ?> <tr id="<?php echo $user['id'] ?>"> <td><?php echo $user['position_order'] ?></td> <td><?php echo $user['title'] ?></td> <td><?php echo abs($position1-$position2); ?></td> </tr> <?php } ?> </tbody> </table> 类,该类返回强类型的DirectoryInfo对象(与仅代表目录路径的字符串相反)。

我们可以对目录包含指定扩展名的文件的结果进行过滤,然后可以向该目录添加子目录:

DirectoryInfo

因此,如果我们想在根public static void CreateDirectoryNextToFileType(string rootPath, string fileExtension, string newDirectoryName) { if (rootPath == null || !Directory.Exists(rootPath)) throw new ArgumentException("rootPath must be the path to an existing directory"); if (fileExtension == null) throw new ArgumentNullException(nameof(fileExtension)); if (string.IsNullOrEmpty(subDirectoryName) || subDirectoryName.Any(chr => Path.GetInvalidPathChars().Contains(chr))) throw new ArgumentException("subDirectoryName is null, empty, or " + "contains invalid characters"); // Enumerate all directories and return those that // contain a file with the specified extension var directoriesContainingFile = new DirectoryInfo(rootPath) .EnumerateDirectories("*", SearchOption.AllDirectories) .Where(dir => dir.EnumerateFiles().Any(file => file.Extension.Equals(fileExtension, StringComparison.OrdinalIgnoreCase))); // For each of the directories, create a sub directory with the specified name foreach (var directory in directoriesContainingFile) { directory.CreateSubdirectory(newDirectoryName); } } 下任何位置包含"NewDirNextToCmdFile"文件的任何目录中创建一个名为".cmd"的子目录,则可以这样调用该方法:

"f:\private\temp"