batch copy and Rename

August 19, 2010 at 15:59:05
Specs: Windows XP
Greetings. I'd like some help creating a DOS batch file. The script must rename files in the directory by trimming the last two digits from the file name, copy the files to a new direction. The difficult part is that some of the files in the directory have duplicates, so only the newest file must be trimmed and copied, while discarding the older copies.

Example:
Original Files
2988-0054-0001-A.dxf
2988-0055-0001-A.dxf
2988-0056-0001-A.dxf
2988-0057-0001-A.dxf
2988-0057-0001-B.dxf
2988-0057-0001-C.dxf <- the "C" version is newest
2988-0058-0001-A.dxf
2988-0059-0001-A.dxf
2988-0060-0001-A.dxf

Desired Results
2988-0054-0001.dxf
2988-0055-0001.dxf
2988-0056-0001.dxf
2988-0057-0001.dxf <This being the newest file.
2988-0058-0001.dxf
2988-0059-0001.dxf
2988-0060-0001.dxf


Thanks,
PK


See More: batch copy and Rename

Report •

#1
August 20, 2010 at 00:26:36
Here you go:

@echo off

set orig_path=C:\Users\JohnDoe\Desktop\OriginalFolder
set dest_path=C:\Users\JohnDoe\Desktop\DestinationFolder

set counter=0

for /f "tokens=*" %%a in ('dir /b /s /a-d "%orig_path%\*.dxf"') do set item=%%a & (
    setlocal enabledelayedexpansion
    if exist "!item:~-21,15!B.dxf" (
        if exist "!item:~-21,15!C.dxf" (
            if exist "!item:~-21,15!D.dxf" (
                rem Continue nesting here if needed.
            ) else (
                set newest=!item:~-21,15!C.dxf
            )
        ) else (
            set newest=!item:~-21,15!B.dxf
        )
    ) else (
        set newest=!item!
    )
    copy /y "!newest!" "%dest_path%\!newest:~-21,14!.dxf" >nul
    echo !newest:~-21,21!
    set /a counter+=1   
)

echo.
echo File(s) copied: %counter%
endlocal
echo.
echo Press any key to exit...
pause >nul
exit

This batch won't work if there are files with letter 'D' and up.
But you can continue nesting and make it work, use whole alphabet if needed. :)

I think this is enough for you to continue modifying the script on your own.

Regards


Report •

#2
August 20, 2010 at 01:19:34
An alternative - change paths to suit your setup, not fully tested.

@echo off
cls
setlocal enabledelayedexpansion

pushd input-path\||echo PUSHD failed - job terminated&&exit /b

if not exist %temp%\destination\ md %temp%\destination\

for /f "tokens=*" %%1 in ('dir /a-d /b /tw /od *.dxf') do (
    set oldfilename=%%1
       
        for /f "tokens=1-5 delims=-." %%A in ("!oldfilename!") do (
            set newfilename=%%A-%%B-%%C.%%E

                copy /y !oldfilename! %temp%\destination\!newfilename!>nul
)
   )



Report •

#3
August 20, 2010 at 01:21:30
Ok, Thanks. I'll try test the program and get back to you.

Report •

Related Solutions

#4
August 22, 2010 at 14:53:50
I ran a test against 18K files and works. Would you mind modifying(or creating another) the script to examine the duplicate named files for 'Date Created', rather than, alphabet letter?

For Instance, let say the newest file(based on Date created) in the example above is '2988-0057-0001-B.dxf', instead of 'XXXX-0057-0001-C.dxf'.

Note: The Date Created attribute is available in DOS, Windows XP, 2K and 2K3, but not Windows 7.

Thanks for your help.
PK


Report •

Ask Question