PowerShell command to get a file that has the highest number in the filename stringHow to sort by file name the same way Windows Explorer does?Executing an EXE file using a PowerShell scriptHow to search a string in multiple files and return the names of files in Powershell?PowerShell get-childitem cannot handle filename starting with [ character even with escape characterPowerShell command to scan a network drive for certain filenamePowerShell - Determine the existence of certain files in a folder hierarchy efficientlyGetting error when trying to navigate folder, subfolders and filesHow to search a string in multiple files and return file name with line number/text in an Excel or csv in PowershellCopy files in a folder structure to their respective sub-folder with PowershellHow to structure Out-File as directory then any files within that directoryHow to get Foldername into the output txt file of Using Out-File or “>” in Powershell?

Generate random number in Unity without class ambiguity

How long should I wait to plug in my refrigerator after unplugging it?

Subtle ways to render a planet uninhabitable

Why is the Vasa Museum in Stockholm so Popular?

In a KP-K endgame, if the enemy king is in front of the pawn, is it always a draw?

How to design an effective polearm-bow hybrid?

Have you been refused entry into the Federal Republic of Germany?

How to call made-up data?

Being told my "network" isn't PCI compliant. I don't even have a server! Do I have to comply?

Is there any difference between "result in" and "end up with"?

Is the first page of Novel really that important?

Why wasn't interlaced CRT scanning done back and forth?

Why does the friction act on the inward direction when a car makes a turn on a level road?

A wiild aanimal, a cardinal direction, or a place by the water

Implementation of a Singularly LinkedList

What does Argus Filch specifically do?

What is Modern Vipassana?

Astable 555 circuit not oscillating

Do moonless nights cause dim light to become darkness, and bright light (e.g. from torches) to become dim light?

Polygons crash kernel?

C# TCP server/client class

What is it exactly about flying a Flyboard across the English channel that made Zapata's thighs burn?

A verb for when some rights are not violated?

How do people drown while wearing a life jacket?



PowerShell command to get a file that has the highest number in the filename string


How to sort by file name the same way Windows Explorer does?Executing an EXE file using a PowerShell scriptHow to search a string in multiple files and return the names of files in Powershell?PowerShell get-childitem cannot handle filename starting with [ character even with escape characterPowerShell command to scan a network drive for certain filenamePowerShell - Determine the existence of certain files in a folder hierarchy efficientlyGetting error when trying to navigate folder, subfolders and filesHow to search a string in multiple files and return file name with line number/text in an Excel or csv in PowershellCopy files in a folder structure to their respective sub-folder with PowershellHow to structure Out-File as directory then any files within that directoryHow to get Foldername into the output txt file of Using Out-File or “>” in Powershell?






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








0















I'm new to PowerShell. I have files in a folder with one-up numbers. I'm trying to find the file with the highest number and change that name.



I'm able to return the file with the following command:



Get-ChildItem -Path C:TempWayneFolder1File*.txt |
Sort-Object |
Select-Object -Last 1 -ExpandProperty Name


it returns:



Get-ChildItem -Path C:TempWayneFolder2File*.txt |
Sort-Object |
Select-Object -Last 1 -ExpandProperty Name

PS C:> File0005.txt


I would like to change File0005.txt, to FileCHANGED(TodaysDate).txt, then move it to C:TempWayneFolder1File*.txt



I'm able to return the correct file, but before I can get to the point of moving it, I'm stuck at trying to rename it.










share|improve this question


























  • Please format your code and sample input/output properly.

    – mklement0
    Mar 27 at 1:53

















0















I'm new to PowerShell. I have files in a folder with one-up numbers. I'm trying to find the file with the highest number and change that name.



I'm able to return the file with the following command:



Get-ChildItem -Path C:TempWayneFolder1File*.txt |
Sort-Object |
Select-Object -Last 1 -ExpandProperty Name


it returns:



Get-ChildItem -Path C:TempWayneFolder2File*.txt |
Sort-Object |
Select-Object -Last 1 -ExpandProperty Name

PS C:> File0005.txt


I would like to change File0005.txt, to FileCHANGED(TodaysDate).txt, then move it to C:TempWayneFolder1File*.txt



I'm able to return the correct file, but before I can get to the point of moving it, I'm stuck at trying to rename it.










share|improve this question


























  • Please format your code and sample input/output properly.

    – mklement0
    Mar 27 at 1:53













0












0








0








I'm new to PowerShell. I have files in a folder with one-up numbers. I'm trying to find the file with the highest number and change that name.



I'm able to return the file with the following command:



Get-ChildItem -Path C:TempWayneFolder1File*.txt |
Sort-Object |
Select-Object -Last 1 -ExpandProperty Name


it returns:



Get-ChildItem -Path C:TempWayneFolder2File*.txt |
Sort-Object |
Select-Object -Last 1 -ExpandProperty Name

PS C:> File0005.txt


I would like to change File0005.txt, to FileCHANGED(TodaysDate).txt, then move it to C:TempWayneFolder1File*.txt



I'm able to return the correct file, but before I can get to the point of moving it, I'm stuck at trying to rename it.










share|improve this question
















I'm new to PowerShell. I have files in a folder with one-up numbers. I'm trying to find the file with the highest number and change that name.



I'm able to return the file with the following command:



Get-ChildItem -Path C:TempWayneFolder1File*.txt |
Sort-Object |
Select-Object -Last 1 -ExpandProperty Name


it returns:



Get-ChildItem -Path C:TempWayneFolder2File*.txt |
Sort-Object |
Select-Object -Last 1 -ExpandProperty Name

PS C:> File0005.txt


I would like to change File0005.txt, to FileCHANGED(TodaysDate).txt, then move it to C:TempWayneFolder1File*.txt



I'm able to return the correct file, but before I can get to the point of moving it, I'm stuck at trying to rename it.







powershell






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 27 at 8:45









boxdog

4,1672 gold badges7 silver badges15 bronze badges




4,1672 gold badges7 silver badges15 bronze badges










asked Mar 27 at 1:43









Wayne W. Van EllisWayne W. Van Ellis

1




1















  • Please format your code and sample input/output properly.

    – mklement0
    Mar 27 at 1:53

















  • Please format your code and sample input/output properly.

    – mklement0
    Mar 27 at 1:53
















Please format your code and sample input/output properly.

– mklement0
Mar 27 at 1:53





Please format your code and sample input/output properly.

– mklement0
Mar 27 at 1:53












2 Answers
2






active

oldest

votes


















0














try this :



Get-ChildItem "C:TempFile*.txt" | sort Name -Descending | select -First 1 | %

$Newname="0:yyyy-MM-dd-HH-mm-ss-fffff(1:yyyy-MM-dd-HH-mm-ss-fffff)" -f $_.LastWriteTime, (Get-Date)
Rename-Item $_.FullName -NewName $Newname






share|improve this answer
































    0














    • Your (by default alphabetically) sorting approach only works with numbers of equal length.

    • more universal is using $ToNatural by Roman Kuzmin which prepends all numbers with zeroes to a unique length

    On my empty Ramdrive A: the following script:



    ## Q:Test2019327SO_55368572.ps1
    $SrcDir = 'A:Folder1' # 'C:TempWayneFolder1'
    $DstDir = 'A:Folder2' # 'C:TempWayneFolder2'

    ## create test folders,files
    MD $SrcDir,$DstDir | Out-Null
    1..5|New-Item -ItemType File -Path "0File1:D4.txt" -f $SrcDir,$_|Out-Null

    "_"*10+" Before "+"_"*10
    Tree A: /F

    Get-ChildItem -Path $SrcDir -Filter File*.txt |
    Sort-Object | Select-Object -Last 1 |
    Move-Item -Destination Join-Path $DstDir (
    "FileChanged(0:yyyy-MM-dd)1" -f (Get-Date),$_.Extension)

    "_"*10+" After "+"_"*10
    Tree A: /F

    Tree A: /F


    yields this (German locale) output:



    > Q:Test2019327SO_55368572.ps1
    __________ Before __________
    Auflistung der Ordnerpfade für Volume RamDisk
    Volumeseriennummer : 5566-7788
    A:
    ├───Folder1
    │ File0001.txt
    │ File0002.txt
    │ File0003.txt
    │ File0004.txt
    │ File0005.txt

    └───Folder2
    __________ After __________
    Auflistung der Ordnerpfade für Volume RamDisk
    Volumeseriennummer : 5566-7788
    A:
    ├───Folder1
    │ File0001.txt
    │ File0002.txt
    │ File0003.txt
    │ File0004.txt

    └───Folder2
    FileChanged(2019-03-27).txt





    share|improve this answer



























      Your Answer






      StackExchange.ifUsing("editor", function ()
      StackExchange.using("externalEditor", function ()
      StackExchange.using("snippets", function ()
      StackExchange.snippets.init();
      );
      );
      , "code-snippets");

      StackExchange.ready(function()
      var channelOptions =
      tags: "".split(" "),
      id: "1"
      ;
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function()
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled)
      StackExchange.using("snippets", function()
      createEditor();
      );

      else
      createEditor();

      );

      function createEditor()
      StackExchange.prepareEditor(
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader:
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      ,
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      );



      );













      draft saved

      draft discarded


















      StackExchange.ready(
      function ()
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55368572%2fpowershell-command-to-get-a-file-that-has-the-highest-number-in-the-filename-str%23new-answer', 'question_page');

      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      0














      try this :



      Get-ChildItem "C:TempFile*.txt" | sort Name -Descending | select -First 1 | %

      $Newname="0:yyyy-MM-dd-HH-mm-ss-fffff(1:yyyy-MM-dd-HH-mm-ss-fffff)" -f $_.LastWriteTime, (Get-Date)
      Rename-Item $_.FullName -NewName $Newname






      share|improve this answer





























        0














        try this :



        Get-ChildItem "C:TempFile*.txt" | sort Name -Descending | select -First 1 | %

        $Newname="0:yyyy-MM-dd-HH-mm-ss-fffff(1:yyyy-MM-dd-HH-mm-ss-fffff)" -f $_.LastWriteTime, (Get-Date)
        Rename-Item $_.FullName -NewName $Newname






        share|improve this answer



























          0












          0








          0







          try this :



          Get-ChildItem "C:TempFile*.txt" | sort Name -Descending | select -First 1 | %

          $Newname="0:yyyy-MM-dd-HH-mm-ss-fffff(1:yyyy-MM-dd-HH-mm-ss-fffff)" -f $_.LastWriteTime, (Get-Date)
          Rename-Item $_.FullName -NewName $Newname






          share|improve this answer













          try this :



          Get-ChildItem "C:TempFile*.txt" | sort Name -Descending | select -First 1 | %

          $Newname="0:yyyy-MM-dd-HH-mm-ss-fffff(1:yyyy-MM-dd-HH-mm-ss-fffff)" -f $_.LastWriteTime, (Get-Date)
          Rename-Item $_.FullName -NewName $Newname







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Mar 27 at 2:40









          Esperento57Esperento57

          9,4622 gold badges19 silver badges28 bronze badges




          9,4622 gold badges19 silver badges28 bronze badges


























              0














              • Your (by default alphabetically) sorting approach only works with numbers of equal length.

              • more universal is using $ToNatural by Roman Kuzmin which prepends all numbers with zeroes to a unique length

              On my empty Ramdrive A: the following script:



              ## Q:Test2019327SO_55368572.ps1
              $SrcDir = 'A:Folder1' # 'C:TempWayneFolder1'
              $DstDir = 'A:Folder2' # 'C:TempWayneFolder2'

              ## create test folders,files
              MD $SrcDir,$DstDir | Out-Null
              1..5|New-Item -ItemType File -Path "0File1:D4.txt" -f $SrcDir,$_|Out-Null

              "_"*10+" Before "+"_"*10
              Tree A: /F

              Get-ChildItem -Path $SrcDir -Filter File*.txt |
              Sort-Object | Select-Object -Last 1 |
              Move-Item -Destination Join-Path $DstDir (
              "FileChanged(0:yyyy-MM-dd)1" -f (Get-Date),$_.Extension)

              "_"*10+" After "+"_"*10
              Tree A: /F

              Tree A: /F


              yields this (German locale) output:



              > Q:Test2019327SO_55368572.ps1
              __________ Before __________
              Auflistung der Ordnerpfade für Volume RamDisk
              Volumeseriennummer : 5566-7788
              A:
              ├───Folder1
              │ File0001.txt
              │ File0002.txt
              │ File0003.txt
              │ File0004.txt
              │ File0005.txt

              └───Folder2
              __________ After __________
              Auflistung der Ordnerpfade für Volume RamDisk
              Volumeseriennummer : 5566-7788
              A:
              ├───Folder1
              │ File0001.txt
              │ File0002.txt
              │ File0003.txt
              │ File0004.txt

              └───Folder2
              FileChanged(2019-03-27).txt





              share|improve this answer





























                0














                • Your (by default alphabetically) sorting approach only works with numbers of equal length.

                • more universal is using $ToNatural by Roman Kuzmin which prepends all numbers with zeroes to a unique length

                On my empty Ramdrive A: the following script:



                ## Q:Test2019327SO_55368572.ps1
                $SrcDir = 'A:Folder1' # 'C:TempWayneFolder1'
                $DstDir = 'A:Folder2' # 'C:TempWayneFolder2'

                ## create test folders,files
                MD $SrcDir,$DstDir | Out-Null
                1..5|New-Item -ItemType File -Path "0File1:D4.txt" -f $SrcDir,$_|Out-Null

                "_"*10+" Before "+"_"*10
                Tree A: /F

                Get-ChildItem -Path $SrcDir -Filter File*.txt |
                Sort-Object | Select-Object -Last 1 |
                Move-Item -Destination Join-Path $DstDir (
                "FileChanged(0:yyyy-MM-dd)1" -f (Get-Date),$_.Extension)

                "_"*10+" After "+"_"*10
                Tree A: /F

                Tree A: /F


                yields this (German locale) output:



                > Q:Test2019327SO_55368572.ps1
                __________ Before __________
                Auflistung der Ordnerpfade für Volume RamDisk
                Volumeseriennummer : 5566-7788
                A:
                ├───Folder1
                │ File0001.txt
                │ File0002.txt
                │ File0003.txt
                │ File0004.txt
                │ File0005.txt

                └───Folder2
                __________ After __________
                Auflistung der Ordnerpfade für Volume RamDisk
                Volumeseriennummer : 5566-7788
                A:
                ├───Folder1
                │ File0001.txt
                │ File0002.txt
                │ File0003.txt
                │ File0004.txt

                └───Folder2
                FileChanged(2019-03-27).txt





                share|improve this answer



























                  0












                  0








                  0







                  • Your (by default alphabetically) sorting approach only works with numbers of equal length.

                  • more universal is using $ToNatural by Roman Kuzmin which prepends all numbers with zeroes to a unique length

                  On my empty Ramdrive A: the following script:



                  ## Q:Test2019327SO_55368572.ps1
                  $SrcDir = 'A:Folder1' # 'C:TempWayneFolder1'
                  $DstDir = 'A:Folder2' # 'C:TempWayneFolder2'

                  ## create test folders,files
                  MD $SrcDir,$DstDir | Out-Null
                  1..5|New-Item -ItemType File -Path "0File1:D4.txt" -f $SrcDir,$_|Out-Null

                  "_"*10+" Before "+"_"*10
                  Tree A: /F

                  Get-ChildItem -Path $SrcDir -Filter File*.txt |
                  Sort-Object | Select-Object -Last 1 |
                  Move-Item -Destination Join-Path $DstDir (
                  "FileChanged(0:yyyy-MM-dd)1" -f (Get-Date),$_.Extension)

                  "_"*10+" After "+"_"*10
                  Tree A: /F

                  Tree A: /F


                  yields this (German locale) output:



                  > Q:Test2019327SO_55368572.ps1
                  __________ Before __________
                  Auflistung der Ordnerpfade für Volume RamDisk
                  Volumeseriennummer : 5566-7788
                  A:
                  ├───Folder1
                  │ File0001.txt
                  │ File0002.txt
                  │ File0003.txt
                  │ File0004.txt
                  │ File0005.txt

                  └───Folder2
                  __________ After __________
                  Auflistung der Ordnerpfade für Volume RamDisk
                  Volumeseriennummer : 5566-7788
                  A:
                  ├───Folder1
                  │ File0001.txt
                  │ File0002.txt
                  │ File0003.txt
                  │ File0004.txt

                  └───Folder2
                  FileChanged(2019-03-27).txt





                  share|improve this answer













                  • Your (by default alphabetically) sorting approach only works with numbers of equal length.

                  • more universal is using $ToNatural by Roman Kuzmin which prepends all numbers with zeroes to a unique length

                  On my empty Ramdrive A: the following script:



                  ## Q:Test2019327SO_55368572.ps1
                  $SrcDir = 'A:Folder1' # 'C:TempWayneFolder1'
                  $DstDir = 'A:Folder2' # 'C:TempWayneFolder2'

                  ## create test folders,files
                  MD $SrcDir,$DstDir | Out-Null
                  1..5|New-Item -ItemType File -Path "0File1:D4.txt" -f $SrcDir,$_|Out-Null

                  "_"*10+" Before "+"_"*10
                  Tree A: /F

                  Get-ChildItem -Path $SrcDir -Filter File*.txt |
                  Sort-Object | Select-Object -Last 1 |
                  Move-Item -Destination Join-Path $DstDir (
                  "FileChanged(0:yyyy-MM-dd)1" -f (Get-Date),$_.Extension)

                  "_"*10+" After "+"_"*10
                  Tree A: /F

                  Tree A: /F


                  yields this (German locale) output:



                  > Q:Test2019327SO_55368572.ps1
                  __________ Before __________
                  Auflistung der Ordnerpfade für Volume RamDisk
                  Volumeseriennummer : 5566-7788
                  A:
                  ├───Folder1
                  │ File0001.txt
                  │ File0002.txt
                  │ File0003.txt
                  │ File0004.txt
                  │ File0005.txt

                  └───Folder2
                  __________ After __________
                  Auflistung der Ordnerpfade für Volume RamDisk
                  Volumeseriennummer : 5566-7788
                  A:
                  ├───Folder1
                  │ File0001.txt
                  │ File0002.txt
                  │ File0003.txt
                  │ File0004.txt

                  └───Folder2
                  FileChanged(2019-03-27).txt






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Mar 27 at 17:42









                  LotPingsLotPings

                  23.1k6 gold badges16 silver badges33 bronze badges




                  23.1k6 gold badges16 silver badges33 bronze badges






























                      draft saved

                      draft discarded
















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid


                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.

                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function ()
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55368572%2fpowershell-command-to-get-a-file-that-has-the-highest-number-in-the-filename-str%23new-answer', 'question_page');

                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

                      Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

                      Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript