Link to home
Start Free TrialLog in
Avatar of tlcsupport
tlcsupportFlag for United Kingdom of Great Britain and Northern Ireland

asked on

BATCH FILE TO CHANGE FILE EXTENSION OF MULTIPLE SIMILARLY NAMED FILES IN ORDER - DG

Hi Experts!

I was wondering if someone could help me with a batch file please.
I need to change the file extension of a series of files in a folder, one by one, with a pause in between.

Basically, I have a folder containing a series of files of the type:

FILE.001
FILE.002
FILE.003
FILE.004    etc.

If the file extension of one of these files is changed to FILE.DATA, it is automatically processed by an application, and then deleted.
Due to a service failure, these files have not been processed, and have built up - so I have thousands of files that I need to process in order.

So I'm looking for a batch file that will:

a) Rename FILE.001 to FILE.DATA
b) Pause until the application has processed the data and deleted the FILE.DATA file (30 seconds should be more than enough for this).
c) Rename FILE.002 to FILE.DATA
d) Pause again for 30 seconds
e) Rename FILE.003 to FILE.DATA

and so on and so on until all the files are processed.

Any ideas on the easiest way to achieve this?

Thanks in advance to anyone who can help me.
Avatar of GangaramGavli
GangaramGavli

Please refer below mentioned link.

graphicssoft.about.com/cs/renamers/ht/renamexp.htm
Avatar of tlcsupport

ASKER

The link above refers to changing a series of file NAMES. I want to change the FILE EXTENSION - as detailed in the question.
If I understand correctly that you only want to rename the files from .png to .jpg, and not convert them, you could use the following batch code:

@ECHO OFF
PUSHD .
FOR /R %%d IN (.) DO (
    cd "%%d"
    IF EXIST *.png (
       REN *.png *.jpg
    )
)
POPD

Update: I found a better solution here that you can run right from the command line (use %%f in stead of %f if using this inside a batch file):

FOR /R %f IN (*.png) DO REN "%f" *.jpg

Note that the above will process the current directory and its subdirectories. If necessary, you can specify an arbitrary directory as the root, like this:

FOR /R "D:\path\to\PNGs" %f IN (*.png) DO REN "%f" *.jpg
Again - this is NOT what I am after. The FILE EXTENSIONS are unique, the FILE NAMES are all the same. So I cannot just use a wild card to change the FILE EXTENSION - as this would result in all files having the same name - which obviously can't happen in a Windows environment.

I also need the files to be processed in order - i.e. starting with FILE.001, then FILE.002,  etc. and a pause in between.
Avatar of oBdA
Try the script below; you didn't specify in which order you want a situation like FileA.001, FileA.002, FileB.001, FileB.002 processed - all .001 extensions first, then all .002, and so on, or FileA.xxx first, then FileB.xxx and so on. The script can do both, just set the variable "Order" accordingly.
It's currently in test mode and will only display the "rename" commands, so that you can test it. Remove the uppercase ECHO in line 35 to run it for real:
@echo off
setlocal enabledelayedexpansion
set SourceFolder=D:\Temp\Files
REM Set Order to "Extension" (without the quotes) means to first process all extensions 001, then all extensions .002, ...
REM Anything else will process FileA.001, FileA.002, FileA.xxx, then FileB.001, FileB.002, ...
set Order=Extension
if /i "%Order%"=="Extension" goto OrderByExtension

:OrderByFileName
echo Processing in file name order.
set PreviousFile=
for %%a in ("%SourceFolder%\*.*") do (
	if /i not "!PreviousFile!"=="%%~na" (
		set PreviousFile=%%~na
		for /l %%i in (0, 1, 999) do (
			set Ext=00%%i
			if exist "%%~dpna.!Ext:~-3!" call :Process "%%~dpna.!Ext:~-3!"
		)
	)
)
echo Done.
goto :eof

:OrderByExtension
echo Processing in extension order.
for /l %%i in (0, 1, 999) do (
	set Ext=00%%i
	for %%a in ("%SourceFolder%\*.!Ext:~-3!") do call :Process "%%a"
)
echo Done.
goto :eof

:Process
echo [%Time%] Processing %~1 ...
ECHO ren "%~f1" "%~n1.data"
:LoopWait
ping -n 5 localhost >NUL
if not exist "%~dpn1.data" goto :eof
goto :LoopWait

Open in new window

Hi oBda
Thanks for your response. It looks fairly promising, but I can't get it to work.
There is only one filename - FILENAME.00000001, FILENAME.00000002 etc. - so I don't need to worry about the extension order.

I just double checked and there are actually 8 digits in the file extension.

I'll have a play over the weekend and see if I can get it to work.
ASKER CERTIFIED SOLUTION
Avatar of oBdA
oBdA

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
That's perfect! Works like a dream! Many thanks for your help! :-)
Further to the above - a friend of mine come up with this very elegant little batch file that works equally well:

for %%i in (FILE.*) do call :process %%i
exit /B 0
:process
REN %1 FILE.DATA
timeout /t 30
Well, I wouldn't rely on the order in which a normal "for" loop returns the files; there seems to be no official documentation about this. Since you explicitly required the order by extension name, the "dir /o:n" plays it safe.
And the loop instead of a timeout makes sure that ...
1. ... even if the application requires more than 30 seconds to process a file, the script won't try (and fail miserably) to rename the next file before the old file is deleted,
2. ... the script will waste a maximum of 5 seconds (or less, you could just lower the "-n 5" in the "ping" command) after the file has been processed before continuing with the next. With the "elegant" script, if the application needs 10 seconds per file, and you have 5000 files, the script will have idled for 20*5000 seconds, which is more than a day.
Cheers for your comments oBdA. As a "newbie" to this kind of thing, I do appreciate it.