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



## golsen (Jan 4, 2013)

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


----------



## foxidrive (Oct 20, 2012)

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?


----------



## golsen (Jan 4, 2013)

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


----------



## golsen (Jan 4, 2013)

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


----------



## golsen (Jan 4, 2013)

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


----------



## foxidrive (Oct 20, 2012)

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.


```
@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
```


----------



## golsen (Jan 4, 2013)

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


----------



## foxidrive (Oct 20, 2012)

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.


```
@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
```


----------



## golsen (Jan 4, 2013)

Thanks that works great


----------

