简体   繁体   中英

Powershell: How can I create text files within folders recursively?

I'm new to Powershell and I have searched for a way to recursively create text files within newly created folders but the code I have so far is not creating the text file in the appropriate folder but instead creating the text file in the top directory.

I have a list of names in a file. For each name in the list, I need a folder with the person's name and a text file with their name in their respective folder. The code I have so far is:

 FOREACH ($User in $UserList)
 { 
$NewFolder = ("C:\Users\me\Documents\" + User.UserName + "Folder");

New-Item $NewFolder -type directory;

$NewFile = ($NewFolder + User.UserName + "File");

New-Item $NewFile -type file;
}

Any help is greatly appreciated.

That should do it. Just made minor changes to your original script.

Like I asked in the comment, UserName sounded like the header used in a csv input file. That is why, using $user.UserName is necessary.

If it is just a text file, change $user.Username to just $user

foreach ($User in $UserList)
{ 
    $NewFolder = "C:\Users\me\Documents\" + $User.UserName + "Folder"
    New-Item $NewFolder -type directory
    $NewFile = "$NewFolder\" + $User.UserName + "File.txt"
    New-Item $NewFile -type file
}
$userlist = @{UserName='auser'},@{UserName='anotheruser'}
$baseDirectory = "C:\temp\46501232"

foreach ($user in $userlist.UserName)
{
    New-Item -ItemType Directory -Path $baseDirectory -Name "$user"
    New-Item -ItemType File -Path "$baseDirectory\$user" -Name "File"
}

PS C:\temp\46501232> ls C:\temp\46501232;ls "C:\temp\46501232\auser"

Directory: C:\temp\46501232
Mode                LastWriteTime         Length Name                                                                                                                                                                                                

----                -------------         ------ ----                                                                                                                                                                                                

d-----       30.09.2017     13.39                anotheruser                                                                                                                                                                                         
d-----       30.09.2017     13.39                auser                                                                                                                                                                                               

Directory: C:\temp\46501232\auser

Mode                LastWriteTime         Length Name                                                                                                                                                                                                
----                -------------         ------ ----                                                                                                                                                                                                
-a----       30.09.2017     13.39              0 File                                

try this method :

$userlist = @{UserName='UserName1'},@{UserName='UserName2'}
$Dir= "C:\temp\"

foreach ($User in $UserList.UserName)
{ 
    $PathFile =[System.IO.Path]::Combine($Dir, $User + "Folder" , $User + "File.txt")
    New-Item $PathFile -type file -Force
}

Like you can see, if you use -force, not nesserary to create folders, they are created automaticly. I use Combine method because if your dir path end with \\ or not the path is created.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM