Rename multiple files, changing spacing

Please support our Computer Science advertiser: Learn about neural networks and artificial intelligence.
Reply

Join Date: Mar 2006
Posts: 5
Reputation: Greatoutdoors is an unknown quantity at this point 
Solved Threads: 0
Greatoutdoors Greatoutdoors is offline Offline
Newbie Poster

Rename multiple files, changing spacing

 
0
  #1
Jul 23rd, 2009
In Windows XP I have several files in this format: text-### ###.ext
I want to change all of them to this format: text_###-###.ext

So, I need to replace all dashes in the file name with underscores, and replace all spaces with dashes.

I know there are apps for download that do this, but how hard is it to do it myself? Can someone advise if this can be done from the Command Line and, if so, advise how to go about it?

Thanks!
Last edited by Greatoutdoors; Jul 23rd, 2009 at 12:06 pm.
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: Rename multiple files, changing spacing

 
0
  #2
Jul 23rd, 2009
Do you have a useful scripting language installed on your machine (perl, python, ruby, etc)?

Do you have any experience in any of those languages?

> but how hard is it to do it myself?
Somewhere between trivial and impossible.

> Can someone advise if this can be done from the Command Line and, if so, advise how to go about it?
After nearly 30 years, M$ command shell syntax is still a sucking void. Doing anything remotely more interesting that "dir" is an exercise in pointless frustration.

Hence the comment about useful scripting languages....
Reply With Quote Quick reply to this message  
Join Date: Mar 2006
Posts: 5
Reputation: Greatoutdoors is an unknown quantity at this point 
Solved Threads: 0
Greatoutdoors Greatoutdoors is offline Offline
Newbie Poster

Re: Rename multiple files, changing spacing

 
0
  #3
Jul 24th, 2009
Originally Posted by Salem View Post
Do you have a useful scripting language installed on your machine (perl, python, ruby, etc)?
Only if a Java jdk counts.

Do you have any experience in any of those languages?
I have a beginner familiarity with Java and have heard of the others. I would like to get my feet wet in actual programming (as opposed to school assignments) and thought this might be a good place to start.

Somewhere between trivial and impossible.
I think I know what you mean -- as in "Nothing works quite like you plan it." I thought I had the solution to my issue when I found that Windows Explorer has a multiple file name change capability. Then I realized that was only if you wanted to add a number to files to differentiate them. Not what I would like to do.

After nearly 30 years, M$ command shell syntax is still a sucking void. Doing anything remotely more interesting that "dir" is an exercise in pointless frustration. Hence the comment about useful scripting languages....
Yes, it can be frustrating. My first operating system was DOS -- DOS 3 I think; anyway, pre-5. It was fun because of the rush I got when something worked the way I actually planned it. But it was also extremely frustrating when things did not go as planned -- "File not found" "Command not recognized" -- that kind of thing. The present-day command line seems to offer greater control in some areas, but still only when it works as planned.
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: Rename multiple files, changing spacing

 
0
  #4
Jul 24th, 2009
Well you might be able to do it in Java, don't ask me how.

You'll need some kind of directory class to list the filenames, and maybe even rename them.

You'll also need some kind of string manipulation class to do the substitutions to get from the old filename to the new filename.


Personally, I'd use bash
for i in text*.ext; do
  b=`echo $i | sed 's/-/_/g'`
  b=`echo $b | sed 's/ /-/g'`
  mv "$i" $b
done

But feel free to practice the Java approach - it's all good learning when it comes to solving actual problems (rather than bookwork).
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 9
Reputation: prime1999 is an unknown quantity at this point 
Solved Threads: 0
prime1999 prime1999 is offline Offline
Newbie Poster

Re: Rename multiple files, changing spacing

 
0
  #5
Jul 26th, 2009
I doubt it can be done with the command-line, but it could be done with the WSH, which comes preinstalled.
var fso,a,s,d,f,t,t0,t1;
var dr;
dr='c:\\mydocu~1\\test';
fso=new ActiveXObject('scripting.filesystemobject');
d=fso.getfolder(dr);
f=new Enumerator(d.files);
t=[];
for(;!f.atEnd();f.moveNext())
{
	t0=f.item().name;
	if( t0.indexOf(' ')==-1 ) continue;
	t1=t0.replace(/-/g,'_').replace(/ /g,'-');
	if( t0!=t1 ) t.push([t0,t1]);
}
for(a=0;a<t.length;a++)
{
	fso.movefile(dr+'\\'+t[a][0],dr+'\\'+t[a][1]);
}

Copy the above and paste it into a file with extension '.js', then change the variable dr to your directory's name without an ending backslash -- also remember to escape the backslashes. Then double-click the '.js' file.
Last edited by prime1999; Jul 26th, 2009 at 10:36 am.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC