

elseif ( Test-Path ($pathDocsRename + $newFileName + $fileExtension)) # Check if file has already been renamed. # Add message in notes column stating no new file name provided. # Check if new file name has been provided. # Add message in notes column stating no new valid name provided. # Add message in notes column stating file can't be renamed. # Add message in notes column stating file has been renamed.
:max_bytes(150000):strip_icc()/Batch_Rename_Files_08-3415890854e04d9a9b945967c99d170d.jpg)
($pathDocsRename + $currentFileName + $fileExtension) -NewName ` if ($newFileName.IndexOfAny(::GetInvalidFileNameChars()) ` $newFileName = $newFileName + $fileExtension if (( Test-Path ($pathDocsRename + $currentFileName + $fileExtension)) -and ` # Check if the file exists and a new name has been provided. # Process the rows, from second row to the last used.

# Message stating files are being processed. Write-Host "There are no files to rename." # Check if there are any files to rename. Write-Host "Unable to open file names workbook." # Message stating that workbook can't be opened. # Open the file names workbook and assign to a variable. if (( Test-Path $pathNamesDoc) -and ( Test-Path $pathDocsRename)) # Check if the file and folder paths exist. $pathNamesDoc = "C:\Demo\Rename\Documents to rename.xlsx" $excel = New-Object -ComObject Excel.Application Overall feedback is also given as to the total number of files renamed. Feedback is added in column 3 as to whether the file was renamed or not, or, if it had already been renamed. A check is made to see if the file exists with the current name and if it does the file gets renamed to the new name, providing there is one available. The current and new file names are extracted from columns ‘A’ and ‘B’, using their numerical references, 1 and 2. The last row used is obtained and all the rows, from the second to the last are handled one by one. If they do exist, the document containing the file names is opened and the file name information on the first sheet is processed. Existing File Nameįirst of all, the path to the above document, as well as to the files to be renamed, is set and a check is made to see if they exist. Column ‘A’ contains the existing file name, without the file extension, column ‘B’ contains the new file name, if available, without the extension, and column ‘C’ is for notes to be added during the renaming process. In the example below, a spreadsheet called “Documents to rename.xlsx” contains the file name information. If it is necessary to rename a batch of files, where both the existing and new file names exist in an Excel spreadsheet, PowerShell can be used to automate the renaming process. To rename all my files in a folder called blog I navigate to the folder in question and wrote this in PowerShellĭir | %.text and incrementing the number so we can name the files MyFile1.txt, MyFile2.Batch File Search and Renaming with PowerShell The script I’m working on is rather long, and not finished, so for now I’ll just share this handy little snippet. Quite often I find myself having to rename a bunch of files in a folder, and what better way to do this than PowerShell? This simple one-liner is also a great way to learn a few new PowerShell tricks, or rather- get familiar with the syntax and see how flexible PowerShell is.
