[英]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.