DonationCoder.com forum
 
Read-only archive. This is a static copy of the DonationCoder.com forum. Reading and searching work as before; logging in, posting and private messages do not.
Archive of 2026-10-21 · About this archive · Main site

Author Topic: DONE: In Single Filename Change Spaces To Underscores  (Read 8,255 times)

nkormanik

  • Participant
  • Joined in 2010
  • *
  • Posts: 563
DONE: In Single Filename Change Spaces To Underscores
« on: April 01, 2018, 07:42 AM »
Looking for a tiny program that can be used in a batch file as follows:

"(path\)ChangeSpace2Underscore.exe" "%1"

The original file will then have any spaces in name converted to underscores.  File will remain in same initial subdirectory.

Any help appreciated.

Nicholas Kormanik

4wd

  • Pedantic Old Bastard
  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 5,652
    • Coding Snacks
Re: In Single Filename Change Spaces To Underscores
« Reply #1 on: April 01, 2018, 08:23 AM »
From StackOverflow

s2u.cmd

Code: (text)
cmd /e:on /v:on /c "for %%f in ("%~1") do (set "n=%%~nxf" & set "n=!n: =_!" & ren "%%~ff" "!n!" )"

Usage: s2u.cmd %1

Example: s2u.cmd "t e s t.txt"
Result: t_e_s_t.txt

FYI
"I will not follow where the path may lead, but I will go where there is no path, and I will leave a trail." - Muriel Strode, 1903
« Last Edit: April 02, 2018, 01:00 AM by 4wd »

nkormanik

  • Participant
  • Joined in 2010
  • *
  • Posts: 563
Re: In Single Filename Change Spaces To Underscores
« Reply #2 on: April 01, 2018, 10:02 AM »
Wow, 4wd, no idea what you're doing there.  But it works!

And in such short time after my asking.

I placed your batch file into a special reserved location.  Created a shortcut to that batch file, and put it into my SendTo folder.  Then, selecting a file, right-click to bring up context menu, SendTo your batch file....  Presto.  Spaces changed to underscore, right where it sits.

Terrific job, 4wd.  Thanks a million!

Nicholas

P.S. -- Why this request to begin with?  A finicky program that won't accept being 'sent' a file if there are spaces in the filename, or even in the path.  Go figure.  But good now.

4wd

  • Pedantic Old Bastard
  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 5,652
    • Coding Snacks
Re: In Single Filename Change Spaces To Underscores
« Reply #3 on: April 01, 2018, 10:43 AM »
Wow, 4wd, no idea what you're doing there.

I didn't do anything except a search and repackage into a stand alone command file.

I would have done it as a short PowerShell script but why re-invent the wheel?
"I will not follow where the path may lead, but I will go where there is no path, and I will leave a trail." - Muriel Strode, 1903
« Last Edit: April 01, 2018, 11:32 AM by 4wd »

skwire

  • I eat Coding Snacks for breakfast...
  • Global Moderator
  • Joined in 2005
  • *****
  • Posts: 5,297
    • Participated in NANY Challenge Seventeen Times
    • Coding Snacks
    • Superboy Coder
    • Cody Award
    • Volunteers/Staff
    • Gathering Badge
Re: In Single Filename Change Spaces To Underscores
« Reply #4 on: April 03, 2018, 07:18 PM »
Thanks, 4wd.  :Thmbsup:  I'll mark this done and move it.

Original address: https://www.donationcoder.com/forum/index.php?topic=45298.0