locked
Renaming multiple files using Get-Content RRS feed

  • Question

  • Hi -

    I'm new to Powershell, and need to rename multiple txt files with the first line of contents from the file.

    Using Get-Content, I'm able to get the contents, but I'm not sure how to tie it all together.

    Get-Content *.txt -First 1

    Get-ChildItem -Filter "*job*" -Path C:\Files-Recurse | Rename-Item -NewName "From Content.txt"

    Thanks!

    Sunday, November 1, 2020 4:32 PM

All replies

  • When you're just starting out, the trick is to NOT try to do everything all at once. Split it up into smaller pieces of code. Test them. Get them to work. Understand the variables and the data that's contained in them. Be verbose with output. Yes, an experienced programmer could do this in one line of code but you will be better off to split things up so that they are more understandable/readable. 

    To start you need to process some files. Get that working. Here I use my own set of test files. You should build your own test files to play with.

    "Here are the files that I found."
    Get-ChildItem -Filter "*.ren" -Path C:\Temp -Recurse | foreach {
        $_.fullname
    }
    

    Then read the first line from each file. 

    "Here are the files that I found along with the first line from each."
    Get-ChildItem -Filter "*.ren" -Path C:\Temp -Recurse | foreach {
        $_.fullname
        Get-Content -Path $_.fullname -First 1 
    }
    

    Next you build the new name of the file. 

    "Here are the files that I found, some more values, and the new name."
    Get-ChildItem -Filter "*.ren" -Path C:\Temp -Recurse | foreach {
        $_.fullname
        $_.name
        $_.Extension
        $newname = Get-Content -Path $_.fullname -First 1      # The first line of each 
        $newname += $_.Extension                               # Append the file extension.  
        $newname
    }


    On day 1 of the first programming course that I ever took, the teacher said; "garbage in, garbage out". When writing code, you you need to validate your data. Think about what could go wrong. Handle errors.

    "Here are the files that I found, the new name, along with some nicer formatting. " Get-ChildItem -Filter "*.ren" -Path C:\Temp -Recurse | foreach { "" "Full name: {0}" -f $_.fullname "Short name: {0}" -f $_.name "Extension: {0}" -f $_.Extension $newname = Get-Content -Path $_.fullname -First 1 $newname += $_.Extension # Append the file extension. "New name: {0}" -f $newname if ($_.Name -eq $newname) { # Do our names match? "The names match, nothing to do here." return # Go process the next item in the pipeline } # Hmmm, what do we do if a file already exists with that new name??? # Hmmm, what do I do if the first line of the file was all blanks, or has leading/trailing blanks ? # rename-item goes somewhere around here. } # Run these commands to get more help. # get-help test-path -examples # get-help rename-item -examples

    # get-help about_Try_Catch_Finally


    See if you can finish it. 

    Sunday, November 1, 2020 7:28 PM