What is wrong with this script to create files in

2019-09-02 03:24发布

I have a directory tree, consisting of several layers, within which I want to create 30 placeholder files, recursively in each folder.

The tree looks something like this:

--F:\inbox\test  
 ----folder1  
 ------subfolder1  
 ------subfolder2  
 ----folder2  
 ------subfolder1  
 ------subfolder2  
 ----folder3  
 ------subfolder1  
 ------subfolder2

Here is what I have.

$folders = gci -path f:\inbox\test -recurse | where {$_.PsIsContainer}  
foreach ($folder in $folders) {  
        1..30 | % { New-Item -Name "$_.txt" -Value (get-date).tostring() -Itemtype file -force}
}

This just creates 30 files in the root folder. I know I am missing something in my logic.

1条回答
劫难
2楼-- · 2019-09-02 03:38

You're not telling new-item where to put the file, so it uses the current working directory. Fortunately, this is easliy fixed with the -Path parameter for the cmdlet.

$folders = gci -path f:\inbox\test -recurse | where {$_.PsIsContainer}  
foreach ($folder in $folders) {  
        1..30 | % { New-Item -Path $folder.FullName -Name "$_.txt" -Value (get-date).tostring() -Itemtype file -force}
}
查看更多
登录 后发表回答