VBA & Macro Problems

Copy and Rename Files from Excel Part Numbers

Kushani NimanthikaKushani Nimanthika Nov 18, 2026 869 views

Question details

Create a renamed copy of one JPG file for each part number in an Excel column, preserving leading zeros.

Copy and Rename Files from Excel Part Numbers
Product
Microsoft Excel
Device & OS
not provided
Scenario
A column contains part numbers, and each value should become the filename of a copy of BlankImage.jpg.
Observed behavior
Each output file should use the exact part number followed by .jpg, such as 001.001.00.jpg.
Before you start

The PowerShell method below requires Windows. Check that the part numbers are stored as text and that BlankImage.jpg exists; if leading zeros have already been lost from the underlying values, restore them before continuing.

Solution 1Recommended

Use an Excel list and PowerShell to copy and rename the JPG

Use this method on Windows to create one file per part number without enabling Excel macros.

1
Check the part numbers

In Excel, select the part-number column and choose Home > Number Format > Text before entering or importing the values. Confirm that values such as 001.001.00 appear exactly as required. Formatting existing values as text does not restore zeros that have already been lost.

2
Create a filename list

Select the part-number cells without the header and press Ctrl+C. Open Notepad, paste the values, and use File > Save As to save them as C:\parts.txt with UTF-8 encoding. Use one part number per line.

3
Run the copy script

Open Windows PowerShell, paste the script below, and press Enter. Change the three paths if your source, list, or destination is elsewhere. The destination's parent folder must already exist. $source = 'C:\BlankImage.jpg' $list = 'C:\parts.txt' $destination = 'C:\Images' if (-not (Test-Path -LiteralPath $source -PathType Leaf)) { throw 'Source image not found.' } if (-not (Test-Path -LiteralPath $list -PathType Leaf)) { throw 'Part-number list not found.' } New-Item -ItemType Directory -Path $destination -Force | Out-Null $invalid = [System.IO.Path]::GetInvalidFileNameChars() foreach ($part in Get-Content -LiteralPath $list -Encoding UTF8) { if ([string]::IsNullOrWhiteSpace($part)) { continue } if ($part.IndexOfAny($invalid) -ge 0 -or $part -match '[. ]$' -or $part -match '^(CON|PRN|AUX|NUL|COM[1-9]|LPT[1-9])(\.|$)') { Write-Warning "Invalid filename: $part" continue } $target = Join-Path $destination ($part + '.jpg') try { [System.IO.File]::Copy($source, $target, $false) Write-Host "Created: $target" } catch { Write-Warning "Could not create $target : $($_.Exception.Message)" } }

4
Check the output

Open C:\Images in File Explorer and check the filenames. Review PowerShell warnings for invalid names, duplicate part numbers, existing files, or other copy errors.

Use an Excel list and PowerShell to copy and rename the JPG
Existing files are not overwritten: The script reports an error if an output file already exists. Correct the list or choose a different destination before running it again.
Work with part-number lists in WPS

Prepare part numbers in WPS Spreadsheets

Use WPS Spreadsheets to prepare the text-based part-number list, then use the Windows PowerShell script above to create the renamed image copies.

  1. 1. Open the workbook: Open WPS Spreadsheets and use File > Open to select the Excel workbook containing the part numbers.
  2. 2. Verify the values: Select the part-number cells and check that each value displays its required leading zeros. Set the cells to Text before entering or importing values; restore any zeros already lost from the underlying data.
  3. 3. Save the list: Copy the part-number cells without the header, paste them into Notepad, and use File > Save As to create C:\parts.txt in UTF-8 encoding.
  4. 4. Create the image copies: Open Windows PowerShell and run the script in the solution above, adjusting the source, list, and destination paths as needed. Check the destination folder and any warnings.
Prepare part numbers as text to preserve leading zeros.Open and edit Microsoft Excel XLSX and XLS files.Use a familiar spreadsheet interface to review the filename list.
microsoft office alternative - wps office

Frequently Asked Questions

How do I keep leading zeros in my part numbers?

In Excel, set the cells to Text before entering or importing the part numbers. Check the underlying values before copying them to Notepad; changing the format afterward cannot restore zeros that were already removed.

Will the script overwrite images that already exist?

No. The script uses File.Copy with overwrite set to false. PowerShell displays a warning when the destination filename already exists.

Why was a part number skipped?

Blank lines are skipped. PowerShell also warns about invalid Windows filenames, existing destination files, and copy errors; review the warning and correct the part number or destination.

Can I use a different source image or output folder?

Yes. Change the $source and $destination paths at the top of the PowerShell script. If the source is not a JPG, also change the '.jpg' extension used when constructing each target filename.