[英]Create file list in every subfolder using PowerShell
我是 PowerShell 的新手,对 PowerShell 脚本的知识基本上为零,这就是我需要一些帮助的原因。
我有带有子文件夹的文件夹结构(如下所示),我想使用Get-ChildItem -Recurse
在每个第一级子文件夹中生成一个 filelist.txt。
Main Folder
├───Sub 1
│ │ file.ext
│ └───Sub A
│ file.ext
└───Sub 2
| | file.ext
| └───Sub B
└─── etc.
到目前为止,我尝试了一些我在网上找到的代码,但要么出现错误,要么完全没有任何反应。 这是最接近我想要的东西,因为它在正确的目录中创建 filelist.txt 文件,但不幸的是这些文件是空的。 此外,在我不想要的主文件夹中创建了一个 filelist.txt。
Get-ChildItem -Recurse -Directory |
ForEach-Object {
New-Item -ItemType file -Path "$($_.FullName)" -Name "filelist.txt"
Get-ChildItem $_ -Recurse > filelist.txt
}
非常感谢任何帮助或建议!
如果您正在寻找Main Folder
的直接子文件夹,您不应该在第一次调用Get-ChildItem -Directory
时使用-Recurse
,因为这会递归地为您提供所有子文件夹,正如zett42在他的有用评论中指出的那样。
关于您的出口线:
Get-ChildItem $_ -Recurse > filelist.txt
它会将filelist.txt
文件放在您当前的位置而不是子文件夹中,它还会在每次循环迭代时替换文件。
如果我正确理解您要查找的内容,代码将如下所示:
Get-ChildItem 'path\to\Main Folder' -Directory | ForEach-Object {
$destination = Join-Path $_.FullName -ChildPath filelist.txt
Get-ChildItem -LiteralPath $_.FullName -Recurse |
Set-Content $destination
}
值得注意的是,这只会导出Main Folder
的每个子文件夹下的文件和文件夹的绝对路径,但是可能值得将导出类型更改为 CSV 以获得每个 object 的属性,如您在控制台上看到的那样:
$destination = Join-Path $_.FullName -ChildPath filelist.csv
Get-ChildItem -LiteralPath $_.FullName -Recurse |
Export-Csv $destination -NoTypeInformation
当使用 PowerShell cmdlet 找到合适的解决方案时,我通常会避免使用外部命令,但tree /f
已经为您做到了。 依靠抄本来提供目录树有些奇怪,但您应该能够毫无问题地手动将 output 重定向到文件:
cd /path/to/base/folder
tree /f > filelist.txt
这将相对于您运行tree /f
的目录创建filelist.txt
,并将 output 重定向到filelist.txt
。 请注意,如果文件夹树中已存在filelist.txt
,它将与文件的 rest 一起被枚举。
这就是我所做的。
##variables
$startingLocation = Get-Location
$subs = Get-ChildItem -Recurse -Depth 1 -Directory
$startingCheck = ($startingLocation).Path + "\filelist.txt"
##create starting list in original directory
##check if it exists already and write to it
if ( Test-Path -Path $startingCheck) {
Get-ChildItem -Recurse >> filelist.txt
}
else {
Write-Warning "File doesn't exist... creating file."
New-Item -ItemType File -Path $startingLocation -Name "filelist.txt"
Write-Warning "File created."
Get-ChildItem -Recurse >> filelist.txt
Write-Warning "File created and now writing GCI to log."
}
##loop
foreach ($s in $subs) {
##go to the location
Set-Location -Path $s.FullName -Verbose
##test if output file exists in current directory of loop
$testPwd = Get-Location -Verbose
$currentPath = ($testPwd).Path + "\filelist.txt"
## if the file already exists - write GCI to it
if (Test-Path -Path $currentPath) {
Get-ChildItem -Recurse -Depth 1 >> filelist.txt
Write-Warning "File exists... writing GCI to log."
}
## otherwise if there is no file, make one and write GCI to it
else {
Write-Warning "File doesn't exist... creating file."
New-Item -ItemType File -Path $testPwd -Name "filelist.txt"
Write-Warning "File created."
Get-ChildItem -Recurse -Depth 1 >> filelist.txt
Write-Warning "File created and now writing GCI to log."
}
##exit the loop
}
#go back to original location
Set-Location $startingLocation
前:
Folder PATH listing for volume Windows
Volume serial number is A4DB-B980
C:.
│ test.ps1
│
├───Folder1
│ │ doc1.txt
│ │ doc2.txt
│ │
│ ├───FolderA
│ └───FolderB
│ └───FolderB1
│ doc1b.txt
│ doc2b.txt
│
├───Folder2
│ ├───FolderA
│ └───FolderB
└───Folder3
结果:
Folder PATH listing for volume Windows
Volume serial number is A4DB-B980
C:.
│ filelist.txt
│ test.ps1
│
├───Folder1
│ │ doc1.txt
│ │ doc2.txt
│ │ filelist.txt
│ │
│ ├───FolderA
│ │ filelist.txt
│ │
│ └───FolderB
│ │ filelist.txt
│ │
│ └───FolderB1
│ doc1b.txt
│ doc2b.txt
│
├───Folder2
│ │ filelist.txt
│ │
│ ├───FolderA
│ │ filelist.txt
│ │
│ └───FolderB
│ filelist.txt
│
└───Folder3
filelist.txt
我想你想要的是:
Get-ChildItem -Path $BaseFolder -Depth 1
如果您需要另一个级别,您可以使用 -Depth 参数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.