1. Computer problem? Tech Support Guy is completely free -- paid for by advertisers and donations. Click here to join today! If you're new to Tech Support Guy, we highly recommend that you visit our Guide for New Members.

Solved: Batch file to copy file and rename from the middle

Discussion in 'DOS/Other' started by golsen, Jan 4, 2013.

Thread Status:
Not open for further replies.
Advertisement
  1. golsen

    golsen Thread Starter

    Joined:
    Jan 4, 2013
    Messages:
    6
    I am new to writing a batch, I am going to be as specific as I can.

    I need help writing a file that will copy a file from a location then rename it in another location with the middle of the first file name. Then I want to delete the original file.

    For example

    This is the location

    copy"x:\Datasmart\FTP Downloads\Caremark\PAL 4000 D.0\FILE.CNT401.FIBMAHC.DT010113.TM163552\FIBMAHC" "x:\Datasmart\FTP Downloads\Caremark\PAL 4000 D.0\FIBMAHC.DT010113"

    This is where I was originally copying the file and renaming it. So here is my batch I want to do the same process but I want to find all the FILE.CNT401 files and rename the file within them with the extension that is listed after CNT401.

    Then I want to delete the existing file "File CNT401" with the name after it is completed.

    Any help on this would be greatly appreciated, I am stuck in writing the batch for this

    Thanks

    Greg
     
  2. foxidrive

    foxidrive Banned

    Joined:
    Oct 20, 2012
    Messages:
    793
    Is this a file or a folder? We have to be sure.

    x:\Datasmart\FTP Downloads\Caremark\PAL 4000 D.0\FILE.CNT401.FIBMAHC.DT010113.TM163552\FIBMAHC

    Do you want to copy all files in this folder
    x:\Datasmart\FTP Downloads\Caremark\PAL 4000 D.0\FILE.CNT401.FIBMAHC.DT010113.TM163552\
    to this folder
    x:\Datasmart\FTP Downloads\Caremark\PAL 4000 D.0\
    but to rename them in the format you described?
     
  3. golsen

    golsen Thread Starter

    Joined:
    Jan 4, 2013
    Messages:
    6
    FILE.CNT401 is a folder, FIBMAHC is a file so when I copied that file FIBMAHC it is renamed at the same parent level as FILE.CNT401... THen I need to delete the original folder FILE.CNT.401

    I am trying to write it so it makes sense they way I see it i guess.

    Thanks Greg
     
  4. golsen

    golsen Thread Starter

    Joined:
    Jan 4, 2013
    Messages:
    6
    I need a batch that will Identify the folder FILE.CNT401.????.???? with the associated file in it and rename to x:\Datasmart\FTP Downloads\Caremark\PAL 4000 D.0\

    with the correct naming convention that I stated..

    Thanks for looking at this so quickly

    Greg
     
  5. golsen

    golsen Thread Starter

    Joined:
    Jan 4, 2013
    Messages:
    6
    AND that was a yes to your second part but I need to pick up the later part of the folder FILE.CNT401.FIBMAHC.DT010113.TM163552 and rename the new file that was within with the FIBMAHC.DT010113.TM163552 at location

    x:\Datasmart\FTP Downloads\Caremark\PAL 4000 D.0\

    I hope this helps

    Thanks Greg
     
  6. foxidrive

    foxidrive Banned

    Joined:
    Oct 20, 2012
    Messages:
    793
    This searches x:\Datasmart\FTP Downloads\Caremark\PAL 4000 D.0\ for folders with FILE.CNT401 in the name, and then expects a single file to be in every folder. It is designed to copy that file to
    x:\Datasmart\FTP Downloads\Caremark\PAL 4000 D.0\ with the file format you mentioned, taken from the folder name, and then delete the file in the holding folder.

    So that you can test this batch file it will merely print to the screen the copy command and the del command.
    To make it perform the copy and deletion you need to remove the two echo statements, if the commands look right to you.

    EDIT: I changed the code to do a rename and move. That is much faster for large files.

    Code:
    @echo off
    for /f "delims=" %%a in (' dir "x:\Datasmart\FTP Downloads\Caremark\PAL 4000 D.0\FILE.CNT401*" /b /ad /s ') do (
    for /f "delims=" %%b in ('  dir "%%a" /b /a-d ') do (
    for /f "tokens=3,4 delims=." %%c in ("%%~nxa") do (
    echo ren "%%a\%%b" "%%c.%%d"
    echo move "%%a\%%c.%%d" "x:\Datasmart\FTP Downloads\Caremark\PAL 4000 D.0\"
    )
    )
    )
    pause
     
  7. golsen

    golsen Thread Starter

    Joined:
    Jan 4, 2013
    Messages:
    6
    When I ran @echo off
    for /f "delims=" %%a in (' dir "x:\Datasmart\FTP Downloads\Caremark\PAL 4000 D.0\FILE.CNT401*" /b /ad /s ') do (
    for /f "delims=" %%b in (' dir "%%a" /b /a-d ') do (
    for /f "tokens=3,4 delims=." %%c in ("%%~nxa")
    )
    )
    pause

    it didn't create a new file nor did it delete the old one so I am guessing it is a file path problem on my part,

    I will work on it you gave me a great point to finish up from,

    I wanted to ask will this help me keep just the middle of the file name such as FIBMAHC.DT010113 or do I need to add something to delete the ending?

    Thanks Greg
     
  8. foxidrive

    foxidrive Banned

    Joined:
    Oct 20, 2012
    Messages:
    793
    Run the batch file below and if it prints the right commands to the screen then remove ONLY the "echo" in three places. I added a command to remove the folder after the file move.



    Code:
    @echo off
    for /f "delims=" %%a in (' dir "x:\Datasmart\FTP Downloads\Caremark\PAL 4000 D.0\FILE.CNT401*" /b /ad /s ') do (
    for /f "delims=" %%b in ('  dir "%%a" /b /a-d ') do (
    for /f "tokens=3,4 delims=." %%c in ("%%~nxa") do (
    echo ren "%%a\%%b" "%%c.%%d"
    echo move "%%a\%%c.%%d" "x:\Datasmart\FTP Downloads\Caremark\PAL 4000 D.0\"
    echo rd "%%a"
    )
    )
    )
    pause
     
  9. golsen

    golsen Thread Starter

    Joined:
    Jan 4, 2013
    Messages:
    6
    Thanks that works great
     
  10. Sponsor

As Seen On
As Seen On...

Welcome to Tech Support Guy!

Are you looking for the solution to your computer problem? Join our site today to ask your question. This site is completely free -- paid for by advertisers and donations.

If you're not already familiar with forums, watch our Welcome Guide to get started.

Join over 733,556 other people just like you!

Thread Status:
Not open for further replies.

Short URL to this thread: https://techguy.org/1083738

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice