Everyday tasks
Replace Spaces in Filenames with Underscores or Hyphens
Normalize separators before sharing a folder or using filenames in a project. Choose whether each space becomes an underscore, a hyphen or nothing, then check repeated spaces and naming collisions before applying the change.
Replace spaces with underscores
- Before
- Quarterly Report 2026.pdf
- After
- Quarterly_Report_2026.pdf
- Before
- draft final.txt
- After
- draft__final.txt
Replaces every ordinary space with an underscore. Two consecutive spaces become two underscores; other punctuation and extensions are unchanged.
Review the rules, then preview sample filenames without changing real files.
Replace every ordinary space with an underscore
Use Find & Replace for a literal space. The Find field contains one press of the space bar, not the word space or the two characters \s. Enable Replace all to change every occurrence.
The Try this example button opens this exact rule. Confirm Try with sample filenames to see both a normal name and a name containing two consecutive spaces. No file access is needed for this test.
- 1Set Scope to Name so extensions remain unchanged.
- 2Add Find & Replace. Enter one ordinary space in Find and _ in Replace with.
- 3Enable Replace all and leave By position off.
- 4Check the preview. Two spaces become two underscores because this is a character-for-character replacement.
Quarterly Report 2026.pdfQuarterly_Report_2026.pdfdraft final.txtdraft__final.txtChoose hyphens or remove spaces entirely
For hyphens, change Replace with to -. To remove ordinary spaces, leave Replace with empty. Neither setting changes the letter case or other punctuation.
Removing separators can make names harder to read: My Report becomes MyReport. Keep the distinction between replacing spaces and converting to snake_case; case conversion can also split words and change existing separators.
My Report.pdfMy-Report.pdfFind one ordinary space; replace all with -.
My Report.pdfMyReport.pdfFind one ordinary space; replace all with empty text.
Collapse repeated whitespace with regex
If you want several consecutive whitespace characters to become one separator, use Regex Replace instead of the literal rule: pattern \s+, replacement _, flags g. It matches whitespace runs, including tabs where the filesystem permits them.
A leading or trailing run also becomes an underscore. To remove surrounding whitespace first, put a separate Regex Replace rule before it: pattern ^\s+|\s+$, empty replacement, flags g. This trimming step is optional and is not included in the basic example button.
draft final.txtdraft_final.txtOnly Regex Replace: \s+ → _, flags g.
My Report .pdfMy_Report.pdfFirst trim ^\s+|\s+$ with empty replacement and flags g, then \s+ → _ with flags g.
Review collisions and mixed naming styles
Report Final.pdf and Report_Final.pdf both end up as Report_Final.pdf when spaces become underscores. Select a different label, exclude one file, or resolve the conflict in the preview before executing. Collapsing whitespace can create similar collisions.
This cleanup changes filenames, not folder names or document contents. A Name-scoped rule preserves .pdf and other extensions; it does not convert file formats. Existing references to old filenames may need updating.
After testing, clear the sample list and import your real files in a supported desktop browser such as Chrome or Edge. Review the complete batch and execute only when the names are correct. If direct file access is unavailable, use sample names to generate a script, review it, and run it in the corresponding folder.
Ready to try the workflow?
Open Rename.Tools, add a few sample files, and preview every rule before touching the real filenames.