Page 1 of 1

passing and gaining parameters to MS DOS Batch file

Posted: 23 Jan 2008 12:54
by abo_fahad
Hi,
i have unKnown number of images.jpg
i need MS DOS Batch file to Group them in One New file
and Rename them with new numbers i generate from Visual basic code.
i found out the VB code to run ms dos batch file passing parameters(witch
in this case will be the new numbers of images)
the VB code is:

Code: Select all

[b]Private Sub cmdRun_Click()
    Shell App.Path & "\dosbatch.bat " & _
        txtparameters.Text, vbNormalFocus
End Sub[/b]

but i need the MS DOS Batch file i mention before
can you help me pls.
thanks in advance.

Posted: 31 Jan 2008 03:24
by jeb
Hi,

:?:
i have unKnown number of images.jpg
i need MS DOS Batch file to Group them in One New file


What are the names of the files and what to do with them?

My understanding of the behavior is:
The batch file collect all img*.jpg files and put them in to a list,
then the batch file starts to rename all filenames from the list to
myNewImg-1.jpg to myNewImg-n.jpg. The order of the files will be random(order of the collected filenames).

ex.
your files:
    myPet.jpg
    myCat.jpg
    myDog.jpg
    myDog2.jpg
after dosbatch.bat
    myPet.jpg -> myImg1.jpg
    myCat.jpg -> myImg2.jpg
    myDog.jpg -> myImg3.jpg
    myDog2.jpg -> myImg4.jpg


Is this your aim?

jeb

Posted: 31 Jan 2008 12:23
by abo_fahad
thanks for reply jeb
you are allmost right
actualy i recive these images from adf scanner(auto feeder)
with these names: image 001.jpg, image 002.jpg, image 003.jpg ... etc.
i need to give image 001.jpg the name master 001.jpg
and the rest of the files give them the name attach 001.jpg, attach 002.jpg
attach 003.jpg ... etc. and so on.
i hope i could Clarifie it to u.
thanks again

Posted: 01 Feb 2008 02:22
by jeb
Hi Abo,

a solution with use of array lists.
Loads first the filenames in to an array
picList[0]=image001.jpg
picList[1]=image002.jpg
picList[2]=image003.jpg

then it replace the name with "master" or "attach".

Code: Select all

@echo off
SETLOCAL ENABLEEXTENSIONS
SETLOCAL ENABLEDELAYEDEXPANSION

dir /b /on image*.jpg > pictureList.tmp

call :loadFile picList pictureList.tmp
del pictureList.tmp


:: Iterate through the array and rename all files
set /a max=picList.length-1
for /L %%i in (0,1,%max%) do (
  set srcName=!picList[%%i]!
  if %%i==0 (
   set dstName=!srcName:image=master!
   set /a first=0
  ) ELSE (
   set dstName=!srcName:image=attach!
  )
  echo ren !srcName! !dstName!
)

goto :EOF


:::::::::::::::::::::::::::::::::::::::::::::::::
:loadFile <resultVar> <filename>
:: loads a file <filename> into an array of <resulVar>[0] to <resulVar>[n] and set <resulVar>.length=n+1
:: WITHOUT setlocal, because it is else a bit complicated to use loadFile.counter
:: loadFile.counter will be set global, but i don't find a way to stop that

set /a loadFile.counter=0
for /f "tokens=*" %%a in (%~2) do (
  set %~1[!loadFile.counter!]=%%a
  set /a loadFile.counter=loadFile.counter+1
)
set %~1.length=%loadFile.counter%
goto :eof


bye
Jan Erik

Posted: 01 Feb 2008 07:26
by abo_fahad
thank u very mutch jeb
i am new on dos programming
i will try to understand the code and run it
thanks again jeb

Posted: 03 May 2008 15:49
by jaffamuffin
Is there any limit on the number of files (images) that could be recorded into variables like this? In my tests I have found dropping about 31/32 files onto a batch and reading them in to array style like that is the limit...

Posted: 04 May 2008 14:09
by jeb
Hi jaffamuffin,

I tested it now with 2644 files (Windows\System32), with no problems (only echo the filenames).
I'm using Vista yet, but on XP I use a similiar batch with more than 100 files.

Your break at 30 files seems to be another problem.

jeb

Posted: 05 May 2008 04:00
by jaffamuffin
Interesting, I definately need to look at this.

I'm trying to write a batch file that will accept either, 1 image, more than one image (upto around a 1000) or a folder, dropped onto the icon.

Will post up some code later.

Cheers

Posted: 06 May 2008 05:44
by jeb
Hi,

try this as an idea

Code: Select all

:::::::::::::::::::::::::::::::::::::::::::::::::
:loadFile <filename> <resultVar>
:: Loads the content of a file into an array an set the length of the array
:: without setlocal, because the array should be visible outside of this function
set loadFile.counter=0
for /f "tokens=*" %%a in (%1) do (
  set %2[!loadFile.counter!]=%%a
  set /a loadFile.counter=loadFile.counter+1
)
set %2.length=%loadFile.counter%
goto :eof


jeb