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!

Recommended Answers

All 4 Replies

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....

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. :icon_biggrin:

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).

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.