2

I have a question with powershell script. I have a daily task scheduler and it exports the file with the name 12122_000000.txt and store it on C:\ drive. I'm also have another Report folder at C:\Report. In that Report folder, I have a lot of file with the name 12122_000001.txt, 12122_000002.txt, 12122_0000003.txt,12122_0000004.txt and so forth.

I would like to have a script that will check the Report folder to find the latest file name. Then, moving the file name 12122_000000.txt on C:\ to C:\Report and rename it with the highest name increase 1. The format of filename must be 12122_xxxxxx.txt

I have tried to write the script but it didn't work. Please help me to gain my knowledge of powershell.

$SourceFile = "C:\12122_000000.txt"
$DestinationFolder = "C:\Report"
if (Test-Path $SourceFile)
{ 
$latest = Get-ChildItem -Path $DestinationFolder| Sort-Object Name -Descending | Select-Object -First 1
$i = 1
Move-Item -path $SourceFile -destination $latest.basename + $i++ + ".txt")
}

Thanks

Long Truong
  • 23
  • 1
  • 1
  • 3
  • 1
    I think what you need is here [http://stackoverflow.com/questions/10287762/powershell-script-filename-increment](http://stackoverflow.com/questions/10287762/powershell-script-filename-increment) – Mr Deans Aug 31 '16 at 15:26
  • I have tried it but it is not my case. Thanks – Long Truong Aug 31 '16 at 15:41
  • The jist of what you are trying to do is covered in that answer. It show increment numbers as well as being padded with zeros. You will have to adapt it to your code obviously. In the future please explain what _it didn't work._ means – Matt Aug 31 '16 at 16:04
  • I don't think this is quite a duplicate of that question, as this does not appear to be a bulk rename. While some of the principles could be applied, the accepted answer below is very different than the accepted answer of the "duplicate" question. – morgb Aug 31 '16 at 17:09
  • Hi Matt I don't think this is duplicate that answer. I wrote my first script and I didn't know why it is not work. I just need someone can help me to revise my script . – Long Truong Aug 31 '16 at 18:26

3 Answers3

7

Here is an easy way to increment this filename and move the file:

$SourceFile = "C:\12122_000000.txt"
$DestinationFolder = "C:\Report"
if (Test-Path $SourceFile)
{ 
    $latest = Get-ChildItem -Path $DestinationFolder| Sort-Object Name -Descending | Select-Object -First 1
    #split the latest filename, increment the number, then re-assemble new filename:
    $newFileName = $latest.BaseName.Split('_')[0] + "_" + ([int]$latest.BaseName.Split('_')[1] + 1).ToString().PadLeft(6,"0") + $latest.Extension
    Move-Item -path $SourceFile -destination $DestinationFolder"\"$newFileName
}
morgb
  • 2,252
  • 2
  • 14
  • 14
  • You're the man. This is very simple and easy way to do it. – Long Truong Aug 31 '16 at 16:12
  • @morgb I have folder name like " Project_setup_v2019.0.zip". I want to increment "0" to 1,2,3..... PLeases suggest me..Above mentioned is not working for me –  Nov 26 '19 at 09:58
  • @Stella - just change the character you're splitting on to '.' instead of '_', and remove the padding - something like: $newFileName = $latest.BaseName.Split('.')[0] + "." + ([int]$latest.BaseName.Split('.')[1] + 1).ToString() + $latest.Extension – morgb Nov 29 '19 at 15:49
1

You could scan your target directory to extract the list of all the files indexes, use the Measure-Object CmdLet to identify the maximum index, add one and then move the file.

I did this to setup a test env.:

$rootDirectory = "C:\test"
$filePrefix = "12122_"
$fileExt = ".txt"
$filePathFmt = "{3}\{1}{0,6:000000}{2}"
@(1..12) | % { New-Item ([String]::Format($filePathFmt, $_, $filePrefix, $fileExt, $rootDirectory)) -Type File } | Out-Null
New-Item ([String]::Format($filePathFmt, 0, $filePrefix, $fileExt, "C:")) -Type File | Out-Null

This to mode the 0 indexed file to the target directory:

$nextIdx = (Get-ChildItem $rootDirectory -Filter "${filePrefix}*${fileExt}" | Select-Object -ExpandProperty Name | ForEach-Object { [int]($_.Replace($filePrefix,"").Replace($fileExt, "")) } | Measure-Object -Maximum | Select-Object -ExpandProperty Maximum) + 1
Move-Item ([String]::Format($filePathFmt, 0, $filePrefix, $fileExt, "C:")) ([String]::Format($filePathFmt, $nextIdx, $filePrefix, $fileExt, "$rootDirectory"))

It output:

PS C:\> $rootDirectory = "C:\test"
PS C:\> $filePrefix = "12122_"
PS C:\> $fileExt = ".txt"
PS C:\> $filePathFmt = "{3}\{1}{0,6:000000}{2}"
PS C:\> @(1..12) | % { New-Item ([String]::Format($filePathFmt, $_, $filePrefix, $fileExt, $rootDirectory)) -Type File } | Out-Null
PS C:\> New-Item ([String]::Format($filePathFmt, 0, $filePrefix, $fileExt, "C:")) -Type File | Out-Null
PS C:\> Get-ChildItem $rootDirectory | Select-Object -ExpandProperty FullName
C:\test\12122_000001.txt
C:\test\12122_000002.txt
C:\test\12122_000003.txt
C:\test\12122_000004.txt
C:\test\12122_000005.txt
C:\test\12122_000006.txt
C:\test\12122_000007.txt
C:\test\12122_000008.txt
C:\test\12122_000009.txt
C:\test\12122_000010.txt
C:\test\12122_000011.txt
C:\test\12122_000012.txt
PS C:\> $nextIdx = (Get-ChildItem $rootDirectory -Filter "${filePrefix}*${fileExt}" | Select-Object -ExpandProperty Name | ForEach-Object { [int]($_.Replace($filePrefix,"").Replace($fileExt, "")) } | Measure-Object -Maximum | Select-Object -ExpandProperty Maximum) + 1
PS C:\> Move-Item ([String]::Format($filePathFmt, 0, $filePrefix, $fileExt, "C:")) ([String]::Format($filePathFmt, $nextIdx, $filePrefix, $fileExt, "$rootDirectory"))
PS C:\> Get-ChildItem $rootDirectory | Select-Object -ExpandProperty FullName
C:\test\12122_000001.txt
C:\test\12122_000002.txt
C:\test\12122_000003.txt
C:\test\12122_000004.txt
C:\test\12122_000005.txt
C:\test\12122_000006.txt
C:\test\12122_000007.txt
C:\test\12122_000008.txt
C:\test\12122_000009.txt
C:\test\12122_000010.txt
C:\test\12122_000011.txt
C:\test\12122_000012.txt
C:\test\12122_000013.txt
PS C:\>
Gzeh Niert
  • 148
  • 8
0

The $i variable should be derived from the actual filename stored in $latest:

$prefix, $i = $latest.baseName -split '_'
$i = '{0:d6}' -f ([int]$i + 1)

The parameter should be enclosed in parentheses because it's an expression:

Move-Item $SourceFile -dest (Join-Path $DestinationFolder "$prefix`_$i.txt")
wOxxOm
  • 65,848
  • 11
  • 132
  • 136