Discussion Forums > Technology

Windows Scripts for Adding & Removing Spaces in Filenames?

<< < (3/3)

ph4zr:

--- Quote from: Baby Naruto on May 15, 2011, 05:42:20 PM ---I want a script that can remove all of those extra spaces between You and A, but it leaves one space in/alone.

--- End quote ---
Makes sense. Grats on solving the problem. But, one last thought: you could also just collapse multiple spaces/underscores into a single one, this way you don't lose the meaning of such word breaks.

bork:
I know the original question was for a Windows script but if anyone is interested in a Unix/Linux flavor:

(click to show/hide)
--- Code: ---


This script does the directory names, change the test command to file names and off you go.


#! /usr/local/bin/bash
#
# Removes space characters and replaces them with an underscore
# in the directories names in the directory you are currently in.
#
#  The script as is, does not do anything but prints out
#  the the changes that would have happened if the mv command
#  was uncommented.  This is a safe guard to prevent a major
#  screw-up.  Make sure it is functioning correctly before
#  you trash your file names.
#

ls |
 while read name
 do
   if test -d "$name"
   then
     new_name="`echo $name | sed 's/ \{1,\}/_/g'`"
     if [ "$name" != "$new_name" ]
     then
#       mv "$name" "$new_name"
       echo "$name"    +++    "$new_name "
     fi
   fi
 done



--- End code ---

rkruger:

--- Quote from: bork on May 16, 2011, 02:37:54 AM ---I know the original question was for a Windows script but if anyone is interested in a Unix/Linux flavor:

(click to show/hide)
--- Code: ---


This script does the directory names, change the test command to file names and off you go.


#! /usr/local/bin/bash
#
# Removes space characters and replaces them with an underscore
# in the directories names in the directory you are currently in.
#
#  The script as is, does not do anything but prints out
#  the the changes that would have happened if the mv command
#  was uncommented.  This is a safe guard to prevent a major
#  screw-up.  Make sure it is functioning correctly before
#  you trash your file names.
#

ls |
 while read name
 do
   if test -d "$name"
   then
     new_name="`echo $name | sed 's/ \{1,\}/_/g'`"
     if [ "$name" != "$new_name" ]
     then
#       mv "$name" "$new_name"
       echo "$name"    +++    "$new_name "
     fi
   fi
 done



--- End code ---

--- End quote ---

For people wanting to learn more, let me complement that with a bash one-liner that performs the same task:

--- Code: ---
shopt -s extglob; for i in *; do mv -i "$i" "${i//+( )/ }"; done

--- End code ---

Pants:
GET THIS!
I love this thing, have used it for years, and nothing I could ever say could embody how awesome it is. No installs - it just runs and works. Sure, it isn't very pretty, but it's a hell of a workhorse.

bork:
shopt -s extglob; for i in *; do ln -i "$i" "${i//+( )/ }"; done

Have the best of both now.   ;D

Navigation

[0] Message Index

[*] Previous page

Go to full version