Copying and renaming file

Posted on 2012-08-20
Last Modified: 2012-08-27
We want to create a batch file which can do following

1) It looks for lastest modified file (that file is text file) in directory. That filename is in format of   "Retail Operations16Aug2012083512" obviously with latest date everyday
2) Copy that file it to a sub folder.
3) Then after renaming the original file to POSJNL.txt

Can above be doable? Even if you can send the rough batch file and will modify accordingly our environment.
Question by:ArcIT
    LVL 21

    Expert Comment

    by:David Atkin
    This may start you off:

    "Windows shell, one liner:

    FOR /F %%I IN ('DIR *.* /B /O:-D') DO COPY %%I <<NewDir>> & EXIT"

    It does not rename the old file though.
    LVL 12

    Expert Comment

    Assuming above is a Script executed via cScript. You can call this call from a bat file and after calling this file, on next line you can use ren command to rename the file which is a simple dos command.

    Hope it helps.
    LVL 43

    Expert Comment

    by:Steve Knight
    Well to get the latest file in a dir that matches the easiest way is to ignore the actual filename and gets the first entry in a dir listing:

    @Echo off
    set sourcedir=c:\mysource\dir\name
    set destfile=C:\mydest\dir\name\posjnl.txt
    pushd "%sourcedir%
    for /f "tokens=*" %%f in ('dir /b /a-d /o-d *.txt') do copy /y "%%~f" "%destfile%" & goto :end
    echo Completed

    Open in new window

    The for command looks at a dir listing - /b = bare list, /a-d = only files, /o-d = date order newest first.
    It takes the first entry runs the copy command into your destfile name then ends the loop.


    LVL 43

    Expert Comment

    by:Steve Knight
    Hmm delayed in hitting submit there!


    Author Comment

    @ -Scorpeo-

    FOR /F %%I IN ('DIR *.* /B /O:-D') DO COPY %%I <<NewDir>> & EXIT"

    whats %%I ? and do we run above code simply in windows command prompt?

    e.g file name is Retail Operations19Aug2012083511.txt and location is "\\SERVERNAME\Infogenesis\Export\Great Plains"

    and want to copy to folder called "\\SERVERNAME\Infogenesis\Export\AUG 2012"
    LVL 43

    Expert Comment

    by:Steve Knight
    See my example.  The For command in this way takes the output of the DIR command (in his case it will also contain sub directory names and all files not just text files) and each name gets pushed into the variable %%I one at a time which can then be run by the bit after the "DO".

    This one won't work as it stands because:

    1. It will not work with filenames with spaces, commas etc. in as the FOR command will split the filename up at it's default delimiters
    2. It may pick a directory name not a filename for you

    Please save my example as "something.cmd" in notepad - When you save change it from "Text files" to "All files" in the file type to make sure it doesn't helpfully add ".txt" onto the end of the name.  Then you can run from explorer, command prompt (cmd.exe), scheduled,  or shortcut on desktop etc.

    LVL 51

    Accepted Solution

    Based on what you've described I think this should do the whole job.  The only thing I'm not sure of (since you didn't specify) is if the destination folder with the month and year in it, needs to automatically be chosen based on the file name?  Save this as a BAT file.

    @echo off
    set "BaseDir=\\SERVERNAME\Infogenesis\Export\Great Plains"
    set "DestDir=\\SERVERNAME\Infogenesis\Export\AUG 2012"
    set "NewName=posjnl.txt"
    for /F "tokens=*" %%F in ('dir /B /A-D /O-D "%BaseDir%"') do (
      copy /Y "%%~F" "%DestDir%"
      ren /Y "%%~F" "%NewName%"
      goto :End

    Open in new window


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Maximize Your Threat Intelligence Reporting

    Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

    When you receive another warning that your shared drive is almost full and you have asked your users to clean out old files again and again, here is a single command that may help. This command will place all the files that have not been used rec…
    If like me you are one who spends a lot of time working and scripting with cmd.exe, sometimes it is handy to be able to quickly view a calendar for a given month and year. This script will quickly do just that!  Save the code posted below to a .bat …
    Internet Business Fax to Email Made Easy - With eFax Corporate (, you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
    This video discusses moving either the default database or any database to a new volume.

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    22 Experts available now in Live!

    Get 1:1 Help Now