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
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
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.
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?