Renaming script

iam looking for a PERL script to goto a folder and rename all the files in it.can some one help me please?

I could possibly help you ...

How would you like to rename them? Change an extension? Number them?

How do you know which folder? Pre-defined? Ask at run-time?

I would like to rename the file name (e.g test1.txt to test.txt)
for the folder, ask at run time. ( can you also tell me the command for a pre-defined please)

thanks

What system is it for? Unix or Windows? And what's it for, by-the-way?

its for win98 or unix. its basicly to get the file in the folder and rename them,so i can put others in with that same name (before the others were renamed) so that if theres a problem with the new files i put in, i simply rename d others back to d original

Haven't quite had the chance to properly test this - and it's aimed at Windows machines; but here you go! Give it a try and let me know!

...

See below for improved WORKING version! :slight_smile:

getting loads of errors!!!! any other idea

What type of errors are you getting?

bareword found line 23 near "system" copy"
scalar found found line 23 near ${trackFolder}\

and stuff like that

I made 1 mistake; so sue me ... I am doing you a favour! I have checked it and it worked on WinME!!! It's all due to the fact that Windows uses the "\" to define a folder in a path, so in Perl you have to use two of them (like: \\). This should now work:

#!/perl/bin/perl

# --------------------------------------------------------------
# Creates a backup of each file in a specified folder

#set this to the folder you want check
$trackFolder = "c:\\perl_tmp";

  opendir(TARGETFOLDER, $trackFolder);
  @files = readdir(TARGETFOLDER);
  closedir(TARGETFOLDER);

  
  $found = 0;
  

  foreach $targetFile (@files) {	  
	  # append the number 1 to any file which doesn't have it
      if ($targetFile =~ m/^1/) {
        $found = 1;
      }

    if ($found == 0) {
      system "copy ${trackFolder}\\${targetFile} ${trackFolder}\\1${targetFile}";
    }

  }

getting a error on line 2. says sumtin about predecement (--) on $trackFolder =

Weird ?

Are you running it in Windows?

How are you starting the script?

Have you correctly altered the line:
$trackFolder = "c:\\perl_tmp";
?

Sounds very weird though!

iam running in windows. The directory is correct. is there any other of doing it??

I tried the script too and I got similar errors, as follows:
<B>
Can't modify predecrement (--) in predecrement (--) at rn.pl line 8, near "$trackFolder ="
Execution of rn.pl aborted due to compilation errors.</B>

I'm sorry to hear that you are having trouble - I tried my script at work on my Win98 box and it still works for me.

Could you tell me how you are copying the text for the script above? Copy and paste? Are you saving it as a "plain text" file?

Ok I realized why I got those errors. I copied the script incorrectly from the post...including the line:
code:--------------------------------------------------------------------------------

The next line of actual code was
$trackFolder = "c:\\temp\\ptmp"; #or whatever you set it to

so perl thought I was trying to pre-decrement the variable $trackfolder. If you comment out the first line, the script will work.

yea, i got no errors. but nottings happed to the file in my folder.When i run it. i get .....testfile.txt (thats the name of the file in the folder) but notting else happens

perleo,

How exactly are you running it? What command are you using?

running it from windows.i have perl builder. saved it as rename.pl and go to run it from windows explorer and thats what happens

Have you tried running it from the command line? Try it like this:

perl rename.pl