0

The action I am supposed to do:

  1. Read a file from a folder
  2. Extract a date from the 1st line
  3. Change it to 'yyyymmdd' format
  4. Rename all the file by removing first 4 characters and place this format date at the beginning.

Example FILE.FILE will be changed to 20180725.FILE (The date will be in the file)

I am able to accomplish this to a single folder in Newpath by the following code:

$path="\\Data\DEV\DevDat\Arun\DXSL\Newpath\20170601"

$files=Get-ChildItem $path  | Select-Object -first 1

$data = Get-Content "$path\$files" -first 1
foreach($line in $data)
 {
 $arr = $line.substring(52,4)+$line.substring(46,2)+$line.substring(49,2)
}
get-childitem $path | rename-item -newname { $arr +[string]($_.name).substring(4)}

However, I am not able to replicate this action to the other folders in a loop. The Newpath folder has several sub-folders. I need to rename the files inside each sub-folder inside Newpath. Is there any way to achieve this?

FYI I'm using Version 4 of powershell.

Arun
  • 65
  • 1
  • 9
  • You will probably find some relevant code in these similar questions: https://stackoverflow.com/q/21611551/478656, https://stackoverflow.com/q/40411126/478656, https://stackoverflow.com/q/42356769/478656, https://stackoverflow.com/q/31053940/478656, https://stackoverflow.com/q/46045626/478656, https://stackoverflow.com/q/42356769/478656 – TessellatingHeckler Jul 25 '18 at 15:46
  • Extract a date in what format, position in the first line? [edit] your question to contain some more details. in general first iterate the folders, then the files, then read content and get the date with a RegEx possibly `(?\d{2}).(?\d{2}).(?\d{4})` –  Jul 25 '18 at 15:48
  • @LotPings. The issue is not in extracting the date. It's in replicating the results to the other folders automatically in a loop. I'm able to achieve my desired results for the folder 20170601 since I have explicitly mentioned in the $path variable. However, there are other folders 20170602, 20170603 and so on... for which I need a loop that will do the above modification – Arun Jul 26 '18 at 06:22

1 Answers1

0

A PowerShell script as suggested in my comment.

  • It does not check if the file to rename to already exists.
  • The Rename-Item has the parameer -WhatIf appended, so it only shows what would be done.
  • If there are more numbers in the first line, the script will match the first pattern.
  • The script also does not check if the file already is renamed to the pattern.

## Q:\Test\2018\07\25\SO_51522205.ps1
#Requires -Version 3.0
$BasePath = "\\Data\DEV\DevDat\Arun\DXSL\Newpath"

$RE = [RegEx]'(?<Month>\d{2}).(?<Day>\d{2}).(?<Year>\d{4})'

ForEach ($Folder in (Get-ChildItem -Path "$BasePath\*" -Directory)){
    ForEach ($File in (Get-ChildItem -Path "$($Folder.FullName)\*" -File)){
        $Line1 = (Get-Content $File.FullName | Select-Object -First 1)
        If ($File.BaseName.Length -gt 4){
            $BaseName = $File.BaseName.SubString(4)
        } else {
            $BaseName = ''
        }
        If ($Line1 -match $RE){
            $NewName = ("{0}{1}{2}{3}{4}" -f `
                $Matches.Year,
                $Matches.Month,
                $Matches.Day,
                $BaseName,
                $File.Extension)
            $File | Rename-Item -NewName $NewName -WhatIf
        } Else {
            "{0} doesn't have a proper date in 1st line" -f $File.FullName
        }
    }
}

Sample tree before,

> tree /F
└───Newpath
    └───20170601
            blahblah.txt
            FILE.FILE

after running the script.

> tree /F
└───Newpath
    └───20170601
            20180724blah.txt
            20180725.FILE