Question: I am using Windows XP and need to create a batch file to move tok.filename to filename.tok. 
This works for one file if I type in `fix.bat tok.filename`.

```
set filename=%1
set newname=%filename:~4,45%
ren %1 %newname%.tok

```

I need to type in `fix.bat tok*`, but this puts `tok*` in the filename. 

How do I get it to read all the files into the filename one at a time?

Answer: 
Use a `for` statement.

```
setlocal enabledelayedexpansion

for %%i in (tok.*) do (
  set filename=%%i
  set newname=!filename:~4,45!
  ren %%i !newname!.tok
)

```

Enabling the delayed expansion makes it so that the variables are evaluated at the time they are used.

Alternatively, since you already have a batch file that works, you could write another batch file that uses the `for` statement, which calls your working batch file -- like this:

```
for %%i in (tok.*) do call fix.bat %%i

```

Or you could run it directly from the command line like this:

```
for %i in (tok.*) do call fix.bat %i

```
No need for a batch script

```
for %F in (tok.*) do for /f "delims=." %X in ("%~xF") do ren "%F" "%X.%~nF"

```

Double up the percents if used within a batch script.

The above may not give the desired result if more than one `.` appears in a file name.

For example `tok.part1.part2` would become `part2.tok.part1`.

If you want `part1.part2.tok` instead, then you can use the following simple script.

```
@echo off
ren tok.* .*?.tok
for %%F in (.*.tok) do for /f "tokens=* delims=." %%A in ("%%F") do ren "%%F" "%%A"

```

For an explanation of how that 1st REN works in the 2nd script, see [How does the Windows RENAME command interpret wildcards?](https://superuser.com/q/475874/109090)

I suppose an esoteric argument could be made that a name like `tok..name` would become `name.tok` when it should be `.name.tok`. That could be fixed by using:

```
@echo off
setlocal disableDelayedExpansion
ren tok.* .*?.tok
for %%F in (.*.tok) do (
  set "name=%%F"
  setlocal enableDelayedExpansion
  ren "!name!" "!name:~1!"
  endlocal
)

```

The nice thing about the solutions above is they don't rely on the length of "tok". A string value of any length could be substituted for "tok" and the code works just as well.

The James L solution works well for a constant length. That solution can be improved a bit. 

* The 2nd argument to the substring operation is not needed. Removing it lets the solution work no matter how long the full original name is.
* The code will fail if a name has an `!` in it. Fixed by toggling delayed expansion on and off.
* The filenames should be quoted in the REN statement in case of spaces or special characters

.

```
@echo off
setlocal disableDelayedExpansion
for %%i in (tok.*) do (
  set "filename=%%i"
  setlocal enableDelayedExpansion
  ren "!filename!" "!filename:~4!.tok"
  endlocal
)

```