Home > database >  Read text file, creater folders and copy files with Powershell scirpt
Read text file, creater folders and copy files with Powershell scirpt

Time:12-10

I was trying to make some kind of autocopy Powershell script that can read a text file with project names and file names and then create new folders with project names and copy those files into them. So, for example, i have file RELEASE.txt with:

project1 example1.rule
project1 example1.doc
project2 example2.sql
project3 example3.sql

I need to run a script on my local machine, that can read this file, create folders project* and copy this example*.* files to the appropriate folders.

I understand step with Get-Content command, that just copy all files from list to specific folder, but i want to do something more easier to manage for releases.

Get-Content 'c:\Test\RELEASE.txt' | ForEach-Object { Copy-Item "c:\Test\Projects\$_" c:\Test\Project-1 }

CodePudding user response:

Although not very clear in your question where the files to be copied are to be found, I gather these are all in the same root folder where you want to create subfolders.

For that you can do:

$sourceFolder = 'C:\repo'
$rootFolder = 'C:\Test\Projects'
Get-Content 'C:\Test\RELEASE.txt' | ForEach-Object { 
    # split the line from the text file in a project and filename
    $project, $file = ($_ -split '\s ').Trim()
    $sourceFile = Join-Path -Path $sourceFolder -ChildPath $file
    if (Test-Path -Path $sourceFile -PathType Leaf) {
        $targetFolder = Join-Path -Path $rootFolder -ChildPath $project
        # create the subfolder if it does not already exist
        $null = New-Item -Path $targetFolder -ItemType Directory -Force
        # copy the file to the new Project subfolder
        Copy-Item -Path $sourceFile -Destination $targetFolder
    }
    else {
        Write-Warning "File '$file' could not be found in folder '$rootFolder'"
    }
}

As per your comment, if the filename taken from the RELEASE.txt needs to be searched inside possible subdirectories inside the Source folder, you could do:

$sourceFolder   = 'C:\repo'            # where the original files to copy are
$projectsFolder = 'C:\Test\Projects'   # where the project subfolders and file copies need to go

Get-Content 'C:\Test\RELEASE.txt' | ForEach-Object { 
    # split the line from the text file in a project and filename
    $project, $file = ($_ -split '\s ').Trim()
    # search for this file inside the Source folder. 
    # This COULD return several files with that name in different subfolders..
    # to not get into trouble with naming collisions in the ProjectX subfolder 
    # when copying, grab only the first one found.
    $sourceFile = Get-ChildItem -Path $sourceFolder -Filter $file -File -Recurse | Select-Object -First 1
    if ($sourceFile) {
        $targetFolder = Join-Path -Path $projectsFolder -ChildPath $project
        # create the subfolder if it does not already exist
        $null = New-Item -Path $targetFolder -ItemType Directory -Force
        # copy the file to the new Project subfolder
        $sourceFile | Copy-Item -Destination $targetFolder
    }
    else {
        Write-Warning "File '$file' could not be found in folder '$projectsFolder'"
    }
}
  • Related