0

I'm trying to rename a certain file 'ABCDE' to 'ABXCDE' using the command

>ren AB* ABX*

But I get the result below.

'ABXDE'

It's eating up a character space

Is there a way to do this with a single command? I think it may have something to do with wildcards.

Thanks

dbenham
  • 127,446
  • 28
  • 251
  • 390
Luigi
  • 439
  • 5
  • 23

3 Answers3

1

Unfortunately, Windows cmd.exe does not have a simple native command that will easily perform the rename operation you want.

The REN (RENAME) command does support wild cards, but Microsoft never bothered to properly document how they work. That really irritated me, so I did a bunch of experimentation and derived rules for how they work. You can see the result at How does the Windows RENAME command interpret wildcards? on the SuperUser site.

If you want to use nothing but pure native cmd.exe commands, then you can use the following one liner on the command line:

for /f "delims=:" %F in ('dir /b /a-d ab*') do @set "F=%F"&cmd /v:on /c ren "%F" "!F:*AB=ABX!"

I think it looks a bit better as a batch file. Note that I toggle delayed expansion on and off within the loop to protect any ! that may be present in the file name.

@echo off
setlocal disableDelayedExpansion
for /f "delims=:" %F in ('dir /b /a-d ab*') do (
  set "F=%F"
  setlocal enableDelayedExpansion
  ren "!F!" "!F:*AB=ABX!"
  endlocal
)

You could write a simple VBS or JScript program to do the rename. Or I imagine there is a simple PowerShell solution.

I like to work in the batch (Windows cmd.exe) environment, so I wrote a regular expression rename utility called JREN.BAT. It is pure script (hybrid JScript/batch) that runs natively on any Windows machine from XP onward. Full documentation is embedded within the script.

The solution with JREN.BAT is trivial :-)

jren "^AB" "ABX" /i

The above ignores case. Remove the /I option and the above will only rename files that begin with uppercase AB.

You must use CALL JREN if you put the command within a batch script.

Community
  • 1
  • 1
dbenham
  • 127,446
  • 28
  • 251
  • 390
1

REN AB* ABX*

Working

The TargetMask is processed against the source name strictly from left to right with no back-tracking.

Let the character c represents all characters except '*','.' and ' ' (space).

  1. In the TargetMask c Advances the position within the source name as long as the next character is not . and appends c to the target name.

  2. '*' At end of sourceMask - Appends all remaining characters from source to the target. If already at the end of source, then does nothing.

Complete reading source: dbenham's answer to rename command

Explanation

In your case first A of TargetMask is matched with A of SourceMask.

B of TargetMask is matched with B of SourceMask.

X of TargetMask is matched with * of SourceMask and *(position 3 of SourceMask which is C) is replaced by X.

Finally * of TargetMask is matched with * of SourceMask which is {DE}(as position C has already been replaced by X)

Solution Step by Step

You can do this by following these steps:(Assuming the file has an extension of .txt ie. initial file name is ABCDE.txt)

REN AB* " "*

NOTE: there are two spaces inside quotes.

In the command above I am replacing first two chars by two blank space chars. Status of file name after executing this command:' CDE.txt' two blank spaces in the beginning of the name.

FOR %v IN (" "*) DO REN "%v" %v.X

NOTE: there are two spaces inside quotes.

In the above command I have removed the initial two spaces and renamed the file with .X extension. Status of file name after executing this command: 'CDE.txt.X'

FOR %v IN (*.X) DO REN %v ABX%v

In the above command I have prefixed the file with ABX.Status of file name after executing this command: 'ABXCDE.txt.X'

FOR %v IN (*.X) DO REN %v *.txt

By using above command I am renaming 'ABXCDE.txt.X' to 'ABXCDE.txt'.

In depth Reading about tricks of ren: http://www.lagmonster.org/docs/DOS7/z-ren1.html

Community
  • 1
  • 1
  • This strategy is extremely convoluted, and it fails at `FOR %v IN (" "*) DO REN "%v" %v.X` if the original name contains spaces. – dbenham Nov 14 '15 at 17:20
  • The info at the SS64 link comes from my original [SuperUser post](http://superuser.com/questions/475874/how-does-the-windows-rename-command-interpret-wildcards), as is noted in the credit at the bottom of the SS64 page. – dbenham Nov 14 '15 at 17:23
  • The info at the DOS7 link is for MSDOS 7, which is an entirely different OS than Windows. I haven't studied the MSDOS rules in depth, but there are likely differences in REN behavior in MSDOS 7 vs. Windows. – dbenham Nov 14 '15 at 17:30
  • The simple FOR loop can process the same file multiple times because the DO code manipulates the list via REN. The solution is to use `FOR /F` with `('DIR /B /A-D AB*')` instead. – dbenham Nov 14 '15 at 17:39
  • This is my first answer on the stack so forgive me for my mistakes. Thanks for the corrections @dbenham I appreciate your remark about looping. I am adding your name into credits. –  Nov 14 '15 at 19:32
  • Sorry. It really fails. My exact file names are SA00XXX.01 to SLS00XXX.DTA The problem is, it's replacing the 0 it's becomineg SLS0XXX.DTA – Luigi Nov 15 '15 at 00:49
  • I also tried your working solution first before I posted my question in stackoverflow because it was really bugging me – Luigi Nov 15 '15 at 00:53
  • I retried the solution with the new test case but it seems that this time the loop caused the operation to be performed twice and the result is SLSSLS00XXX.DTA ; I can't see 0 being replaced. --> Replace first two chars of file name with space --> Rename it by removing first two spaces and appending .x -->Prefix SLS or what ever you want -->Rename name.01.x to name .01 --> Rename ab.01 to ab.dat –  Nov 15 '15 at 06:41
1

As I said in my comment, your question is vague. There are several details that are not clear and you have not posted a precise example of what you want, so I can only guess what the real request is. I assume that given the file names below, you want to change they to the names at right side:

SA00???.01  ->  SLS00???.DTA
SB00???.01  ->  SLS00???.DTA
. . .
SZ00???.01  ->  SLS00???.DTA

The Batch file below solve the problem as I stated it:

@echo off
setlocal EnableDelayedExpansion

for /F %%a in ('dir /B S?00*.01') do (
   set "name=%~Na"
   ren "%%a" "SLS!name:~2!.DTA"
)
Aacini
  • 65,180
  • 12
  • 72
  • 108
  • Hi. I have also tried the ? wildcard. It just eats up space. When I went and did more research it had something to do with how * behaves. This worked tho. I've decided to add my own command and add it in windows PATH. Thank you! – Luigi Nov 15 '15 at 18:14