PDA

View Full Version : Batch Rename via Command Line



Skippybox
08.09.2008, 08:58 PM
How does someone do a batch rename via the command line without altering the file, except its name? That is, how do you do the rename without doing a conversion? Is it possible?

Skippybox
09.09.2008, 03:55 PM
I would like to do the batch rename without the program resaving the file using the settings for the file type. For instance, I don't want a jpg to be converted to a jpg just because I change the name in the command line.

Bhikkhu Pesala
09.09.2008, 05:47 PM
I don't see any reason why renaming should be converting your files.

See Batch Renaming (http://homepage.ntlworld.com/pesala/Irfan%20View/Help/Tutorials/Batch/batch.html#Rename)

There are three options on the dialogue: Convert, Rename, or Both.

Skippybox
09.09.2008, 06:19 PM
I don't see any reason why renaming should be converting your files.

There are three options on the dialogue: Convert, Rename, or Both.

My question specified that I am trying to rename via the command line, not the Batch Conversion/Rename dialog.

Laurent
09.09.2008, 08:27 PM
Hi Skippybox,

Why do you want to use IrfanView ? The DOS "rename" command could do that.

Laurent

Skippybox
09.09.2008, 08:39 PM
Why do you want to use IrfanView ? The DOS "rename" command could do that.

Hi Laurent,

The DOS 'rename' command does not allow you to specify a new path for the file. Is the Batch dialog the only way in IrfanView? I would like to do it without an interface, so I could turn it into a shortcut. Is a batch file better. Any ideas?

Laurent
10.09.2008, 10:53 AM
Hi Skippybox,

Then use the move command.

Laurent

derniwi
10.09.2008, 12:27 PM
Hi Skippybox,

this is possible, with a few restrictions:
if you have german umlauts or special characters like "&" or "+" in the filename, than windows can deal with them, a DOS script not (ok, a DOS script can work with the german umlaut but you can not convert it easy from the Windows character set to the DOS character set).

What do you exactly need? A script which you can put in the send to folder and you want to send an folder to this script. The script shall move, copy or rename the files to another given or asked name?

Maybe I can help you. :)

Regards, Nils.

Skippybox
10.09.2008, 09:57 PM
What do you exactly need? A script which you can put in the send to folder and you want to send an folder to this script. The script shall move, copy or rename the files to another given or asked name?


Hi Nils, :)

I probably should use a renamer program, but I would like to experiment with scripting. I was basically looking for a quick way using a shortcut to re-pad a folder of files to make sorting easier. I would like a script similar to what you described. I know you are quite talented at batch files. Perhaps you could give me some pointers or share your ideas with me? I would love to learn! :D

derniwi
11.09.2008, 06:58 AM
Hi Skippy,

so take a look at this script. But use it with care! I have done just a few little tests... ;)

@echo off
rem ask for values?
set askfor=y

rem set default values
set defdestpath=D:\renamed
set defpattern=*.jpg
set defcopymove=c
set defprefix=My_picture_
set defpostfix=_fun
set /a defstartnum=1

rem the source path is passed from the send to function
set sourcepath=%~1

rem allow the output path to be dragged and dropped to the dos box
if "%askfor%" == "y" set /p destpath=Rename to path [%defdestpath%]:
if "%destpath%" == "" set destpath=%defdestpath%

rem ask for file pattern
if "%askfor%" == "y" set /p pattern=File pattern [%defpattern%]:
if "%pattern%" == "" set pattern=%defpattern%

rem ask for copying or moving
if "%askfor%" == "y" set /p copymove=Copy or Move [%defcopymove%]:
if "%copymove%" == "" set copymove=%defcopymove%

rem check if the files should be copied or moved
set copymove=%copymove:~0,1%
if /i "%copymove%" == "m" set copymove=move
if /i "%copymove%" neq "move" set copymove=copy

rem ask for file name prefix
if "%askfor%" == "y" set /p prefix=File prefix [%defprefix%]:
if "%prefix%" == "" set prefix=%defprefix%

rem ask for file name postfix
if "%askfor%" == "y" set /p postfix=File postfix [%defpostfix%]:
if "%postfix%" == "" set postfix=%defpostfix%

rem ask for starting number
if "%askfor%" == "y" set /p startnum=Starting number [%defstartnum%]:
if "%startnum%" == "" set startnum=%defstartnum%
set /a startnum=%startnum%


rem check for files
if not exist "%sourcepath%\%pattern%" goto NoFiles

rem check if outpath contains a drive letter
set drive=%destpath:~0,2%
set drive=%drive:~1,1%
if "%drive%" neq ":" set destpath=%sourcepath%\%destpath%

rem check if the output path exist
if not exist "%destpath%\." md "%destpath%"


for /f "delims=" %%a in ('dir /b /a-d /on "%sourcepath%\%pattern%"') do call :rename "%sourcepath%\%%a"
goto end

rem Move the files
:rename
set suffix=%~X1
set number=0000%startnum%
set number=%number:~-4%

set filename=%prefix%%number%%postfix%%suffix%
%copymove% %1 "%destpath%\%filename%"

set /a startnum=startnum + 1
goto :eof

rem Write some text if no files are found
:NoFiles
echo Sorry, but I can't find files matching "%pattern%" in the
echo directory "%sourcepath%".
goto end

:end
pause

Skippybox
11.09.2008, 04:55 PM
Nils,

You didn't have to go and do all the work for me! I appreciate the wonderful script, but I thought you were going to just show me how to get started. I'm afraid I am not familiar with such elaborate batch files, so I may have trouble figuring out what you have done. Thanks again! :D

derniwi
15.09.2008, 07:53 AM
Hi,

don't worry..

:D:D:D