简体   繁体   English

在每个文件夹中创建一个子文件夹然后将所有文件和文件夹移动到该子文件夹中的代码是什么?

[英]What is a code to create a sub-folder in each folder and then move all the files and folders into that sub-folder?

I am organizing all the documents for our customers and the structure should be like this: Customer Service\\Customer\\"Customer's Name"\\Quotation . 我正在为客户组织所有文档,其结构应如下所示: Customer Service\\Customer\\"Customer's Name"\\Quotation

So far I managed to create the "Quotation" folder in each customer's folder. 到目前为止,我设法在每个客户的文件夹中创建“报价”文件夹。

foreach ($folder in (Get-ChildItem 'C:\Users\S7051895\Desktop\Customer Service\Customer' -Directory)) {
    New-Item -ItemType Directory -Path ($folder.FullName + "\Quotes")
}

I have created the Quotes sub-folder in all the customer's folders. 我已经在所有客户的文件夹中创建了Quotes子文件夹。 Now, I need to move all the files and folders from the customer's folder into its Quotes sub-folder. 现在,我需要将所有文件和文件夹从客户的文件夹移到Quotes子文件夹中。 I would like to do it within one script ideally. 我想理想地在一个脚本中完成它。 Though, anything will be great as there are like hundreds of customers. 虽然,任何事情都会很棒,因为有数百个客户。

foreach ($folder in (Get-ChildItem 'C:\Users\S7051895\Desktop\Customer Service\Customer' -Directory))
{
        $files = Get-ChildItem $folder.FullName
        $newfolder = new-item -ItemType directory -Path ($folder.fullname+"\Quotes")
        $files | Move-Item -Destination $newfolder.FullName
}

You can get a list of all files in a folder before creating the "Quotes" folder. 在创建“ Quotes”文件夹之前,您可以获取文件夹中所有文件的列表。

Since you already have the code for creating the subfolder, all that's left to do is move the folder content to the new subfolder. 由于您已经具有创建子文件夹的代码,因此只需将文件夹内容移动到新的子文件夹即可。

$dir    = 'C:\Users\S7051895\Desktop\Customer Service\Customer'
$quotes = 'Quotes'

foreach ($folder in (Get-ChildItem $dir -Directory)) {
    $newdir = Join-Path $folder.FullName $quotes
    New-Item -ItemType Directory -Path $newdir | Out-Null
    Get-ChildItem $folder.FullName -Exclude $quotes |
        Move-Item -Destination $newdir
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将子文件夹名称导出为路径,并为不同位置的每个父子文件夹创建 csv - Export sub-folder name as path and create a csv for each parent sub-folder in a different location 创建子文件夹结构 - Creating a sub-folder structure 使用Powershell将文件夹结构中的文件复制到其各自的子文件夹 - Copy files in a folder structure to their respective sub-folder with Powershell 使用 powershell 将子文件夹内容上移一级; 不同的父文件夹 - Using powershell to move sub-folder contents up one level; different parent folders 如何使用powershell从文件夹的根目录中仅删除子文件夹和子文件夹内容而不是单个文件 - How to delete only the sub-folders and sub-folder contents but not individual files from the root of a folder using powershell 具有最新写访问权限的输出子文件夹 - Output sub-folder with the latest write access 在PowerShell中新子文件夹中检测CSV文件 - Detecting csv files in newly sub-folder in PowerShell 扫描Powershell中每个文件夹的特定子文件夹 - Scan specific sub-folder of every folder in powershell 重命名文件夹和子文件夹中的所有文件和文件夹 - Rename all files and folders in folder and sub folder Cmd - 使用命令行在子文件夹中创建文件的最简单方法 - Cmd - easiest way to create a file in sub-folder using command line
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM