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;
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
add a comment |
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
Please format your code and sample input/output properly.
– mklement0
Mar 27 at 1:53
add a comment |
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
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
powershell
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
add a comment |
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
add a comment |
2 Answers
2
active
oldest
votes
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
add a comment |
- 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
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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
add a comment |
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
add a comment |
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
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
answered Mar 27 at 2:40
Esperento57Esperento57
9,4622 gold badges19 silver badges28 bronze badges
9,4622 gold badges19 silver badges28 bronze badges
add a comment |
add a comment |
- 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
add a comment |
- 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
add a comment |
- 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
- 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
answered Mar 27 at 17:42
LotPingsLotPings
23.1k6 gold badges16 silver badges33 bronze badges
23.1k6 gold badges16 silver badges33 bronze badges
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
Please format your code and sample input/output properly.
– mklement0
Mar 27 at 1:53