Search code examples
powershellfiledirectoryzip

Make Individual Folders From .txt Files In A Folder With Powershell


Beginner here! So preemptive apologies. Most of what I've used has just been slightly modified code, and I've just started learning the real basics so I can stop modifying and making it myself.

I've got a folder full of .txt files and I need to make a script (still just dipping my toe, so I don't have a start) that makes a folder for each text file. From that point I need to also have a PDF or two copied into each of the folders. Then each of them zipped.

So what I'm hoping to accomplish with this would be if my List folder of text files is full of:

123.txt 234.txt 345.txt (and so on)

That I end up with a folder full of folders containing the text file, each folder with the same name as the text file, so:

ListFolder > (folder)123 (folder)234 (folder)345, where (folder)123 contains 123.txt, (folder)234 contains 234.txt, and so on.

Then I have a PDF or two, that I want to copy into each of those folders, so (folder)123 would contain 123.txt, PDF1, and PDF2. And have that happen to each folder so they all contain the original txt file as well as the PDF or two.

Then just have them individually zip, taking each folder with 3 files (if there were 2 PDF files) and ending with 123.zip, 234.zip, 345.zip, and so on.

I hope that made sense. There's just numerous steps involved that I know have to have a way to be simplified. Some of these folders will have hundreds of txt files, so you can imagine how tedious that would be by hand. Thanks for any help and guidance!


Solution

  • I just wrote a small script which does what you need with the txt files. For the pdf files you need to specify which pdf goes into which folder!

    Full Script

    $path = "C:\temp\txt\" #Path where all the TXT files are
    $pdfpath = "C:\temp\pdf\" #Path where all the PDF Files are
    
    $files = Get-Childitem $path | Where-Object { ! $_.PSIsContainer} #Get all files in the folder
    
    #For each file in this folder
    foreach ($file in $files){
        ## If it is a txt file
        if ([io.path]::GetExtension($file.Name) -eq ".txt"){ # If it is a .txt file
            $foldername = [io.path]::GetFileNameWithoutExtension($file) #Remove the fileextension in the foldername
            if (!(Test-Path "$path\$foldername")){ #If the folder doesn't exist
                New-Item "$path\$foldername" -ItemType "directory" #Create a new folder for the file
            }
            Move-Item $file.FullName "$path\$foldername" #Move file into the created folder
        }
    }
    
    #Create ZIP-Archive
    $folders = Get-Childitem $path | Where-Object {$_.PSIsContainer} #Get all folders
    
    foreach ($folder in $folders){
        Copy-Item -Path "$pdfpath\*" -Destination "$path\$folder" #Copy PDF into the folder
        Get-Childitem -Path "$path\$folder" | Compress-Archive -DestinationPath "$path\$folder.zip" #Zip the folder
        Remove-item "$path\$folder" -Force -Recurse #Remove the folder
    }
    

    Let me know if it worked. - Nicicalu