Reputation: 18956
I am trying to rename all the files present in a Windows directory using FOR command as follows at the command prompt:
for %1 in (*.*) do ren %1 test%1
E.g. This renames a file enc1.ctl to testenc1.ctl enc2.ctl to testenc2.ctl
Thats not what i want. What i want is enc1.ctl renamed to test1.ctl enc2.ctl renamed to test2.ctl
How do i do that?
@Akelunuk: Thanks, that w kind of works but i have files names as
h263_enc_random_pixels_1.ctl , h263_enc_random_pixels_2.ctl which i want to rename to
test1.ctl and test2.ctl respectively
Then how?
Upvotes: 1
Views: 18738
Reputation: 1
This renames all files in directory for filter file types with PREFIX and today's date and time
@echo ON
cls
for %%a in (*.pdf) do (set myfiledate=%%~ta echo !myfiledate!)
echo Date format = %myfiledate%
echo dd = %myfiledate:~0,2%
echo mm = %myfiledate:~3,2%
echo yyyy = %myfiledate:~6,4%
echo.
echo Time format = %myfiledate%
echo hh = %myfiledate:~11,2%
echo mm = %myfiledate:~14,2%
echo AM = %myfiledate:~17,2%
echo.
echo Timestamp = %myfiledate:~0,2%_%myfiledate:~3,2%_%myfiledate:~6,4%-%myfiledate:~11,2%_%myfiledate:~14,2%_%myfiledate:~17,2%
ECHO "TEST..." > "test-%myfiledate:~0,2%_%myfiledate:~3,2%_%myfiledate:~6,4%-TIME-%myfiledate:~11,2%_%myfiledate:~14,2%_%myfiledate:~17,2%.txt"
PAUSE
This echos successfuly the date modified and time as a postfix but doesnt parse the info into the rename. I cant figure out why, but it is very close. Maybe someone cant tweak to suit your purpose.
@echo ON
setlocal
cls
for %%a in (*.pdf) do (set myfiledate=%%~ta echo !myfiledate!)
:DATETIME
echo Date format = %myfiledate%
echo dd = %myfiledate:~0,2%
echo mm = %myfiledate:~3,2%
echo yyyy = %myfiledate:~6,4%
echo Time format = %myfiledate%
echo hh = %myfiledate:~11,2%
echo mm = %myfiledate:~14,2%
echo AM = %myfiledate:~17,2%
= %myfiledate:~17,2%
echo.
echo Timestamp = %myfiledate:~0,2%_%myfiledate:~3,2%_%myfiledate:~6,4%-%myfiledate:~11,2%_%myfiledate:~14,2%_%myfiledate:~17,2%
ECHO "TEST..." > "test-%myfiledate:~0,2%_%myfiledate:~3,2%_%myfiledate:~6,4%-TIME-%myfiledate:~11,2%_%myfiledate:~14,2%_%myfiledate:~17,2%.txt"
for /f "delims=" %%a in ('dir *.pdf /t:a /a:-d /b /s') do call :RENAME "%%a"
:RENAME
REM for /f "tokens=1-6 delims=/ " %%a in ('dir %%a /t:w^|find "/"') do (
ren %%a "3DC-test-OFF-ELE-%myfiledate:~0,2%_%myfiledate:~3,2%_%myfiledate:~6,4%-TIME-%myfiledate:~11,2%_%myfiledate:~14,2%_%myfiledate:~17,2%~x1")
PAUSE
GOTO :EOF
Upvotes: 0
Reputation: 41132
I am not sure if it is possible in batch, but then again I never mastered this primitive language... :-P
If CMD isn't mandatory, but you can't use a good file renamer, you can do that with WSH:
var path= "E:/tmp";
var fso = WScript.CreateObject("Scripting.FileSystemObject");
var folder = fso.GetFolder(path);
var files = new Enumerator(folder.files);
for (; !files.atEnd(); files.moveNext())
{
var file = files.item();
var fileName = file.Name;
var p = /^enc(\d+)\.ctl$/.exec(fileName);
if (p != null)
{
var newFileName = "test" + p[1] + ".ctl";
// Optional feedback
WScript.echo(fileName + " -----> " + newFileName);
file.Move(newFileName);
}
}
Of course, put that in a file.js
I actually tested with file.Copy(file.ParentFolder + "/SO/" + newFileName);
to avoid loosing files...
HTH.
Upvotes: 1
Reputation: 3845
I've got it!
for %1 in (.) do ren %1 t%1
and then:
ren tenc*.* test*.*
Upvotes: 2
Reputation: 21572
If you know the number of files, (say 10), you can use
for /L %1 in (1,1,10) do ren enc%1.ctl test%1.ctl
Upvotes: 1