Page 1 of 2 12 LastLast
Results 1 to 15 of 21
  1. #1
    3 Star Lounger
    Join Date
    Dec 2009
    Location
    Ohio, USA
    Posts
    227
    Thanks
    0
    Thanked 0 Times in 0 Posts
    So just as the title says I'm looking to make a batch file that moves all but the newest 3 files from a folder to an External drive.

    Some background, I'm using Audacity (starting today) to record weekly meetings and I would like to keep only the most recent 3 recordings on my internal hard drive (for space reasons).
    I'm using Win7-32 bit (as it says in my siggy below)
    Current Machine:HP Compaq 6910p with 4GB RAM, Core2Duo @ 2.20 GHz, Mobile Intel 965 Express Chipset Family, Avast free, Malwarebyte's free, TP-Link wireless card (as the built in card has nothing but problems with empty solutions): The card identifies as "Atheros AR922X Wireless Network Adapter". [Not the best machine but it does internet, docs, and vids, and some games (PvZ, Spore)]

  2. #2
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts
    You might look at VBScript and the FileSystemObject. I don't have any sample code for this exact issue, but I'm attaching a script that scans a directory tree for files modified the previous day, builds a list of them, them submits the list to 7-zip to archive them. Although my list is not sorted by date and time, you might be able to piggyback on some of the date comparison code. Maybe.

    [attachment=90233:Archive-Yesterday-Sample_vbs.txt]
    Attached Files Attached Files

  3. #3
    3 Star Lounger
    Join Date
    Dec 2009
    Location
    Ohio, USA
    Posts
    227
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by jscher2000 View Post
    You might look at VBScript and the FileSystemObject. I don't have any sample code for this exact issue, but I'm attaching a script that scans a directory tree for files modified the previous day, builds a list of them, them submits the list to 7-zip to archive them. Although my list is not sorted by date and time, you might be able to piggyback on some of the date comparison code. Maybe.

    [attachment=90233:Archive-Yesterday-Sample_vbs.txt]
    Thanks. I'm afraid I'll have to do a bit of research to understand everything in this (I've only done some very basic batch scripting myself) but, it could prove a valuable reference.
    Current Machine:HP Compaq 6910p with 4GB RAM, Core2Duo @ 2.20 GHz, Mobile Intel 965 Express Chipset Family, Avast free, Malwarebyte's free, TP-Link wireless card (as the built in card has nothing but problems with empty solutions): The card identifies as "Atheros AR922X Wireless Network Adapter". [Not the best machine but it does internet, docs, and vids, and some games (PvZ, Spore)]

  4. #4
    Super Moderator BATcher's Avatar
    Join Date
    Feb 2008
    Location
    A cultural area in SW England
    Posts
    3,421
    Thanks
    33
    Thanked 195 Times in 175 Posts
    I don't have time to program this until later today, but the principle is to use the parameters of DIR to sort files in descending order of 'file modified' timestamp, and, in the copying subroutine, just ignore the first three (newest) files and copy all the rest. If you are familiar with BATch lines like
    Code:
    for /f %%a in ('dir c:\source /o-d /b') do call :maybemove "%%a"
    then you've almost done the work yourself!
    BATcher

    Time prevents everything happening all at once...

  5. #5
    Super Moderator BATcher's Avatar
    Join Date
    Feb 2008
    Location
    A cultural area in SW England
    Posts
    3,421
    Thanks
    33
    Thanked 195 Times in 175 Posts
    Right, here you are!

    Code:
    @echo off
    setlocal
    :: change the next two statements to match what you want
    set srcdir=c:\source
    set tgtdir=c:\target
    if not exist %tgtdir%\*.* md %tgtdir%
    set ctr=0
    for /f "tokens=*" %%a in ('dir "%srcdir%" /o-d /b') do call :maybemove "%%a"
    set /a ctr-=3
    echo %~n0: %ctr% files were moved from %srcdir% to %tgtdir%
    endlocal
    goto :eof
    ::--------------------
    
    :maybemove
    :: increment counter and bypass the three newest files
    set /a ctr+=1
    if %ctr% leq 3 goto :eof
    :: remove the double-quotes from the front and back of the filename
    set fn=%~1
    :: perform the move
    ECHO move /y "%srcdir%\%fn%" "%tgtdir%"
    goto :eof
    Try it; when you are happy that it appears to do what you want, remove the 'ECHO' from in front of the 'move' statement to get it to actually do the work.

    Beware that BATch files are very susceptible to problems with so-called 'poison characters' in filenames, examples being '&' and '%', so you may wish to inspect your filenames and modify them to avoid such.

    I haven't made any provision for the presence of any subdirectories in your source directory, and assume there are none.

    Finally, I would prefer to use XCOPY followed by DEL if the XCOPY errorlevel was zero, rather than MOVE, mainly because MOVE is more fussy. It should be OK here, though.

    Hope this fits your requirements!
    BATcher

    Time prevents everything happening all at once...

  6. The Following User Says Thank You to BATcher For This Useful Post:

    BHarder (2016-04-13)

  7. #6
    3 Star Lounger
    Join Date
    Dec 2009
    Location
    Ohio, USA
    Posts
    227
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by BATcher View Post
    Right, here you are!

    Code:
    @echo off
    setlocal
    :: change the next two statements to match what you want
    set srcdir=c:\source
    set tgtdir=c:\target
    if not exist %tgtdir%\*.* md %tgtdir%
    set ctr=0
    for /f "tokens=*" %%a in ('dir "%srcdir%" /o-d /b') do call :maybemove "%%a"
    set /a ctr-=3
    echo %~n0: %ctr% files were moved from %srcdir% to %tgtdir%
    endlocal
    goto :eof
    ::--------------------
    
    :maybemove
    :: increment counter and bypass the three newest files
    set /a ctr+=1
    if %ctr% leq 3 goto :eof
    :: remove the double-quotes from the front and back of the filename
    set fn=%~1
    :: perform the move
    ECHO move /y "%srcdir%\%fn%" "%tgtdir%"
    goto :eof
    Try it; when you are happy that it appears to do what you want, remove the 'ECHO' from in front of the 'move' statement to get it to actually do the work.

    Beware that BATch files are very susceptible to problems with so-called 'poison characters' in filenames, examples being '&' and '%', so you may wish to inspect your filenames and modify them to avoid such.

    I haven't made any provision for the presence of any subdirectories in your source directory, and assume there are none.

    Finally, I would prefer to use XCOPY followed by DEL if the XCOPY errorlevel was zero, rather than MOVE, mainly because MOVE is more fussy. It should be OK here, though.

    Hope this fits your requirements!
    Thanks, I'll run a test with it later tonight (probably). The file names are going to be dates without slashes so numbers like 10072010 for recording taken on the tenth month, seventh day of the year 2010. In time I may modify this for similar operations. I failed to realize just how advanced a program you can make with batch files. I've been taking Java classes lately and have also done some P-Basic in the past (this is a lot closer to P-Basic of course) so when I read this it actually makes a lot of sense. I know in Java if I just used a user inputted string to select a directory I would have errors because of special characters meaning different things (for instance if I wanted the directory C:\hi\hello in java I'd have to input it as C:\\hi\\hello or it would try to do whatever \h means twice).
    Current Machine:HP Compaq 6910p with 4GB RAM, Core2Duo @ 2.20 GHz, Mobile Intel 965 Express Chipset Family, Avast free, Malwarebyte's free, TP-Link wireless card (as the built in card has nothing but problems with empty solutions): The card identifies as "Atheros AR922X Wireless Network Adapter". [Not the best machine but it does internet, docs, and vids, and some games (PvZ, Spore)]

  8. #7
    WS Lounge VIP
    Join Date
    Dec 2009
    Location
    Earth
    Posts
    8,203
    Thanks
    49
    Thanked 989 Times in 919 Posts
    Here it is on one line if you want to play on the Command line.
    Code:
    for /f "skip=3 tokens=*" %X in ('dir "C:\source}.*" /b /o:-d') do move /y "C:\source\%X" "E:\destination"
    Change the "skip" number if you want more or less than 3 files.

    cheers, Paul

  9. #8
    Super Moderator BATcher's Avatar
    Join Date
    Feb 2008
    Location
    A cultural area in SW England
    Posts
    3,421
    Thanks
    33
    Thanked 195 Times in 175 Posts
    Quote Originally Posted by P T View Post
    Here it is on one line if you want to play on the Command line.
    Code:
    for /f "skip=3 tokens=*" %X in ('dir "C:\source}.*" /b /o:-d') do move /y "C:\source\%X" "E:\destination"
    Change the "skip" number if you want more or less than 3 files.
    A one-liner in the tradition of APL, but you really should have tested it to get out the typo(s)!
    BATcher

    Time prevents everything happening all at once...

  10. #9
    WS Lounge VIP
    Join Date
    Dec 2009
    Location
    Earth
    Posts
    8,203
    Thanks
    49
    Thanked 989 Times in 919 Posts
    Test? What's that? ;-))

    cheers, Paul

  11. #10
    3 Star Lounger
    Join Date
    Dec 2009
    Location
    Ohio, USA
    Posts
    227
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I ran a test and the program seems to work. I actually like the bigger batch file in this case because though it may take a couple more KB's (if that) I can more easily follow it. I've learned over the few years I've been programming that smaller isn't always better, sometimes having something bigger that's easier to follow is a far better choice. This is especially true if you think you'll be recycling the code for other uses or in the case of more advanced programming it's common to update programs for either more functionality or better compatibility.
    Current Machine:HP Compaq 6910p with 4GB RAM, Core2Duo @ 2.20 GHz, Mobile Intel 965 Express Chipset Family, Avast free, Malwarebyte's free, TP-Link wireless card (as the built in card has nothing but problems with empty solutions): The card identifies as "Atheros AR922X Wireless Network Adapter". [Not the best machine but it does internet, docs, and vids, and some games (PvZ, Spore)]

  12. #11
    Super Moderator BATcher's Avatar
    Join Date
    Feb 2008
    Location
    A cultural area in SW England
    Posts
    3,421
    Thanks
    33
    Thanked 195 Times in 175 Posts
    To be fair to Paul, incorporating his skip=3 parameter (if I had thought about it at the time) would have made my code slightly simpler...!
    BATcher

    Time prevents everything happening all at once...

  13. #12
    WS Lounge VIP
    Join Date
    Dec 2009
    Location
    Earth
    Posts
    8,203
    Thanks
    49
    Thanked 989 Times in 919 Posts
    Just go for the jugular, fair is for wooses!!

    cheers, Paul

  14. #13
    New Lounger
    Join Date
    Apr 2016
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    How to move files with a particular extension

    Hi All,

    Greetings !

    The discussion above was quite helpful. I feel ashamed that I am copying the work you good people are doing. But anyways, it is helping me. I can only say Thank You !

    I was just wondering, if we need to move files with a particular extension ( here a notepad - .txt ), what changes should I make to your code ? [Where do I place the .txt]


    @echo off
    setlocal
    :: change the next two statements to match what you want
    set srcdir=c:\source
    set tgtdir=c:\target
    if not exist %tgtdir%\*.* md %tgtdir%
    set ctr=0
    for /f "tokens=*" %%a in ('dir "%srcdir%" /o-d /b') do call :maybemove "%%a"
    set /a ctr-=3
    echo %~n0: %ctr% files were moved from %srcdir% to %tgtdir%
    endlocal
    goto :eof
    ::--------------------


    :maybemove
    :: increment counter and bypass the three newest files
    set /a ctr+=1
    if %ctr% leq 3 goto :eof
    :: remove the double-quotes from the front and back of the filename
    set fn=%~1
    :: perform the move
    move /y "%srcdir%\%fn%" "%tgtdir%"
    goto :eof



    Regards,

  15. #14
    WS Lounge VIP
    Join Date
    Dec 2009
    Location
    Earth
    Posts
    8,203
    Thanks
    49
    Thanked 989 Times in 919 Posts
    You need to add the file filter to the "dir" statement.
    for /f "tokens=*" %%a in ('dir "%srcdir%\*.txt" /o-d /b') do call :maybemove "%%a"

    If you want to put code at the start of the batch file to allow you to easily change the filter copy the "set srcdir" bits and add it to the "dir" command - I'll let you have a go and help if you have questions.

    cheers, Paul

  16. #15
    New Lounger
    Join Date
    Apr 2016
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks so much. Worked perfectly !

    Regards

    Vik

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •