Javascript and extra zeros.

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Tank

    Javascript and extra zeros.

    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.j pg" (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="javas cript">

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

    folder = window.prompt(' starting folder','defaul t');
    picnumber = window.prompt(' pictures taken?','defaul t');
    f2 = parseFloat(fold er);
    p2 = parseFloat(picn umber);


    /*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\christ opher
    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_fuj i\*.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.j pg" 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.
  • Vladdy

    #2
    Re: Javascript and extra zeros.

    Tank wrote:[color=blue]
    > 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.j pg" (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="javas cript">
    >
    > /*alows user to input starting folder and amount of photos taken and
    > then makes them into numbers*/
    >
    > folder = window.prompt(' starting folder','defaul t');
    > picnumber = window.prompt(' pictures taken?','defaul t');
    > f2 = parseFloat(fold er);
    > p2 = parseFloat(picn umber);
    >
    >
    > /*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\christ opher
    > 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_fuj i\*.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.j pg" 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.[/color]

    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)



    --
    Vladdy

    Comment

    • Michael Winter

      #3
      Re: Javascript and extra zeros.

      On 12 Jan 2004 18:04:44 -0800, Tank <cneu@neuagency .com> wrote:

      <snipped description and code>
      [color=blue]
      > 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.j pg" but it keeps dropping the extra zeros and gives
      > me "dscf1.jpg"[/color]

      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'.subs tr(
      0,
      paddedLength - String( number ).length ) + String( number );
      }

      [color=blue]
      > 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.[/color]

      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\\R eadme.txt" );

      That would display, C:\Windows\Read me.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.
      [color=blue]
      > 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.[/color]

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

      Hope that helps,
      Mike

      --
      Michael Winter
      M.Winter@blueyo nder.co.invalid (replace ".invalid" with ".uk" to reply)

      Comment

      • Tank

        #4
        Re: Javascript and extra zeros.

        Michael Winter <M.Winter@bluey onder.co.invali d> wrote in message news:<opr1o0hez k5vklcq@news-text.blueyonder .co.uk>...[color=blue]
        > On 12 Jan 2004 18:04:44 -0800, Tank <cneu@neuagency .com> wrote:
        >
        > <snipped description and code>
        >[color=green]
        > > 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.j pg" but it keeps dropping the extra zeros and gives
        > > me "dscf1.jpg"[/color]
        >
        > 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'.subs tr(
        > 0,
        > paddedLength - String( number ).length ) + String( number );
        > }
        >
        >[color=green]
        > > 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.[/color]
        >
        > 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\\R eadme.txt" );
        >
        > That would display, C:\Windows\Read me.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.
        >[color=green]
        > > 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.[/color]
        >
        > Don't be. It's far better, and much more preferable, than: "This doesn't
        > work. Why?"
        >
        > Hope that helps,
        > Mike[/color]


        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.

        Comment

        • Brian Genisio

          #5
          Re: Javascript and extra zeros.

          [color=blue]
          >
          > 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
          >[/color]

          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

          Comment

          • Dr John Stockton

            #6
            Re: Javascript and extra zeros.

            JRS: In article <64670f11.04011 21804.a705088@p osting.google.c om>, seen
            in news:comp.lang. javascript, Tank <cneu@neuagency .com> posted at Mon,
            12 Jan 2004 18:04:44 :-[color=blue]
            >
            >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.j pg" but it keeps dropping the extra zeros and gives
            >me "dscf1.jpg"[/color]


            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.

            --
            © John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v4.00 IE 4 ©
            <URL:http://jibbering.com/faq/> Jim Ley's FAQ for news:comp.lang. javascript
            <URL:http://www.merlyn.demo n.co.uk/js-index.htm> jscr maths, dates, sources.
            <URL:http://www.merlyn.demo n.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.

            Comment

            • Tank

              #7
              Re: Javascript and extra zeros.

              Dr John Stockton <spam@merlyn.de mon.co.uk> wrote in message news:<i4r0ioAAY BBAFwkc@merlyn. demon.co.uk>...[color=blue]
              > JRS: In article <64670f11.04011 21804.a705088@p osting.google.c om>, seen
              > in news:comp.lang. javascript, Tank <cneu@neuagency .com> posted at Mon,
              > 12 Jan 2004 18:04:44 :-[color=green]
              > >
              > >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.j pg" but it keeps dropping the extra zeros and gives
              > >me "dscf1.jpg"[/color]
              >
              >
              > 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.[/color]


              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.

              Comment

              • Michael Winter

                #8
                Re: Javascript and extra zeros.

                On 17 Jan 2004 19:23:22 -0800, Tank <isa16w16w@yaho o.com> wrote:
                [color=blue]
                > Dr John Stockton <spam@merlyn.de mon.co.uk> wrote in message
                > news:<i4r0ioAAY BBAFwkc@merlyn. demon.co.uk>...[/color]

                <snip>
                [color=blue][color=green]
                >> 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 }[/color][/color]

                <snip>
                [color=blue]
                > 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.[/color]

                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

                --
                Michael Winter
                M.Winter@blueyo nder.co.invalid (replace ".invalid" with ".uk" to reply)

                Comment

                • Tank

                  #9
                  Re: Javascript and extra zeros.

                  Dr John Stockton <spam@merlyn.de mon.co.uk> wrote in message news:<i4r0ioAAY BBAFwkc@merlyn. demon.co.uk>...[color=blue]
                  > JRS: In article <64670f11.04011 21804.a705088@p osting.google.c om>, seen
                  > in news:comp.lang. javascript, Tank <cneu@neuagency .com> posted at Mon,
                  > 12 Jan 2004 18:04:44 :-[color=green]
                  > >
                  > >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.j pg" but it keeps dropping the extra zeros and gives
                  > >me "dscf1.jpg"[/color]
                  >
                  >
                  > 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.[/color]

                  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.

                  Comment

                  • Tank

                    #10
                    Re: Javascript and extra zeros.

                    Michael Winter <M.Winter@bluey onder.co.invali d> wrote in message news:<opr1ydj5b e5vklcq@news-text.blueyonder .co.uk>...[color=blue]
                    > On 17 Jan 2004 19:23:22 -0800, Tank <isa16w16w@yaho o.com> wrote:
                    >[color=green]
                    > > Dr John Stockton <spam@merlyn.de mon.co.uk> wrote in message
                    > > news:<i4r0ioAAY BBAFwkc@merlyn. demon.co.uk>...[/color]
                    >
                    > <snip>
                    >[color=green][color=darkred]
                    > >> 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 }[/color][/color]
                    >
                    > <snip>
                    >[color=green]
                    > > 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.[/color]
                    >
                    > 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[/color]

                    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...

                    Comment

                    • Tank

                      #11
                      Re: Javascript and extra zeros.

                      Please someone help I have been put on the right path but i suck at this stuff.

                      Comment

                      Working...