rename command

louislsh

Senior Member
Joined
Sep 13, 2006
Messages
713
Reaction score
2
Hi guys, I am trying to use command to rename a file in my C drive.
The file is C:\12345612092013.txt

I am trying to use command to change it to 123456_12092013.txt
I use command rename c:\12345612092013.txt 123456_12092013.txt
I am able to rename the file.

However I am going to schedule it to auto rename the file nightly.
And the 12092013 is a running date.
I'm afraid if the command fails to run, I will rename older files with new date.
Any command to like keep intact the filename, just adding a _ to it?
example 123456XXXXXXXX.txt change to 123456_XXXXXXXX.txt

Thank you for viewing.
 

davidktw

Arch-Supremacy Member
Joined
Apr 15, 2010
Messages
13,547
Reaction score
1,300
Hi guys, I am trying to use command to rename a file in my C drive.
The file is C:\12345612092013.txt

I am trying to use command to change it to 123456_12092013.txt
I use command rename c:\12345612092013.txt 123456_12092013.txt
I am able to rename the file.

However I am going to schedule it to auto rename the file nightly.
And the 12092013 is a running date.
I'm afraid if the command fails to run, I will rename older files with new date.
Any command to like keep intact the filename, just adding a _ to it?
example 123456XXXXXXXX.txt change to 123456_XXXXXXXX.txt

Thank you for viewing.

There is no 1 command in systems performing such functionality. You will very likely need to write a script for it and have this script run as a batch job nightly via the scheduler.

For Windows, you can try powershell, and the plain old batch files. However I find the plain old batch commands of Windows rather limiting and syntax more than necessarily arcane, similar to shell scripts in unix. If you know other scripting languages such as Ruby, Python, Perl or even PHP(CLI), feel free to run these scripts in your Windows environment for your purpose.

Below I will give you a perl fragment to perform what you want

rotate.pl <FILENAME>
Code:
use File::Copy;

if (-f $ARGV[0]) {
  if ($ARGV[0] =~ /^(.*)(\d{6})(\d{8}\.[^.]*)$/) {
    move($ARGV[0], "$1$2_$3") unless (-f "$1$2_$3");
  }
}
 
Important Forum Advisory Note
This forum is moderated by volunteer moderators who will react only to members' feedback on posts. Moderators are not employees or representatives of HWZ Forums. Forum members and moderators are responsible for their own posts. Please refer to our Community Guidelines and Standards and Terms and Conditions for more information.
Top