创建文件夹 -> 文件 -> 并在这些文件中转储数据
Create Folder -> Files -> and dump data in those files
我有一个包含 3 列的 table:文件夹目录、文件名和数据。我想根据第 1 列中的各种值在我的文件系统中创建文件夹,然后在该文件夹下创建名称与第 2 列相同的文件,最后将所有数据转储到第 3 列中的文件中。我可以创建一个 excel 获取我 table 的信息,但不确定如何创建 Folders/Files 并递归地转储数据。非常感谢任何帮助。
这是它在 Powershell 中的样子:
Input.csv 看起来像这样:
Folder Directory,FILE NAME,DATA
Main/A,FILE-A1,DATA-A1
Main/A,FILE-A2,DATA-A2
Main/B,FILE-B1,DATA-B1
Main/C,FILE-C1,DATA-C1
脚本如下所示:
# Load CSV into variable
$CSV = Import-Csv 'C:\script\test\input.csv'
# Set root folder for new folders to be created in
$FolderRoot = 'C:\script\test\root'
# Iterate through each line in $CSV
foreach ($Line in $CSV) {
# Join $FolderRoot and the folder from the current line to a proper path
$Path = (Join-Path $FolderRoot $Line.'Folder Directory')
# Check if folder already exists, create it if not.
if (!(Test-Path $Path)) {
New-Item -Path $Path -ItemType Directory
}
# Write the content of the DATA column into the file in the FILE NAME column.
# Change to Add-Content if you want to add info to the file instead of overwriting.
Set-Content -Path (Join-Path $Path $Line.'FILE NAME') -Value $Line.'DATA'
}
Join-Path 很不错,因为它会自动处理反斜杠、正斜杠、尾随斜杠等并创建正确的路径。
我有一个包含 3 列的 table:文件夹目录、文件名和数据。我想根据第 1 列中的各种值在我的文件系统中创建文件夹,然后在该文件夹下创建名称与第 2 列相同的文件,最后将所有数据转储到第 3 列中的文件中。我可以创建一个 excel 获取我 table 的信息,但不确定如何创建 Folders/Files 并递归地转储数据。非常感谢任何帮助。
这是它在 Powershell 中的样子:
Input.csv 看起来像这样:
Folder Directory,FILE NAME,DATA
Main/A,FILE-A1,DATA-A1
Main/A,FILE-A2,DATA-A2
Main/B,FILE-B1,DATA-B1
Main/C,FILE-C1,DATA-C1
脚本如下所示:
# Load CSV into variable
$CSV = Import-Csv 'C:\script\test\input.csv'
# Set root folder for new folders to be created in
$FolderRoot = 'C:\script\test\root'
# Iterate through each line in $CSV
foreach ($Line in $CSV) {
# Join $FolderRoot and the folder from the current line to a proper path
$Path = (Join-Path $FolderRoot $Line.'Folder Directory')
# Check if folder already exists, create it if not.
if (!(Test-Path $Path)) {
New-Item -Path $Path -ItemType Directory
}
# Write the content of the DATA column into the file in the FILE NAME column.
# Change to Add-Content if you want to add info to the file instead of overwriting.
Set-Content -Path (Join-Path $Path $Line.'FILE NAME') -Value $Line.'DATA'
}
Join-Path 很不错,因为它会自动处理反斜杠、正斜杠、尾随斜杠等并创建正确的路径。