batch rename

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Message
Author
drgt
Posts: 160
Joined: 21 Sep 2010 02:22
Location: Greece

batch rename

#16 Post by drgt » 22 Jan 2013 16:35

Antonio, again thank you very much.

Just to learn something and not get spoonfed all the time, and if it is not too much waisting your time, I 'd like to understand the following:

Code: Select all

for /F %%a in ('find /c /v "" ^< fileList.txt') do set files=%%a


Here you are getting the number of files, but what are you directing the pc to find? I assume the single quote and parenthesis are syntax rules?

Code: Select all

set /A nextNumber=10000 + files*2 - adjust


Here you get the number of the first new file that will be renamed in the previous order. So (for odd and 5 files) A = 10000+5*2-1=10009? or A nextNumber=10009? :?:

Code: Select all

ren %%a HP[u]!nextNumber:~-4!.jpg 2[/u]> NUL


I do not understand the underlined section... :?: I guess it can't do code and underline; between the u's!!

Code: Select all

set /A nextNumber-=2


Now you are setting A again or... I am lost.

---------
What's a good source to learn batch writing?

abc0502
Posts: 1007
Joined: 26 Oct 2011 22:38
Location: Egypt

Re: batch rename

#17 Post by abc0502 » 22 Jan 2013 19:08

Code: Select all

for /F %%a in ('find /c /v "" ^< fileList.txt') do set files=%%a

Aacini is redirecting the list file that contain the file names as it will be the file where the "FIND" command will search in,

In your example "10009", this will be assigned to !nextNumber! variable, but the files name doesn't contain the number 1 so it must be removed and take only the rest of the numbers or you can just take the first 4 numbers from the right ( and that what !nextNumber:~-4! represent.

- sign means from the right.
4 number means the four characters in the variable.


Or you can excluded the number 1 by using !nextNumber:~1!
that means to start from second character and take the rest.

The rule is
suppose we have a variable X has the value 1234
now, number 1 has index number 0 and number 2 has index number 1 and number 3 has index number 2 and number 4 has index number 3
so when you want to display number 1 and 2 -->
%x:~0,2%

That means take characters in index 0 and index 1 but not index 2 [till index 2 as index 2 is not included]

To take all characters from let's say number 2 and all the rest %x:~1%
You don't have to provide the end index.

But when using the - sign, you start from the right and the index then start from number 1 not 0.

I hope that is clear enough :)

What's a good source to learn batch writing?
This question was asked a lot :lol: ,
well, there is no particular source.
all what you need is to know is the basics of each command (help in cmd window will give a start), and while coding your own batch files you will face obstacles and during your search for the answers you will get the experience and learn new tricks and advanced techniques.
I only knew the basics when i signed in this forum, but from practice and reading i learned. :)
I guess that is basically how every one here learned. :wink:

abc0502
Posts: 1007
Joined: 26 Oct 2011 22:38
Location: Egypt

Re: batch rename

#18 Post by abc0502 » 22 Jan 2013 19:13

Oh, this is a function i wrote to determine if a number is odd or even, check the notes in the function
It might get handy for some one,

Important, the value is EVEN or ODD all capital, in case you didn't use /I switch in the if command while comparing results.

Code: Select all

:Odd_or_Even <number> <variable> <Max>
:: <number>      -- A Number to get it's Type ( Odd or Even ).
:: <varibale>    -- Optional Name you set to save the values [ODD or EVEN] <<"all letter is Capital">>) to it.
:: <max>         -- Default Max number to get it's type is 1000, you can change it by adding yours
::                  Max number means max number you can compare your number to, and must be equal or greater to yours
::
:: IF you don't set the varibale name and set max number add empty "" in place of the varibale.
:: If you didn't use a variable, the result will be displayed on screen.
::
:: Note:when checking your variable whether it was ODD or EVEN, Use /I switch with the <IF command>, As ODD/EVEN
::      has the all letter Capital to prevent any errors while comparing.
::
SET "Odd_or_Even_num=%~1"
SET "Odd_or_Even_max=%~3"
IF NOT DEFINED Odd_or_Even_max SET "Odd_or_Even_max=1000"
For /L %%A In (1 2 %Odd_or_Even_max%) Do (
   IF "%Odd_or_Even_num%" EQU "%%A" SET "Odd_or_Even_Var=ODD" & Goto :Odd_or_Even_Next
)
:: This option will be validated if it wasn't odd number
SET "Odd_or_Even_Var=EVEN"
:Odd_or_Even_Next
(
ENDLOCAL
IF "%~2" NEQ "" SET "%~2=%Odd_or_Even_Var%"
IF "%~2" EQU "" Echo %Odd_or_Even_Var%
)
Goto :EOF

Aacini
Expert
Posts: 1914
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: batch rename

#19 Post by Aacini » 22 Jan 2013 19:46

This is the method I use to determine if a number is odd or even:

Code: Select all

set /A "parity=number & 1"
Previous line take the rightmost bit of number and store it in parity variable, so its value is 0 if number is even, otherwise is 1.

Code: Select all

if %parity% equ 0 (
   echo The number is even
) else (
   echo The number is odd
)


Antonio

abc0502
Posts: 1007
Joined: 26 Oct 2011 22:38
Location: Egypt

Re: batch rename

#20 Post by abc0502 » 23 Jan 2013 00:19

Aacini wrote:This is the method I use to determine if a number is odd or even:

Code: Select all

set /A "parity=number & 1"
Previous line take the rightmost bit of number and store it in parity variable, so its value is 0 if number is even, otherwise is 1.

Code: Select all

if %parity% equ 0 (
   echo The number is even
) else (
   echo The number is odd
)


Antonio

WOW :shock: :shock: :shock: :shock:
I have to say this is amazing. HOW IT WORKS, i used your code in a batch like this:

Code: Select all

@echo off
setlocal enabledelayedexpansion
set "number=5656565656565"
set /A "parity=number & 1"

if %parity% equ 0 (
   echo The number is even
) else (
   echo The number is odd
)
pause
and every time it get the right value, HOW ! :?: :!:
This make my code as an antique .

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: batch rename

#21 Post by foxidrive » 23 Jan 2013 00:34

It is clever. I learned bitwise operations on college but I don't use them in batch files.

@abc0502:

If you have a number like 256 or hex FF then it is represented in binary as 1111 1111
The right most bit is the bit that is incremented with every increasing number.

Every odd number will have that bit set to 1 and every even number will have it set to 0

Antonio's code uses bitwise logic in batch code to extract that bit, and then the test is easy.

Some more numbers as an example:

00 = 0000 0000
01 = 0000 0001
02 = 0000 0010
03 = 0000 0011
04 = 0000 0100
05 = 0000 0101

abc0502
Posts: 1007
Joined: 26 Oct 2011 22:38
Location: Egypt

Re: batch rename

#22 Post by abc0502 » 23 Jan 2013 02:25

I understand that now, it is really clever :)
thanks, Foxidrive and Aacini too :) for this code

Aacini
Expert
Posts: 1914
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: batch rename

#23 Post by Aacini » 23 Jan 2013 07:42

Perhaps a simpler way to understand previous method is this:

Code: Select all

set /A parity=number %% 2
In this case, parity is equal to the remainder when the number is divided by two. Of course, such remainder is zero for even numbers and is one for odd numbers. However, this operation is slightly slower than a bitwise and.

Antonio

brinda
Posts: 78
Joined: 25 Apr 2012 23:51

Re: batch rename

#24 Post by brinda » 06 Jul 2013 08:38

Antonio & foxidrive,

my gratitude to both of you. me searching for a short code for very long that could find odd and even numbers

thanks for giving this code and explanation (odd and even number). Wow :mrgreen: this is the shortest i have seen so far.

it made me possible to build the css zebra styling with set/a as line numbers count so that older IE browsers can show the effect as an alternative to javascript. Older IE browsers do not support css (first|last|nth)-child selectors so we have to individually input this.

This is a beautiful code :D :D :D

Code: Select all

The number is odd 0001  
The number is even 0002 
The number is odd 0003 
The number is even 0004 
The number is odd 0005 
The number is even 0006 
The number is odd 0007 
The number is even 0008 
The number is odd 0009 


script used

Code: Select all

setlocal enabledelayedexpansion
set "index=0000"
   
for /f "usebackq delims=|" %%f in (serial.txt) do (
     del %%f_lnk.txt

     set /a i=1%index%
   
 
  for /f "tokens=*" %%g in ('findstr /c:"%%f" slink.txt') do (
   set /a i+=1
   set /a "parity=!i! & 1"
   if !parity! equ 0 (
     echo The number is even !i:~-4!  >> l.txt
   ) else (
     echo The number is odd !i:~-4!  >> l.txt
   )

  )

 
)

endlocal enabledelayedexpansion

Post Reply