Batch file copy and Rename

April 17, 2009 at 05:49:20
Specs: Windows 2000
Greetings.

I need assistance creating a batch file that will rename a file in a directory to include the directory name.

For Example:

Current Directory view

z:\Orders <----- Directory name
55K139AA<---- Sub Directory Name
B13.dxf <---- File Name
B14.dxf <-----File Name
B16.dxf <----File Name

55K151AA<---- Sub Directory Name
B13.dxf <--------File Name
B14.dxf <--------FIle Name

Desired results

z:\Orders <----- Directory name
55K139AA<---- Sub Directory Name
55K139AAB13.dxf <---- File Name
55K139AAB14.dxf <-----File Name
55K139AA B16.dxf <----File Name

55K151AA<---- Sub Directory Name
55K151AAB13.dxf <--------File Name
55K151AAB14.dxf <--------FIle Name


Thanks for any assistance. I'm willing to Paypal a few $$ to examine for a little code. :-)


See More: Batch file copy and Rename

Report •


#1
April 17, 2009 at 17:07:27
Is 55K151AA a subdirectory of 55K139AA or is it a subdirectory of Orders.

"Batch file Copy and Rename" is the subject of your post but the source and destination directory\subdirectories names don't change, do you want the files copied into the same directories but with the new names or renamed in their current directories?


Report •

#2
April 18, 2009 at 00:31:40
55K151AA is a subdirectory of orders. Orders is the main directory, which has many subdirectories with in it. There are two directory levels; Orders, the main directory and a subdirectory which begins with a number sequence. The subdirectory name(55K139AA) represents an order number.

As in the example above, sometimes both subdirectories, 55K151AA and 55K139AA, may have files(B13.dxf) with same name. Although these files have the same name, they have different contents, and, more importantly, represent different parts. To prevent confusion and to make orders easier to track, creating new file name that consist of the file name appended to the directory(Order#) will make to build process more efficient and life easier for everyone.

The files need to be renamed in their existing directories. Once the files are renamed, I don't want the old names listed in the directories. If creating new directory tree called "Orders2" with the correct subdirectories and new filenames contain within is easier to perform, that will work also.

Thanks,

Richard


Report •

#3
April 18, 2009 at 00:35:52
55K151AA and of 55K139AA are both subdirectories of Orders. There are no files contained directly within the "Orders"directory. All files are located within the subdirectories, which represent order numbers. The file names represent different parts.

HTH,

Richard


Report •

Related Solutions

#4
April 18, 2009 at 17:52:13
Thank you. The following script should rename the files in their existing directory/subdirectories. You should test it on your system before committing.

:: Code begins...
@echo off
cls
setlocal enabledelayedexpansion

set level1=z:\orders\

for /f %%1 in ('dir /ad /b %level1%') do (
    set level2=%%1

    pushd !level1!!level2!

    for /f %%2 in ('dir /b') do (
    ren %%2 !level2!%%2
    )

    popd
)

echo.&echo.&echo.
echo                      Renaming of files is complete.
echo.
echo                      Press any key to continue.....
pause > nul
cls

exit/b
:: Code ends...

The following script should Copy sub-directories to a new directory on Z: named Orders2 and rename files. No changes are made to source filenames. The directory and sub-directories will be created if they do not already exist. Again up to you to fully test.

:: Code begins...
@echo off
cls
setlocal enabledelayedexpansion

set level1=z:\orders\
set level1a=z:\orders2\

if not exist %level1a% md %level1a%

echo.&echo.&echo.
echo                      Please wait while files are copied...


for /f %%1 in ('dir /ad /b %level1%') do (
    set level2=%%1
    set /a directs +=1
    if not exist !level1a!!level2! md !level1a!!level2!
    pushd !level1!!level2!


    for /f %%2 in ('dir /b') do (
    copy %%2 !level1a!\!level2!\!level2!%%2 > nul
    set /a fils +=1
    )
    popd
)

cls
echo.&echo.&echo.
echo                      Copying of files is complete.
echo.
echo                      Sub-Directories = %directs%
echo                      Files copied    = %fils%
echo.
echo                      Press any key to continue.....
pause > nul
cls

exit/b
:: Code ends...

Good luck

V.Mc


Report •

#5
April 19, 2009 at 02:09:44
Great. I'll try to code later today and respond with results.

Thanks very much.

Richard


Report •

#6
April 20, 2009 at 06:09:30
V. mc -

It Works! I tested on the production system this evening. Tomorrow I'll show it to the sales manager to make sure this is what he wanted.

I'll be in touch tomorrow.
Thanks again.
R


Report •

#7
April 20, 2009 at 06:12:30
V. Mc

Two things.

1)Would you modify the file to insert a hyphen between the directory name and file name.

From this -
55D125KAB13.dxf

To this -
55D125KA-B13.dxf

2) Now that all of the files have a unique name, a script to copy/move the files to one directory.

Thanks,

Richard


Report •

#8
April 20, 2009 at 14:21:41
1) In the script to RENAME files change this line:

ren %%2 !level2!%%2

to:

ren %%2 !level2!-%%2

In the script to COPY the files change this line:

copy %%2 !level1a!\!level2!\!level2!%%2 > nul

to:

copy %%2 !level1a!\!level2!\!level2!-%%2 > nul

(In both cases note the addition of a hyphen between !level2! and %%2)

2) The following script will copy all files from the Orders2 sub-directories to a new directory. To name this new directory change the Level1a variable content to whatever you want, it's currently z:\newfolder\

Code begins...
@echo off
cls
setlocal enabledelayedexpansion

set level1=z:\orders2\
set level1a=z:\newfolder\

if not exist %level1a% md %level1a%

echo.&echo.&echo.
echo                      Please wait while files are copied...


for /f %%1 in ('dir /ad /b %level1%') do (
    set level2=%%1
    set /a directs +=1
    pushd !level1!!level2!


    for /f %%2 in ('dir /b') do (
    copy %%2 !level1a! > nul
    set /a fils +=1
    )
    popd
)

cls
echo.&echo.&echo.
echo                      Copying of files is complete.
echo.
echo                      Sub-Directories = %directs%
echo                      Files copied    = %fils%
echo.
echo                      Press any key to continue.....
pause > nul
cls

exit/b

:: Code ends...

As usual it's up to you to test & confirm.

V.McI.


Report •

#9
April 21, 2009 at 11:22:31
Thanks again.

YGM!


Report •

#10
April 21, 2009 at 14:51:19
I take it we're done so I want to take you up on your offer, or your company's offer:

I'm willing to Paypal a few $$ to examine for a little code.

If you feel my efforts are worth it please make a donation to your local Breast Cancer Support Group or to any Cancer Research Foundation of your choice.

Thanks

V. McI.


Report •

#11
April 21, 2009 at 15:49:58
Done.

Donation made to the following organization:

http://www.komenlexington.org/site/...

in your name.
Thanks,
Richard


Report •

#12
April 21, 2009 at 18:58:05
Thank you, it's been pleasure collaborating with you, I'm sure the donation made will be appreciated.

Valerie.


Report •


Ask Question