Page 1 of 2
batch rename
Posted: 21 Jan 2013 10:35
by drgt
Hi
Need to rename range of sequential files starting with cmc001.jpg to cmc999.jpg:
First to odd numbers (001 to 001, 002 to 003, 003 to 005, 004 to 007.....)
And then the original range,
To even numbers (001 to 002, 002 to 004, 003 to 006...etc)
Could you please tell me how to do that?
Thanks
Re: batch rename
Posted: 21 Jan 2013 10:48
by foxidrive
Your question is a little unclear.
First to odd numbers (001 to 001, 002 to 003, 003 to 005, 004 to 007.....)
If you rename 2 to 3 then it will be renamed again, 3 to 5.
To even numbers (001 to 002, 002 to 004, 003 to 006...etc)
Similarly here: 1 is renamed to 2 and then 2 is being renamed to 4
I don't understand the task - it's not clear.
Re: batch rename
Posted: 21 Jan 2013 11:21
by drgt
Ok.
Its gonna be actually more complicated.
I have a set of double sided printed matter to scan. My scanner has a doc feeder but not a reverse one.
So the first time I feed this, we are gonna do the odd pages to the end BUT the saved images won't be 1,3,5,7.jpg but sequential (1,2,3,4.jpg). So we need to rename those (even copy in another directory) to match the page numbers scanned.
Now for the even pages is even trickier because as you reverse the pack on the feeder, the 001.jpg that will be produced will belong to the last even page of the document! In a 100 page doc, 001.jpg will be for page 100 and 050.jpg will be for page 2!
The other problem we have is that we need this to work for any number paged document.
I hope I made it clear for you BUT not impossible!
P.S. Need these in correct order to produce pdf
Re: batch rename
Posted: 21 Jan 2013 11:42
by abc0502
So you first scan the (lets name it the "Head pages") first, and after you are done, you go for the "back pages".
if so, that mean, total page numbers of any scan job will be even or odd as the last page from the "head pages" might have no "back page"
Is that correct ?
Re: batch rename
Posted: 21 Jan 2013 11:52
by drgt
To make it easy, every job will have even pages, a white will be added to the last odd if needed.
Re: batch rename
Posted: 21 Jan 2013 14:11
by abc0502
Hi, This should work, set the first 3 variables at the beginning of the file.
Note, this will work for files has names like this: 1 2 3 4 5 NOT 001 002 003 004 005
sorry for that but i forgot about the name form and didn't notice until the code was done, i will try to alter the code and post it tomorrow , but i think you can change the scanner setting to start from 1 not 001.Also, this code should handle files till 1000 file, you can make it more by adding "2000" or any number you want at the end of this command
Call :Odd_or_Even "!total!" "status" "2000"
but more files mean more time.
And you don't have to have odd or even files, the batch will work with any of them.
Code: Select all
@Echo OFF
:: V.7
:: Set These Variable Here
SET "in=%userprofile%\desktop\1"
SET "out=%userprofile%\desktop\1\TMP"
SET "type=txt"
:: Code Start from Here
SETLOCAL EnableDelayedExpansion
:: Create Destination Folder If not exist
IF not Exist "%out%" MD "%out%" >nul
SET "total=0"
For /F "delims=" %%A In ('DIR /A:-D /B "%in%\*.%type%"') Do SET /A "total+=1"
:: Check Total files number to see if the count is Odd or Even Number
Call :Odd_or_Even "!total!" "status"
IF "%status%" EQU "Odd" ( SET /A First_Half= total / 2 + 1
) Else ( SET /A First_Half = total / 2 )
:: Here we Identify the first page of the Back Pages
SET /A Sec_Half=First_Half + 1
:: Create a list of Head and Back Pages
SET "Head="
SET "Back="
For /L %%A In (1 1 %First_Half%) Do SET "Head=!Head! %%A"
For /L %%A In (%Sec_Half% 1 %total%) Do SET "Back=!Back! %%A"
:: Copy The Head Pages to Destination Folder with the New Names
SET "C1=1"
For %%A In (!Head!) Do (
IF Exist "%in%\%%A.%type%" (
IF "%%A" == "1" Copy "%in%\%%A.%type%" "%out%\!C1!.%type%" >nul
IF not "%%A" == "1" (
SET /A C1 += 2
Copy "%in%\%%A.%type%" "%out%\!C1!.%type%" >nul
)
)
)
:: Copy The Back Pages to Destination Folder with the New Names
SET "C2=2"
For %%A In (!Back!) Do (
IF Exist "%in%\%%A.%type%" (
Copy "%in%\%%A.%type%" "%out%\!C2!.%type%" >nul
SET /A C2 += 2
)
)
Pause
Exit /B
:: Odd or Even Check Function
:Odd_or_Even <number> <variable> <Max>
:: Note when combraing your variable Odd and Even have 1st Letter in CAPITAL
:: If you set a variable name, the result will be assigned to that variable, if you don't the result will be displayed on screen
SET "Even_or_Odd_num=%~1"
SET "Even_or_Odd_max=%~3"
IF NOT DEFINED Even_or_Odd_max SET "Even_or_Odd_max=1000"
For /L %%A In (1 2 %Even_or_Odd_max%) Do (
IF "%Even_or_Odd_num%" EQU "%%A" SET "Even_or_Odd_Var=Odd" & Goto :Even_or_Odd_Next
)
:: This option will be validated if it wasn't odd number
SET "Even_or_Odd_Var=Even"
:Even_or_Odd_Next
(
ENDLOCAL
IF "%~2" NEQ "" SET "%~2=%Even_or_Odd_Var%"
IF "%~2" EQU "" Echo %Even_or_Odd_Var%
)
Goto :EOF
batch rename
Posted: 21 Jan 2013 15:21
by drgt
Hey, that's a lot of code! Thank you for going through the trouble writing it. I do appreciate it.
I do have to tell you that I am unable to change the file name assigned by the scanner to the file, because there is no computer involved. It goes directly to an sd card plugged into the scanner itself.
The first file name will be HP0001.jpg (3 leading zeros) and will grow from there in increments of 1. When I am done with odd pages, I transfer the files to pc, erase the card and start the odd pages, so the first file will be again named HP0001.jpg
Like I said, because of the way the pack is loaded, HP0001 will represent the LOWEST odd page number, while the same name for the even pages will represent the HIGHEST even page.
If you do not want to alter the code for the syntax of the file names generated by the scanner, perhaps we can do a simple rename in the beginning of the batch to your specs.
I do not know if Adobe will have a problem with the order if we omit the leading zeros. It might put 10 after 1 for example.
batch rename
Posted: 21 Jan 2013 15:57
by drgt
ERROR: File not found press any key to continue...
made 10 dummy files 1.jpg to 10.jpg and placed them in folder named 1 on my desktop. Run the batch, it made the TPM subdirectory with nothing in it and gave the above error.
By the way, this is designed to pause so I clean the TMP subfolder and refill the 1 folder with the even pages, then continue?
NOTE: There is no :EOF heading to go to
Re: batch rename
Posted: 21 Jan 2013 16:15
by foxidrive
drgt wrote:NOTE: There is no :EOF heading to go to
It doesn't need one.
Change
SET "type=txt"
to
SET "type=jpg"
and see if it works.
Re: batch rename
Posted: 21 Jan 2013 21:07
by Aacini
Let's suppose you have 4 pages. The Batch file below take one parameter, if it is the word "EVEN" the files are renamed in even order: 1->8, 2->6, 3->4 and 4->2. Otherwise (parameter "ODD" or no parameter) the files are renamed in odd order: 1->1, 2->3, 3->5 and 4->7.
Code: Select all
@echo off
setlocal EnableDelayedExpansion
rem Get control values for Odd/Even pages
if /I "%1" equ "EVEN" (
set order=N
set adjust=0
) else (
set order=-N
set adjust=1
)
rem Generate the list of files in the right order
dir /B /O%order% *.jpg > fileList.txt
rem Get the number of files
for /F %%a in ('find /c /v "" ^< fileList.txt') do set files=%%a
rem Get the number of the first new file that will be renamed in the previous order
set /A nextNumber=10000 + files*2 - adjust
rem Rename the files
set lastRename=
for /F %%a in (fileList.txt) do (
ren %%a HP!nextNumber:~-4!.jpg 2> NUL
if !errorlevel! gtr 0 (
set lastRename=%%a HP!nextNumber:~-4!.jpg
)
set /A nextNumber-=2
)
rem Do the last rename, if any
if defined lastRename (
ren %lastRename%
)
Note that this program does not process the names of the original files, just generate new names that consist of "HP", four digits and ".jpg" extension. This can be modified, if required.
Antonio
Re: batch rename
Posted: 21 Jan 2013 23:26
by drgt
@Aacini
Antonio thank you for your input. I appreciate it.
Correct me if I am wrong, but whether the half run (be that the odd pages or the even pages) total file number will be odd or even, it will depend on the number of pages of the original document. For example, for a 10 page document, the half run will be 5 (odd) but for a 20 page document, the half run will be 10 (even).
Since I will be running the rename at 2 different times, (explained in previous post), perhaps it will be easier to apply 2 separate batches, rather to try to determine which half we have to rename.
Just a suggestion.
P.S. I run your batch on 2 sets, one with 5 files and one with 6. The result was the SAME. The files were renamed odd, ascending (1->1, 2->3, 3->5 etc)
Re: batch rename
Posted: 22 Jan 2013 00:05
by drgt
foxidrive wrote:
Change
SET "type=txt"
to
SET "type=jpg"
and see if it works.
Well, left the set type to txt, and changed extension of test files:
It worked but not correctly:
Test Set: 1.txt contained 1, 2.txt contained 3, 3.txt contained 5, 4.txt contained 7, 5.txt contained 9.
The result:
1.txt contains 1
2.txt contains 7
3.txt contains 3
4.txt contains 9
5.txt contains 5
The desired result:
1.txt contains 1
3.txt contains 3
5.txt contains 5
7.txt contains 7
9.txt contains 9
Re: batch rename
Posted: 22 Jan 2013 00:05
by Aacini
drgt wrote:Antonio thank you for your input. I appreciate it.
Correct me if I am wrong, but whether the half run (be that the odd pages or the even pages) total file number will be odd or even, it will depend on the number of pages of the original document. For example, for a 10 page document, the half run will be 5 (odd) but for a 20 page document, the half run will be 10 (even).
Since I will be running the rename at 2 different times, (explained in previous post), perhaps it will be easier to apply 2 separate batches, rather to try to determine which half we have to rename.
Excuse me, I don't understand what you are talking about.
If the document have 5 double sided printed pages, it have 5 even pages and 5 odd pages. If the document have 10 double sided printed pages it have 10 even pages and 10 odd pages.
drgt wrote:To make it easy, every job will have even pages, a white will be added to the last odd if needed.
The number of double sided printed pages in the document have no relation to the set of pages that the sd card has, so there is no way to "try to determine which half" the sd card has. The only person that know which set the sd card has is you, so you must provide the Batch file this information via the EVEN word as parameter. My Batch file behaves as two separated files at this respect (one with EVEN parameter and another one without). For example, to do even rename:
To do odd rename:
Aacini wrote:The Batch file below take one parameter, if it is the word "EVEN" the files are renamed in even order. Otherwise (parameter "ODD" or no parameter) the files are renamed in odd order.
Perhaps I misundertood something?
Antonio
Re: batch rename
Posted: 22 Jan 2013 01:07
by drgt
Aacini wrote:Perhaps I misundertood something?
Antonio
No, I misunderstood the parameter. Sorry!
There seems to be a problem though, perhaps it lies in renaming within the same directory to a name that may already exist.
For example, I tried to run it with the
even parameter on a 6 file test as follows:
hp0001 containing page 12
hp0002 containing page 10
hp0003 containing page 8
hp0004 containing page 6
hp0005 containing page 4
hp0006 containing page 2
The result:
hp0002 contains page 2 (correct)
hp0004 contains page 6
hp000
5 contains page 4
hp0008 contains page 8 (correct)
hp0010 contains page 10 (correct)
hp0012 contains page 12 (correct)
--------------------------------------
I think I fixed it. I just removed "HP" from this line:
Code: Select all
ren %%a HP!nextNumber:~-4!.jpg 2> NUL
Since the original file names all start with HP and the resulting new names will only be numbers, there should be no conflict.
Your comments, please, Mr. Programmer!
Thanks!!!!
Re: batch rename
Posted: 22 Jan 2013 08:26
by Aacini
Yes, the problem is precisely the one you said, but supposedly I have fixed it with the "lastRename" variable (although not correctly
). If the problem is fixed other way then "lastRename" should be removed, but there is another point here.
If after inserting the sd card in the computer, you do the following:
1- Rename the files.
2- Copy they to the pc.
3- Delete the files.
Then these three steps can be done by the Batch file. This is my new version:
Code: Select all
@echo off
setlocal EnableDelayedExpansion
rem Get control values for Odd/Even pages
if /I "%1" equ "EVEN" (
set order=N
set adjust=0
) else (
set order=-N
set adjust=1
)
rem Generate the list of files in the right order
dir /B /O%order% *.jpg > fileList.txt
rem Get the number of files
for /F %%a in ('find /c /v "" ^< fileList.txt') do set files=%%a
rem Get the number of the first new file that will be renamed in the previous order
set /A nextNumber=10000 + files*2 - adjust
rem Move and rename the files
for /F %%a in (fileList.txt) do (
move %%a "C:\The folder\HP!nextNumber:~-4!.jpg"
set /A nextNumber-=2 )
)
Antonio