[英]Creating a sub-folder structure
Looking for some advice on how to use PowerShell (or some other means) to create some folders.寻求有关如何使用 PowerShell(或其他方式)创建一些文件夹的建议。
I have a list of around 120 products that I've allocated separate folders.我有一份大约 120 种产品的列表,这些产品已分配给单独的文件夹。 (using a CSV to generate the folders)
(使用 CSV 生成文件夹)
I want each product folder to have the same subfolder structure.我希望每个产品文件夹都具有相同的子文件夹结构。 As shown below:
如下所示:
[Products]
├── [Product 1]
│ ├── [1. Datasheet]
│ │ ├── [1. Current]
│ │ ├── [2. Archived]
│ │ ├── [3. Legacy]
│ │ ├── [4. Draft]
│ │ └── [5. Resources]
│ ├── [2. Images]
│ ├── [3. Brochures]
│ ├── [4. Manuals]
│ └── [5. Software]
│
├── [Product 2]
│ ├── [1. Datasheet]
│ │ ├── [1. Current]
│ │ ├── [2. Archived]
│ │ ├── [3. Legacy]
│ │ ├── [4. Draft]
│ │ └── [5. Resources]
│ ├── [2. Images]
│ ├── [3. Brochures]
│ ├── [4. Manuals]
│ └── [5. Software]
:
:
Essentially the first layer of subfolders in each would be:基本上每个子文件夹的第一层将是:
[1. Datasheet]
[1. Datasheet]
, [2. Images]
[1. Datasheet]
, [2. Images]
[2. Images]
, [3. Brochures]
[2. Images]
, [3. Brochures]
[3. Brochures]
, [4. Manuals]
[3. Brochures]
, [4. Manuals]
[4. Manuals]
, [5. Software]
[4. Manuals]
, [5. Software]
[5. Software]
Inside each of these would be the following:在每一个内部将是以下内容:
[1.Current]
, [2.Archived]
, [3. Legacy]
[1.Current]
[2.Archived]
, [3. Legacy]
[2.Archived]
, [3. Legacy]
[3. Legacy]
, [4. Draft]
[3. Legacy]
, [4. Draft]
[4. Draft]
, [5. Resources]
[4. Draft]
, [5. Resources]
[5. Resources]
I don't mind doing this in stages, it's just I don't know where to begin.我不介意分阶段做这件事,只是我不知道从哪里开始。
This could work:这可以工作:
$workingdir = 'c:\temp'
$products = Get-Content c:\temp\listofproducts.txt
$rootfolders = @(
'Datasheet'
'Images'
'Brochures'
'Manuals'
'Software'
)
$subfolders = @(
'Current'
'Archived'
'Legacy'
'Draft'
'Resources'
)
foreach ($product in $products)
{
$rootcount = 0
foreach ($root in $rootfolders)
{
$rootcount++
$subcount = 0
foreach ($sub in $subfolders)
{
$subcount++
mkdir (Join-Path $workingdir ("$product\$rootcount. $root\$subcount. $sub"))
}
}
}
or you could just create the first product folder then copy and paste it then rename the product或者您可以创建第一个产品文件夹,然后复制并粘贴它,然后重命名产品
Thanks for the input.感谢您的投入。
Managed to find a strategy that worked for me, so will share it in case it's of use to anyone else like me.设法找到了一个对我有用的策略,所以会分享它,以防它对像我这样的其他人有用。
Used a combination of the following 3 bits of code to achieve what I needed:使用以下 3 位代码的组合来实现我所需要的:
# Create Folders from CSV
$folder = "Z:\Products\"
$name = Import-Csv Z:\Products\names.csv
Foreach ($line in $name)
{
New-Item -path $folder -Name $line.Name -Type Directory
}
This above code allowed me to make a big list of folders from a CSV list made in Excel.上面的代码允许我从 Excel 中创建的 CSV 列表中创建一个大的文件夹列表。
# Add a Subfolder
foreach($folder in (gci 'Z:\Products' -directory)){
new-item -ItemType directory -Path ($folder.fullname+"\subfolder")
}
This above code let me populate the list of folders with subfolders.上面的代码让我用子文件夹填充文件夹列表。 I added them one at a time.
我一次添加一个。
# Copy a file to folder
$folders = Get-ChildItem Z:\Products
foreach ($folder in $folders.name){
Copy-Item -Path "Z:\Datasheet\changelog.txt" -Destination "Z:\Products\$folder" -Recurse
}
This above code allowed me to copy items to subfolder locations.上面的代码允许我将项目复制到子文件夹位置。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.