Javascript and extra zeros.

T

Tank

First off let me say that I am by no means a skilled programmer so i
probably have made a dozen mistakes in my attempt at coding my
problem. I will tell you the problem so that you have an idea of what
i am trying to do and I will give you my code so you can see my
clusterf*ck.

Here is the problem:

At work I have about 20 photographers who work for me. Each one has a
photo idea number (mine is 102 and my bosses is 101 and my assistant's
is 103 etc.) When they take a picture they give a card to the guest
and then when they have filled up thier memory cards they return to
the office to load thier pictures.

The pictures get loaded into thier own folders. Each photographer has
5000 folders to work from. they drag one picture labeled
"dscfxxxx.jpg" (x = 0000 which counts up as each pisture is taken)
into the lowest unfilled folder (1020001, 1020002, etc.) and then puts
those folders on the server. the guest comes to the counter and
enters thier number on the screen and the image within the folder of
the number entered is then pulled up.

Working with so many images and folders leads to mistakes and
frequently guests enter numbers that do not match up to the picture of
them. This is because the photographer accidentally put it in the
wrong folder or handed out the wrong card. I can not deal with
handing out the wrong card as mistakes happen but i know it can be
programmed to automattically take the pictures from the media card,
create a folder and then transfer the picture to that folder.

My solution and my problem:

I have written a javascript that is very ugly--

<script language="javascript">

/*alows user to input starting folder and amount of photos taken and
then makes them into numbers*/

folder = window.prompt('starting folder','default');
picnumber = window.prompt('pictures taken?','default');
f2 = parseFloat(folder);
p2 = parseFloat(picnumber);


/*adds the amount of photos taken to the starting folder number to
ensure proper amount of folders are present and tells user what the
starting folder and end folders are*/

varAdd = f2+p2-1;
varImg = 0000+p2;
document.write('Starting folder is ',f2,'<br>');
document.write("Ending folder should be " + varAdd + "<br><br>");

/*Commands used to bring system to standard for file transfer*/

document.write("cd..<br>")
document.write("cd documents and settings\christopher
neu\desktop<br>")
document.write("mkdir transfer<br>")
document.write("cd transfer<br>")

/*copies files from smartmedia to transfer folder*/

document.write('copy d:\dcim\100_fuji\*.jpg<br>')

/*calculates and writes proper amount of folders in the transfer
folder via the "mkdir" command to properly transfer files*/

for (count=f2;count<=varAdd;count++)
document.write("mkdir " + count +"<br>");

/*moves pictures into proper folders*/

for (count=p2;count<=varImg;count++)
document.write("move dscf" + count +".jpg <br>")

</script>

--this script successfully allows the photographer to input what
folder number they started on and how many photos they took then
creates the proper amount of folders so each pictures has its own
folder. I then am trying to have the script create the proper
commands to move each photo to its equivalent folder. dscf0001.jpg to
the lowest available folder dscf0002.jpg to the next lowest etc.

here is the problem with that step:

I am trying to have it sequence out the proper number of images by
counting starting with 0000 and adding the number of photos taken and
then combining it onscreen with the text "dscf" and ".jpg" so that it
reads "dscf0001.jpg" but it keeps dropping the extra zeros and gives
me "dscf1.jpg"

Once it is done running the photographer will copy the command text
and paste it to the dos command line and it will move every picture to
its correct folder for them eliminating a major area of mistake that
has cost us a tone of sales because people are like,"well it is no
biggie. Don't worry about finding it."

I am also having the problem of displaying the slashes in the change
directory commands. they are just eliminated and i am not sure how to
make them appear.

If someone can help me with this it will be greatly appreciated!

P.S. sorry this is sol drawn out and not really straight to the point
but i am a firm believer that backstory of the problem brings about a
much swifter solution.
 
V

Vladdy

Tank said:
First off let me say that I am by no means a skilled programmer so i
probably have made a dozen mistakes in my attempt at coding my
problem. I will tell you the problem so that you have an idea of what
i am trying to do and I will give you my code so you can see my
clusterf*ck.

Here is the problem:

At work I have about 20 photographers who work for me. Each one has a
photo idea number (mine is 102 and my bosses is 101 and my assistant's
is 103 etc.) When they take a picture they give a card to the guest
and then when they have filled up thier memory cards they return to
the office to load thier pictures.

The pictures get loaded into thier own folders. Each photographer has
5000 folders to work from. they drag one picture labeled
"dscfxxxx.jpg" (x = 0000 which counts up as each pisture is taken)
into the lowest unfilled folder (1020001, 1020002, etc.) and then puts
those folders on the server. the guest comes to the counter and
enters thier number on the screen and the image within the folder of
the number entered is then pulled up.

Working with so many images and folders leads to mistakes and
frequently guests enter numbers that do not match up to the picture of
them. This is because the photographer accidentally put it in the
wrong folder or handed out the wrong card. I can not deal with
handing out the wrong card as mistakes happen but i know it can be
programmed to automattically take the pictures from the media card,
create a folder and then transfer the picture to that folder.

My solution and my problem:

I have written a javascript that is very ugly--

<script language="javascript">

/*alows user to input starting folder and amount of photos taken and
then makes them into numbers*/

folder = window.prompt('starting folder','default');
picnumber = window.prompt('pictures taken?','default');
f2 = parseFloat(folder);
p2 = parseFloat(picnumber);


/*adds the amount of photos taken to the starting folder number to
ensure proper amount of folders are present and tells user what the
starting folder and end folders are*/

varAdd = f2+p2-1;
varImg = 0000+p2;
document.write('Starting folder is ',f2,'<br>');
document.write("Ending folder should be " + varAdd + "<br><br>");

/*Commands used to bring system to standard for file transfer*/

document.write("cd..<br>")
document.write("cd documents and settings\christopher
neu\desktop<br>")
document.write("mkdir transfer<br>")
document.write("cd transfer<br>")

/*copies files from smartmedia to transfer folder*/

document.write('copy d:\dcim\100_fuji\*.jpg<br>')

/*calculates and writes proper amount of folders in the transfer
folder via the "mkdir" command to properly transfer files*/

for (count=f2;count<=varAdd;count++)
document.write("mkdir " + count +"<br>");

/*moves pictures into proper folders*/

for (count=p2;count<=varImg;count++)
document.write("move dscf" + count +".jpg <br>")

</script>

--this script successfully allows the photographer to input what
folder number they started on and how many photos they took then
creates the proper amount of folders so each pictures has its own
folder. I then am trying to have the script create the proper
commands to move each photo to its equivalent folder. dscf0001.jpg to
the lowest available folder dscf0002.jpg to the next lowest etc.

here is the problem with that step:

I am trying to have it sequence out the proper number of images by
counting starting with 0000 and adding the number of photos taken and
then combining it onscreen with the text "dscf" and ".jpg" so that it
reads "dscf0001.jpg" but it keeps dropping the extra zeros and gives
me "dscf1.jpg"

Once it is done running the photographer will copy the command text
and paste it to the dos command line and it will move every picture to
its correct folder for them eliminating a major area of mistake that
has cost us a tone of sales because people are like,"well it is no
biggie. Don't worry about finding it."

I am also having the problem of displaying the slashes in the change
directory commands. they are just eliminated and i am not sure how to
make them appear.

If someone can help me with this it will be greatly appreciated!

P.S. sorry this is sol drawn out and not really straight to the point
but i am a firm believer that backstory of the problem brings about a
much swifter solution.

Here is a printf emulator I wrote for javascript. It should help you
with formating things like that. If you are unfamiliar with C, you may
want to read a manual on printf() function (just google one)

http://www.vladdy.net/Demos/printf.html
 
M

Michael Winter

I am trying to have it sequence out the proper number of images by
counting starting with 0000 and adding the number of photos taken and
then combining it onscreen with the text "dscf" and ".jpg" so that it
reads "dscf0001.jpg" but it keeps dropping the extra zeros and gives
me "dscf1.jpg"

To pad a number, you can call this function:

// Input: number - The number (integer) to be padded
// paddedLength - The desired length of the number after
// padding (integer)
// Output: A string padded with leading zeros that is equal in
// length to the paddedLength argument
// Note: If a number is passed that is longer than the
// paddedLength argument, the number will be unchanged
// (i.e. no padding will occur)
//
function padZeros( number, paddedLength ) {
return '00000000'.substr(
0,
paddedLength - String( number ).length ) + String( number );
}

I am also having the problem of displaying the slashes in the change
directory commands. they are just eliminated and i am not sure how to
make them appear.

So, to create dscf0013.jpg, you could write:

var photoNumber = 13;
var filename = 'dscf' + padZeros( photoNumber, 4 ) + '.jpg';

As for the disappearing backslashes: this is because a single backslash
(in a string literal) is used to create escape characters. For example,

alert( "This is on the first line.\nThis is on the second line." );

would display an alert box that has two lines of text. The '\n' inserts a
new line between the two sentences. To use a literal backslash, place two
together:

alert( "C:\\Windows\\Readme.txt" );

That would display, C:\Windows\Readme.txt

There are several other problems with your script (lack of type checking,
no type attribute in the SCRIPT tag) but it's too late (time-wise) for me
to comment on them now. Someone else will pick up on them, I'm sure.
P.S. sorry this is sol drawn out and not really straight to the point
but i am a firm believer that backstory of the problem brings about a
much swifter solution.

Don't be. It's far better, and much more preferable, than: "This doesn't
work. Why?"

Hope that helps,
Mike
 
T

Tank

Michael Winter said:
To pad a number, you can call this function:

// Input: number - The number (integer) to be padded
// paddedLength - The desired length of the number after
// padding (integer)
// Output: A string padded with leading zeros that is equal in
// length to the paddedLength argument
// Note: If a number is passed that is longer than the
// paddedLength argument, the number will be unchanged
// (i.e. no padding will occur)
//
function padZeros( number, paddedLength ) {
return '00000000'.substr(
0,
paddedLength - String( number ).length ) + String( number );
}



So, to create dscf0013.jpg, you could write:

var photoNumber = 13;
var filename = 'dscf' + padZeros( photoNumber, 4 ) + '.jpg';

As for the disappearing backslashes: this is because a single backslash
(in a string literal) is used to create escape characters. For example,

alert( "This is on the first line.\nThis is on the second line." );

would display an alert box that has two lines of text. The '\n' inserts a
new line between the two sentences. To use a literal backslash, place two
together:

alert( "C:\\Windows\\Readme.txt" );

That would display, C:\Windows\Readme.txt

There are several other problems with your script (lack of type checking,
no type attribute in the SCRIPT tag) but it's too late (time-wise) for me
to comment on them now. Someone else will pick up on them, I'm sure.


Don't be. It's far better, and much more preferable, than: "This doesn't
work. Why?"

Hope that helps,
Mike


I will give these suggestions a try and see what happens. I will also
post my results. Thank you for the input and if anyone else feels
like throwing mor ideas my way then please feel free. Once again
thank you and I will update this with results.
 
B

Brian Genisio

Here is a printf emulator I wrote for javascript. It should help you
with formating things like that. If you are unfamiliar with C, you may
want to read a manual on printf() function (just google one)

http://www.vladdy.net/Demos/printf.html

Very cute... I like it. It worked just as I would expect it to :)

For the OP, they would do something like this:
printf('%05d', 32);

would print 00032.

Brian
 
D

Dr John Stockton

JRS: In article <[email protected]>, seen
in news:comp.lang.javascript said:
I am trying to have it sequence out the proper number of images by
counting starting with 0000 and adding the number of photos taken and
then combining it onscreen with the text "dscf" and ".jpg" so that it
reads "dscf0001.jpg" but it keeps dropping the extra zeros and gives
me "dscf1.jpg"


The following function, given a number or string, will return the same
padded up to stated length with zeroes; there is no effective limit on
the length of the string. For any reasonable value of L, it should be
fast enough.

function Stretch(Q, L, c) { var S = Q+''
if (c.length>0) while (S.length<L) { S = c+S }
return S }

It could be modified to supply a default value for an absent c.

The function could, basically, have been found by reading the FAQ.
 
T

Tank

Dr John Stockton said:
JRS: In article <[email protected]>, seen



The following function, given a number or string, will return the same
padded up to stated length with zeroes; there is no effective limit on
the length of the string. For any reasonable value of L, it should be
fast enough.

function Stretch(Q, L, c) { var S = Q+''
if (c.length>0) while (S.length<L) { S = c+S }
return S }

It could be modified to supply a default value for an absent c.

The function could, basically, have been found by reading the FAQ.


OK. what ypes of values would i put in those variable positions.
would it be dscf for the file name prefix before the 000x or the .jpg
or what. I do not mean to be annoying but like i said i am a newbie
to this. Very newbie.
 
M

Michael Winter

OK. what ypes of values would i put in those variable positions.
would it be dscf for the file name prefix before the 000x or the .jpg
or what. I do not mean to be annoying but like i said i am a newbie
to this. Very newbie.

In the function above:

Q is the value (either a number or string) that you want to pad
L is the final length of the string
c is the character used to pad the string

For example,

Stretch( 5, 4, '0' ) ; returns '0005'
Stretch( '29', 12, '9' ) ; returns '999999999912'

Mike
 
T

Tank

Dr John Stockton said:
JRS: In article <[email protected]>, seen



The following function, given a number or string, will return the same
padded up to stated length with zeroes; there is no effective limit on
the length of the string. For any reasonable value of L, it should be
fast enough.

function Stretch(Q, L, c) { var S = Q+''
if (c.length>0) while (S.length<L) { S = c+S }
return S }

It could be modified to supply a default value for an absent c.

The function could, basically, have been found by reading the FAQ.

OK I figured out more about what you meant with that function. that
was very helpful. How would i incorporate that into a loop? I have
tried searching various places with no luck. I want to make it so
that the padding occurs on each number returned from the loop until
the loop uis complete. It is simple i am sure but I am just no good
at this stuff.
 
T

Tank

Michael Winter said:
In the function above:

Q is the value (either a number or string) that you want to pad
L is the final length of the string
c is the character used to pad the string

For example,

Stretch( 5, 4, '0' ) ; returns '0005'
Stretch( '29', 12, '9' ) ; returns '999999999912'

Mike

Thank you mike. I figured that out after i posted the message. If
only i could figure how to incorporate that into a loop i would be
set. back to the web...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top