0

I'm trying to rename the file from one format to other format.. 500+ files. Currently with notepad++ manually do it with individual file name. Is there any command or scripts is there, Please let me know, much appreciate all your help on this.

DB2.V12.RPT(DDG1RP01)   
DB2.V12.RPT(DDG1RP02)   
DB2.V12.RPT(DDG1RP03)   
DB2.V12.RPT(DDG1RP04)   

want to rename into

ren DB2.V12.RPT(DDG1RP01)      DDG1RP01.txt
ren DB2.V12.RPT(DDG1RP02)      DDG1RP02.txt
ren DB2.V12.RPT(DDG1RP03)      DDG1RP03.txt
ren DB2.V12.RPT(DDG1RP04)      DDG1RP04.txt

that is, remove these "DB2.V12.RPT(" and ")' and rename into .txt file.

1
  • in NP++ it's a straightforward regular expression substitution to change your file list into to the list of ren commnds. Having done this, save as a .cmd file and call it from cmd.
    – AFH
    Commented Jun 12, 2019 at 14:08

1 Answer 1

0

You can do it in powershell pretty easy. Copy this and save it as a .ps1 file. Change the paths to match what you have. Change the first line to match the directory your files are in and then the $location variable about halfway down to be the same thing. Note the first one does not have a trailing slash but the $location does.

#get all files in this directory
$files = Get-ChildItem("d:\tempfiles")

#Loop through each filename and rename it leaving only the value in parentheses and then adding .txt to the end
foreach ($file in $files)
{

 $filename = $file.ToString()
 #replace first half of filename up to first parenthese with nothing
 $replace1 = $filename.Replace("DB2.V12.RPT`(" , "")

 #replaces the last parenthese with .txt
 $replace2 = $replace1.ToString().Replace("`)" , ".txt")

 $location = "d:\tempfiles\"

 $oldFilename = $location + $filename
 $newFilename = $location + $replace2

 Write-Host($oldFilename)
 Write-Host($newFilename)

 Rename-Item -Path $oldFilename -NewName $newFilename 

}

This: Files Before

Will turn into This: Files After

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .