-4

Looking for a batch script to rename a folder of files.

Names are like My.File.Is.Not.Really.Mine.txt, Another.File.Not.Mine.txt etc.

Would like to rename it to My File Is Not Really Mine.txt, Another File Not Mine.txt

  • 3
    This is not a site for finding an existing script; it's for asking programming questions – Steve Mayne May 31 '18 at 09:35
  • 3
    Hey! Your question is a bit ill-suited for Stack Overflow, as the purpose is not to provide outsourced programming. If you have a specific question on how to achieve what you want, please ask that instead! – vijoc May 31 '18 at 09:36

2 Answers2

1

Here's a simple example of variable expansion and substitution based upon your relatively ambiguous off-topic request:

@For %%A In ("ChangeMe\*.*.*") Do @Call :Sub "%%A"
@Exit /B

:Sub
@Set "fName=%~n1"
@Ren %1 "%fName:.= %%~x1"

You need to either replace ChangeMe\ with the full or relative directory name holding your files, or remove ChangeMe\ entirely and place the batch file inside the directory holding those files.


This preferred method uses the Where commands wildcard matching:

@For /F Delims^=^ EOL^= %%A In ('Where "ChangeMe:*.*.*"') Do @Call :Sub "%%A"
@Exit /B

:Sub
@Set "fName=%~n1"
@Ren %1 "%fName:.= %%~x1"

You need to either replace ChangeMe with the full or relative directory name holding your files, or replace ChangeMe with . and place the batch file inside the directory holding those files.

Compo
  • 36,585
  • 5
  • 27
  • 39
  • 1
    +1 for the preferred method working safely also for file names like `.htaccess` being not typical on Windows and working also fine on FAT32 and exFAT drives. A note for readers still using Windows XP: The command [WHERE](https://ss64.com/nt/where.html) is not available on Windows XP by default. __WHERE__ is available by default on Windows Vista or Windows Server 2003 and any later Windows version. Well, __WHERE__ from Windows Server 2003 can be also copied to a PC still using Windows XP for usage. – Mofi May 31 '18 at 11:36
0

You could use this simple batch file for this task:

@echo off
for /F "eol=| delims=" %%I in ('dir * /A-D-H /B 2^>nul') do if not "%~f0" == "%%~fI" call :RenameFile "%%I"
goto :EOF

:RenameFile
set "NewFileName=%~n1"
if not defined NewFileName goto :EOF
set "NewFileName=%NewFileName:.= %"
if "%NewFileName%" == "%~n1" goto :EOF
echo Renaming "%~nx1" to "%NewFileName%%~x1" ...
ren %1 "%NewFileName%%~x1"
goto :EOF

The first IF condition on FOR command line avoids that the batch file itself is renamed in case of having an unlikely name like Rename.Files.bat and being stored in the directory of the files to rename.

The second IF condition which is the first IF in subroutine RenameFile prevents wrong file name processing for files with a file name starting with a dot and not having a real file extension (string after last dot) like .htaccess. Such file names are not typical on Windows because of being interpreted as file having no file name, just a file extension. But such files can exist on having Unix/Linux files stored on Windows drives which are hidden on Unix/Linux. Unix/Linux file systems don't have a hidden attribute. A file is interpreted as hidden on Unix/Linux if its file name starts with a dot.

The third IF condition compares case-sensitive the current file name with the file name after replacing all dots by spaces and exits the subroutine if new file name is identical to current file name.

Note 1: A wildcard pattern like *.*.* or *.* is interpreted by DIR and FOR, which are internal commands of cmd.exe, like just * and match therefore all (non hidden) files and not just files containing at least two or one dot in name of file.

For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.

Note 2: Command REN does nothing if new name of file is identical to current name of file. So the third IF condition would not be really needed without the ECHO command line near end of batch file.

  • call /?
  • dir /?
  • echo /?
  • for /?
  • goto /?
  • if /?
  • ren /?
  • set /?

See also Where does GOTO :EOF return to?

Mofi
  • 46,139
  • 17
  • 80
  • 143