Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Loops Powershell循环浏览文件夹,在每个文件夹中创建文件_Loops_Powershell - Fatal编程技术网

Loops Powershell循环浏览文件夹,在每个文件夹中创建文件

Loops Powershell循环浏览文件夹,在每个文件夹中创建文件,loops,powershell,Loops,Powershell,我试图递归地遍历目录中的所有文件夹,然后在每个目录中执行一些操作。例如,创建一个文本文件 我可以将所有子文件夹放入一个变量中,如下所示: $folders = Get-ChildItem -Recurse | ?{ $_.PSIsContainer } 但是,我似乎无法在每个子目录中创建文件,而是在顶层目录中创建文件。这不起作用: $folders | ForEach-Object { New-Item -ItemType file -Name "$_.txt" -path $_} 这会引发

我试图递归地遍历目录中的所有文件夹,然后在每个目录中执行一些操作。例如,创建一个文本文件

我可以将所有子文件夹放入一个变量中,如下所示:

$folders = Get-ChildItem -Recurse | ?{ $_.PSIsContainer }
但是,我似乎无法在每个子目录中创建文件,而是在顶层目录中创建文件。这不起作用:

$folders | ForEach-Object { New-Item -ItemType file -Name "$_.txt" -path $_}
这会引发一个错误,即找不到目录或路径太长

如何在嵌套文件夹的每个子目录中执行操作?

尝试以下操作:

Get-ChildItem -Recurse -Directory | ForEach-Object {New-Item -ItemType file -Path "$($_.FullName)" -Name "$($_.Name).txt" }
基本上,
getchilditem
命令返回一系列
DirectoryInfo
对象。
FullName
属性包含完整路径,而
Name
仅包含叶目录的名称