Computing.Net > Forums > Programming > Batch file copy and Rename

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Batch file copy and Rename

Reply to Message Icon

Name: rnharris
Date: April 17, 2009 at 05:49:20 Pacific
OS: Windows 2000
Subcategory: Batch
Comment:

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. :-)



Sponsored Link
Ads by Google

Response Number 1
Name: Valerie (by Garibaldi)
Date: April 17, 2009 at 17:07:27 Pacific
Reply:

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?


0

Response Number 2
Name: rnharris
Date: April 18, 2009 at 00:31:40 Pacific
Reply:

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


0

Response Number 3
Name: rnharris
Date: April 18, 2009 at 00:35:52 Pacific
Reply:

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


0

Response Number 4
Name: Valerie (by Garibaldi)
Date: April 18, 2009 at 17:52:13 Pacific
Reply:

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


0

Response Number 5
Name: rnharris
Date: April 19, 2009 at 02:09:44 Pacific
Reply:

Great. I'll try to code later today and respond with results.

Thanks very much.

Richard


0

Related Posts

See More



Response Number 6
Name: rnharris
Date: April 20, 2009 at 06:09:30 Pacific
Reply:

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


0

Response Number 7
Name: rnharris
Date: April 20, 2009 at 06:12:30 Pacific
Reply:

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


0

Response Number 8
Name: Valerie (by Garibaldi)
Date: April 20, 2009 at 14:21:41 Pacific
Reply:

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.


0

Response Number 9
Name: rnharris
Date: April 21, 2009 at 11:22:31 Pacific
Reply:

Thanks again.

YGM!


0

Response Number 10
Name: Valerie (by Garibaldi)
Date: April 21, 2009 at 14:51:19 Pacific
Reply:

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.


0

Response Number 11
Name: rnharris
Date: April 21, 2009 at 15:49:58 Pacific
Reply:

Done.

Donation made to the following organization:

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

in your name.
Thanks,
Richard


0

Response Number 12
Name: Valerie (by Garibaldi)
Date: April 21, 2009 at 18:58:05 Pacific
Reply:

Thank you, it's been pleasure collaborating with you, I'm sure the donation made will be appreciated.

Valerie.


0

Sponsored Link
Ads by Google
Reply to Message Icon






Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: Batch file copy and Rename

BATCH to copy and rename files www.computing.net/answers/programming/batch-to-copy-and-rename-files/15221.html

Copy and Rename Batch File www.computing.net/answers/programming/copy-and-rename-batch-file/15297.html

Copy and rename mmost recent file www.computing.net/answers/programming/copy-and-rename-mmost-recent-file/19636.html