make multiple files in multiple folders - powershell

I look here on the forums but didn't find my answer.
I'm trying to make multiple folders on my C drive, and at the same time multiple files in these folders.
The folders are no problem, I'm using an array for that. But the files are not working :).
This is my script:
$folders = #("Test1","Test2","Test3")
$files = #("Test1.txt","Test2.txt","Test3.txt")
foreach ($folder in $folders) {
New-Item -ItemType Directory -Path c:\ -Name $folder
}
foreach ($file in $files){
New-Item -ItemType File -Path $folder -Name $file
}

Nest your loops:
$folders = #("Test1","Test2","Test3")
$files = #("Test1.txt","Test2.txt","Test3.txt")
foreach ($folder in $folders) {
# Create folder
$newFolder = New-Item -ItemType Directory -Path c:\ -Name $folder
foreach ($file in $files){
# Create the files in the folder
$null = New-Item -ItemType File -Path $newFolder.FullName -Name $file
}
}

Related

How to move file from on sub folder to another sub folder also repeat same for other files using PowerShell

I am trying to copy a file from one location to another. The issue is there are multiples folders so am working under plugins folder which again has sub folders in it. I want to search in each folder if it contains MANIFEST.MF.eclipse file if yes, the I have to move that file in its subfolder called META-INF. (Need to do same for all subfolder under plugins)
$current_folder = .\temp\p2\plugins\com.xyz.*\MANIFEST.MF.eclipse -Recurse |
$new_folder = .\temp\p2\plugins\com.xyz.*\META-INF
if (Test-Path -Path $new_folder){
Move-Item -Path $current_folder -Destination -Path $new_folder
}
else{
New-Item -ItemType 'Directory' -Name $new_folder
Move-Item -Path $current_folder -Destination -Path $new_folder
}
OK, so you're looking to search through a given directory for any files that are called "MANIFEST.MF.eclipse", then move them to a sub directory calles \META-INF?
I'm sure there's more 'elegant' solutions, but this works in my limited test
$rootpath = "C:\test\app"
$foldername = "META-INF"
$files = Get-ChildItem $rootpath -Recurse -Filter *MANIFEST.MF.eclipse
foreach($f in $files)
{
$testpath = $f.DirectoryName +'\'+ $foldername
if(!(Test-Path $testpath))
{
mkdir $testpath
}
Move-Item $f.FullName -Destination $testpath
}

Powershell script to copy folders/files from text file

I am trying to copy all folders (and all files) from one folder to another in Powershell where the folders are listed in a text file. I have a script that successfully copied the folders, but the files did not copy over.
$file_list = Get-Content C:\Users\Desktop\temp\List.txt
$search_folder = "F:\Lists\Form601\Attachments\"
$destination_folder = "C:\Users\Desktop\601 Attachments 2021b"
foreach ($file in $file_list) {
$file_to_move = Get-ChildItem -Path $search_folder -Filter $file -Recurse -ErrorAction SilentlyContinue -Force | % { $_.FullName}
if ($file_to_move) {
Copy-Item $file_to_move $destination_folder
}
}
List.text contains folders like:
4017
4077
4125
I would use Test-Path on each of the folders in the list to find out if this folder exists. If so do the copy.
$folder_list = Get-Content -Path 'C:\Users\Desktop\temp\List.txt'
$search_folder = 'F:\Lists\Form601\Attachments'
$destination_folder = 'C:\Users\Desktop\601 Attachments 2021b'
# first make sure the destination folder exists
$null = New-Item -Path $destination_folder -ItemType Directory -Force
foreach ($folder in $folder_list) {
$sourceFolder = Join-Path -Path $search_folder -ChildPath $folder
if (Test-Path -Path $sourceFolder -PathType Container) {
# copy the folder including all files and subfolders to the destination
Write-Host "Copying folder '$sourceFolder'..."
Copy-Item -Path $sourceFolder -Destination $destination_folder -Recurse
}
else {
Write-Warning "Folder '$folder' not found.."
}
}

Create Subfolders using Powershell

Ok, looking for some assistance with Powershell. I need to create a subfolder with the same name in about 200 folders in a directory. So far I have this:
$folder = NewFolderName
new-item -type directory -path \\servername\directory\directory\$folder -Force
Will this work to create the single folder in all 200 folders?
Try the following code snippet:
$parent = '\\servername\directory'
$folder = 'NewFolderName'
Get-ChildItem -Path $parent -Directory |
ForEach-Object {
New-Item -WhatIf -Type Directory -Path (
Join-Path -Path $_.FullName -ChildPath $folder) -Force
}
Remove the risk mitigation parameter -WhatIf no sooner than debugged…

Create Folders and Moving Files in Powershell

I've seen similar questions and used them as a basis for what i'm trying to do here. I have a folder with many files that are named like "Author Name - Book Title.azw".
I'd like to create sub-folders for each author and move all their books into that folder. This is the script i have so far. It successfully creates folder for the Authors, but it chokes on the move-item, says Could not find a part of the path.
$files = Get-ChildItem -file
foreach ($file in $files){
$title = $file.ToString().Split('-')
$author = $title[0]
if (!(Test-Path $author))
{
Write-Output "Creating Folder $author"
New-Item -ItemType Directory -Force -Path $author
}
Write-Output "Moving $file to $author"
Move-Item -Path $file -Destination $author -Force
}
You have to use this:
Get-ChildItem -file | foreach {
$title = $_.ToString().Split('-')
$author = $title[0]
if (!(Test-Path $author))
{
Write-Host "Creating Folder $($author)"
New-Item -ItemType Directory -Force -Path "$author"
}
Write-Host "Moving $($_) to $($author)"
Move-Item -Path "$_" -Destination "$author" -Force
}
You must surround file paths in double quotes. So your code was not working.
From my understanding, you want to move files in a directory to sub folders where the author names are used as the sub folder names. You can try this solution to achieve this.
# Folder which contains files
$Path = "PATH_TO_FILES"
# Make sure path is a directory
if (Test-Path $Path -PathType Container) {
# Go through each file in folder
Get-ChildItem -Path $Path | ForEach-Object {
# Get full path of file
$filePath = $_.FullName
# Extract author
$author = $_.BaseName.Split("-")[0].Trim()
# Create subfolder if it doesn't exist
$subFolderPath = Join-Path -Path $Path -ChildPath $author
if (-not (Test-Path -Path $subFolderPath -PathType Container)) {
New-Item -Path $subFolderPath -ItemType Directory
}
# Move file to subfolder
Move-Item -Path $filePath -Destination $subFolderPath
}
}

Coping files from multiple sub folders using powershell

Copy file from multiple sub-folder to another multiple sub-folder
example :
C:\Nani\Code\Relase4\database1\tables
C:\Nani\Code\Relase1\database1\tables
C:\Nani\Code\Relase2\database1\tables
C:\Nani\Code\Relase3\cycle1\database1\tables
C:\Nani\Code\Relase1\database1.02.tables
I have .sql files in above all folders and i want to copy to
C\Build\database1\tables
if database1\tables directory is not there , i have to create it too ,
$sourceFolder = "C:\Nani\Code"
$targetFolder = "C\Build"
Get-Childitem $sourceFolder -recurse -filter "*.sql" -Exclude $exclude | %{
#If destination folder doesn't exist
if (!(Test-Path $targetFolder -PathType Container)) {
#Create destination folder
New-Item -Path $targetFolder -ItemType Directory -Force
}
Copy-Item -Path $_.FullName -Destination $targetFolder -Recurse -force
}
above code is not creating sub folders in destination ,
I have kept the script very simple for your understanding and commented the sections.
Make sure you add all the validations for paths and error handling. Else if any of the files is giving any issue, then it wont proceed and will break the loop.
Script:
#Keeping all the sources in an array
$Sources = #("C:\Nani\Code\Relase4\database1\tables",
"C:\Nani\Code\Relase1\database1\tables",
"C:\Nani\Code\Relase2\database1\tables",
"C:\Nani\Code\Relase3\cycle1\database1\tables",
"C:\Nani\Code\Relase1\database1.02.tables")
$Destination="C\Build\database1\tables\"
#Iterating each source folder
foreach($source in $sources)
{
#Getting all the sql files under an iteration folder recursively
$files=Get-ChildItem -Path $source -Filter "*.sql" -Recurse
#Iterating all the files underneath a single source folder
foreach ($file in $files)
{
#Copying the files for a single folder to the destination
Copy-Item $file.PSPath -Destination ("$Destination" + ($file.PSParentPath | Split-Path -Leaf) + '_' + $file)
}
}
Hope it helps.
Try this, I am creating each folder first before copying files into it.
$sourceFolder = "C:\Nani\Code"
$targetFolder = "C:\Build"
$sources = Get-Childitem $sourceFolder -recurse -filter "*.sql" -Exclude $exclude | Select FullName, DirectoryName
foreach ($source in $sources)
{
$Releasepath = [regex]::match($source.DirectoryName,'C:\\Nani\\Code\\Release\d').Value
$split = $Releasepath.Replace("\","\\")
$targetfolderLeaf = $source.DirectoryName -split $split | select -Last 1
$targetfolderpath = $targetFolder+$targetfolderLeaf
if (!(Test-Path $targetfolderpath -PathType Container)) {
#Create destination folder
New-Item -Path $targetfolderpath -ItemType Directory -Force
}
Copy-Item -Path $source.FullName -Destination $targetfolderpath -Recurse -force
}