Showing posts with label renaming files. Show all posts
Showing posts with label renaming files. Show all posts

Thursday, July 14, 2011

Copy Files and Automatically Rename Existing Ones

Usually while copying large amount of files it is necessary to rename the existing files to avoid overwriting them with the copied ones. Usual commands like copy or xcopy don't let you perform renaming in automatic mode. Still you are able to create a batch script that will help you to solve this problem:
@echo off
for %%i in (SourceDir\*) do (
if exist "DestDir\%%~nxi" (
mv "%%i" "DestDir\~%%~nxi"
) else (
mv "%%i" "DestDir"
)
)

You can change this example script according to your needs with the help of our batch files editor. Hope you'll find this small example useful.

Tuesday, April 26, 2011

Another Example with Renaming Files

Renaming files is likely the most common task being automated with the help of batch files. Here we will discuss how to add the date of file's creation to its name. As far as you can see, the script to automate this task is quite simple, but you can enhance it with the help of our powerful batch files editor.
SETLOCAL ENABLEDELAYEDEXPANSION
@echo off
for /f %%i in ('dir /b') do (
set aaa=%time%
set a1=!aaa:~0,2!
set a2=!aaa:~3,2!
set a3=!aaa:~6,2!
ren "%%i" "!a1!-!a2!-!a3!_%%i"
)
endlocal
If you run this script, you will see that it adds the date before the name of the file. It is not hard to modify the script to make it add the date to the end of batch file.
And here is also another version of this task's automation:
@echo off
for /f %%i in ('dir /b') do (
set "file=%%i"
call :ert
)
goto :eof
:ert
ren "%file%" "%time:~0,2%-%time:~3,2%-%time:~6,2%_%file%"
Both of these scripts are quite quick, so in general you can use any of them.

Translate