Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Windows Forms
  4. Copying last created file in a directory

Copying last created file in a directory

Scheduled Pinned Locked Moved Windows Forms
question
7 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Muammar
    wrote on last edited by
    #1

    This works:

    FOR /F "delims=" %%I IN ('DIR . /B /O:-D') DO COPY "%%I" .\t.xls & GOTO :END
    :END

    however, it takes the date of modification instead of creation, hence, if an old file in that folder got edited, it takes it! Any ideas?? Thanks.

    M A 2 Replies Last reply
    0
    • M Muammar

      This works:

      FOR /F "delims=" %%I IN ('DIR . /B /O:-D') DO COPY "%%I" .\t.xls & GOTO :END
      :END

      however, it takes the date of modification instead of creation, hence, if an old file in that folder got edited, it takes it! Any ideas?? Thanks.

      M Offline
      M Offline
      MicroVirus
      wrote on last edited by
      #2

      Look into Robocopy, if your target is Vista and above. Else, there might be freeware utilities that can do what you need. Finally, you could probably code it yourself, using the FileInfo and related classes. SetCreationTime could be used to set the creation date on the copied file to the creation date of the source file.

      modified on Tuesday, August 9, 2011 8:06 PM

      1 Reply Last reply
      0
      • M Muammar

        This works:

        FOR /F "delims=" %%I IN ('DIR . /B /O:-D') DO COPY "%%I" .\t.xls & GOTO :END
        :END

        however, it takes the date of modification instead of creation, hence, if an old file in that folder got edited, it takes it! Any ideas?? Thanks.

        A Offline
        A Offline
        Alan N
        wrote on last edited by
        #3

        Hi You've missed the timefield switch /T So I reckon you want

        DIR . /B /O:-D /T:C

        DIR [drive:][path][filename] [/A[[:]attributes]] [/B] [/C] [/D] [/L] [/N]
        [/O[[:]sortorder]] [/P] [/Q] [/S] [/T[[:]timefield]] [/W] [/X] [/4]
        [drive:][path][filename]

        /O List by files in sorted order.
        sortorder N By name (alphabetic) S By size (smallest first)
        E By extension (alphabetic) D By date/time (oldest first)
        G Group directories first - Prefix to reverse order
        /T Controls which time field displayed or used for sorting
        timefield C Creation
        A Last Access
        W Last Written

        M 2 Replies Last reply
        0
        • A Alan N

          Hi You've missed the timefield switch /T So I reckon you want

          DIR . /B /O:-D /T:C

          DIR [drive:][path][filename] [/A[[:]attributes]] [/B] [/C] [/D] [/L] [/N]
          [/O[[:]sortorder]] [/P] [/Q] [/S] [/T[[:]timefield]] [/W] [/X] [/4]
          [drive:][path][filename]

          /O List by files in sorted order.
          sortorder N By name (alphabetic) S By size (smallest first)
          E By extension (alphabetic) D By date/time (oldest first)
          G Group directories first - Prefix to reverse order
          /T Controls which time field displayed or used for sorting
          timefield C Creation
          A Last Access
          W Last Written

          M Offline
          M Offline
          Muammar
          wrote on last edited by
          #4

          Thanks mate!

          1 Reply Last reply
          0
          • A Alan N

            Hi You've missed the timefield switch /T So I reckon you want

            DIR . /B /O:-D /T:C

            DIR [drive:][path][filename] [/A[[:]attributes]] [/B] [/C] [/D] [/L] [/N]
            [/O[[:]sortorder]] [/P] [/Q] [/S] [/T[[:]timefield]] [/W] [/X] [/4]
            [drive:][path][filename]

            /O List by files in sorted order.
            sortorder N By name (alphabetic) S By size (smallest first)
            E By extension (alphabetic) D By date/time (oldest first)
            G Group directories first - Prefix to reverse order
            /T Controls which time field displayed or used for sorting
            timefield C Creation
            A Last Access
            W Last Written

            M Offline
            M Offline
            Muammar
            wrote on last edited by
            #5

            Thank you mate, you've helped a lot and you sound like an expert in DOS so I thought maybe you can assist with this.. The file to be copies has a name format of "PO 123.2011.txt" Now it is copied, is it possible to generate the next name (i.e. "PO 124.2011") probably using an expression for the math, concatenation and storing it into a temporary variable?? I'm using this for now..

            FOR /F "delims=" %%I IN ('DIR . /B /O:-D /T:C') DO COPY "%%I" .\new.txt & GOTO :END
            :END
            START /MAX new.txt

            Many thanks mate!

            A 1 Reply Last reply
            0
            • M Muammar

              Thank you mate, you've helped a lot and you sound like an expert in DOS so I thought maybe you can assist with this.. The file to be copies has a name format of "PO 123.2011.txt" Now it is copied, is it possible to generate the next name (i.e. "PO 124.2011") probably using an expression for the math, concatenation and storing it into a temporary variable?? I'm using this for now..

              FOR /F "delims=" %%I IN ('DIR . /B /O:-D /T:C') DO COPY "%%I" .\new.txt & GOTO :END
              :END
              START /MAX new.txt

              Many thanks mate!

              A Offline
              A Offline
              Alan N
              wrote on last edited by
              #6

              Yes, I'd say it's possible to generate the next filename using batch commands but probably not advisable as it is going to be difficult to guarantee any kind of robustness. Here's something that will do it but you'll find out that it's a bit of a train wreck with anything other than the expected input format.

              @echo off
              REM generate next PO NNN.2011.txt where NNN is a number

              SET FILENAME=PO 156.2011.txt
              REM SET FILENAME=PO ABC.2011.txt
              ECHO Initial filename %FILENAME%

              REM get 2nd token using '.' and ' ' delimiters
              for /F "tokens=2 delims=. " %%a in ("%FILENAME%") do SET /a NNN=%%a

              REM what did we get
              ECHO NNN %NNN%

              REM Increment the value
              SET /a NNN=%NNN% + 1
              ECHO NNN+1 = %NNN%

              SET FILENAME=PO %NNN%.2011.txt
              ECHO New filename %FILENAME%
              PAUSE

              We can do a little bit better by attempting to check if the extracted token is actually numeric and then bailing out if something really bad has happened.

              @echo off
              REM generate next PO NNN.2011.txt where NNN is a number

              SET FILENAME=PO 156.2011.txt
              REM SET FILENAME=PO ABC.2011.txt
              ECHO Initial filename %FILENAME%

              REM get 2nd token using '.' and ' ' delimiters
              REM Store token as both number and text
              for /F "tokens=2 delims=. " %%a in ("%FILENAME%") do SET /a NNN=%%a & SET TEXT=%%a

              REM what did we get
              ECHO NNN %NNN% TEXT %TEXT%

              REM Should be the same if the token is a decimal number
              IF %NNN%==%TEXT% (
              ECHO Yippee, got a number

              REM Increment the value
              SET /a NNN=%NNN% + 1

              ECHO NNN+1 = %NNN%
              SET FILENAME=PO %NNN%.2011.txt

              ECHO New filename %FILENAME%
              ) ELSE (
              ECHO Darn it, got a non numeric token
              )

              PAUSE

              Have fun! Alan.

              M 1 Reply Last reply
              0
              • A Alan N

                Yes, I'd say it's possible to generate the next filename using batch commands but probably not advisable as it is going to be difficult to guarantee any kind of robustness. Here's something that will do it but you'll find out that it's a bit of a train wreck with anything other than the expected input format.

                @echo off
                REM generate next PO NNN.2011.txt where NNN is a number

                SET FILENAME=PO 156.2011.txt
                REM SET FILENAME=PO ABC.2011.txt
                ECHO Initial filename %FILENAME%

                REM get 2nd token using '.' and ' ' delimiters
                for /F "tokens=2 delims=. " %%a in ("%FILENAME%") do SET /a NNN=%%a

                REM what did we get
                ECHO NNN %NNN%

                REM Increment the value
                SET /a NNN=%NNN% + 1
                ECHO NNN+1 = %NNN%

                SET FILENAME=PO %NNN%.2011.txt
                ECHO New filename %FILENAME%
                PAUSE

                We can do a little bit better by attempting to check if the extracted token is actually numeric and then bailing out if something really bad has happened.

                @echo off
                REM generate next PO NNN.2011.txt where NNN is a number

                SET FILENAME=PO 156.2011.txt
                REM SET FILENAME=PO ABC.2011.txt
                ECHO Initial filename %FILENAME%

                REM get 2nd token using '.' and ' ' delimiters
                REM Store token as both number and text
                for /F "tokens=2 delims=. " %%a in ("%FILENAME%") do SET /a NNN=%%a & SET TEXT=%%a

                REM what did we get
                ECHO NNN %NNN% TEXT %TEXT%

                REM Should be the same if the token is a decimal number
                IF %NNN%==%TEXT% (
                ECHO Yippee, got a number

                REM Increment the value
                SET /a NNN=%NNN% + 1

                ECHO NNN+1 = %NNN%
                SET FILENAME=PO %NNN%.2011.txt

                ECHO New filename %FILENAME%
                ) ELSE (
                ECHO Darn it, got a non numeric token
                )

                PAUSE

                Have fun! Alan.

                M Offline
                M Offline
                Muammar
                wrote on last edited by
                #7

                Thank you Alan! That was very helpful.

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups