BAT file to copy files with incremental number in the filename !?


GeneralLee01

Well-known member
VIP
Local time
12:33 AM
Posts
175
OS
Windows 11 Home 64-bit
Good morning all,

It has been over 35 years ago that I was working on BAT files, so I forgot more than I ever knew about those. :-)

I am trying to copy all XLSB files (with different file names that might change day by day) from directory TEST to directory ONEDRIVE

But when the destination file already exists I want to add a number to the filename.

My intention is to run this file on every re-boot.

As an example I used the file FruitList.xlsb and the desired result in the ONEDRIVE directory should be:
First time copying: FruitList.xlsb
Second time copying: FruitList(1).xlsb
Second time copying: FruitList(2).xlsb
et cetera

This is the BAT code I used:
Batch:
@echo off
set Source=D:\Test\*.xlsb
set Destination=D:\OneDrive\
set Filename=*.xlsb
set a=1

:loop
if exist %Destination%\%Filename%(%a%).xlsb set /a a+=1 && goto :loop
copy %Source% %Destination%\%Filename%(%a%).xlsb
@echo on

DIR /B D:\OneDrive\*.xlsb

PAUSE

Below the result:
D:\Test\FruitList.xlsb
1 file(s) copied.

D:\Desktop>DIR /B D:\OneDrive\*.xlsb
FruitList.xlsb
FruitList.xlsb(1).xlsb
FruitList.xlsb(2).xlsb

D:\Desktop>PAUSE
Press any key to continue . . .

As one can see it basically works ok . . . BUT the extention part is not as desired.

All help will be appreciated

Thanks
 
Last edited:

My Computer

System One

  • OS
    Windows 11 Home 64-bit
    Computer type
    Laptop
    Manufacturer/Model
    ASUS Laptop Zenbook Pro 14
    CPU
    Intel Core i9-13900H 2.6 GHz, 24MB 14 cores 20 threads
    Memory
    32GB DDR5
    Graphics Card(s)
    NVIDIA GeForce RTX 4060 and Intel Iris Xe Graphics
    Monitor(s) Displays
    14.5" 2.8K OLED 16:10 120Hz 400nits
    Screen Resolution
    2880 x 1800 pixels
    Hard Drives
    1TB M.2 NVMe™ PCIe® 4.0 Performance SSD
    Mouse
    Bluetooth
    Browser
    Microsoft Edge
    Antivirus
    McAfee , Malwarebytes , Ccleaner
What you need to consider:
- Filenames can include spaces (use delims=|)
- Normally, duplicates begin with (2)
- goto loops break for-loops and if-then statements, use a call routine

This script works for all *.xlsb files found in the Source folder.
Batch:
@echo off
SETLOCAL
set Source=C:\Users\GARLIN\Documents
set Destination=C:\Users\GARLIN\OneDrive

for /f "delims=|" %%f in ('dir /b "%Source%"\*.xlsb') do (
    if not exist "%Destination%\%%f" (
        copy "%Source%\%%f" "%Destination%" > NUL
    ) else (
        set Filename=%%~nf
        set a=2
        call :loop
    )
)

dir /b "%Destination%"\*.xlsb
pause
ENDLOCAL
goto :eof

:loop
if exist "%Destination%\%Filename% (%a%).xlsb" set /a a+=1 && goto :loop
copy "%Source%\%Filename%.xlsb" "%Destination%\%Filename% (%a%).xlsb" > NUL

C:\Users\GARLIN\Documents>Dukes.bat
Test ABC.xlsb
testA.xlsb
testB.xlsb
Press any key to continue . . .

C:\Users\GARLIN\Documents>Dukes.bat
Test ABC (2).xlsb
Test ABC.xlsb
testA (2).xlsb
testA.xlsb
testB (2).xlsb
testB.xlsb
Press any key to continue . . .

C:\Users\GARLIN\Documents>Dukes.bat
Test ABC (2).xlsb
Test ABC (3).xlsb
Test ABC.xlsb
testA (2).xlsb
testA (3).xlsb
testA.xlsb
testB (2).xlsb
testB (3).xlsb
testB.xlsb
Press any key to continue . . .
 
Last edited:

My Computer

System One

  • OS
    Windows 7
  • Like
Reactions: OAT
Thanks Garlin. Will check it out / understand what the file is doing tomorrow.
 

My Computer

System One

  • OS
    Windows 11 Home 64-bit
    Computer type
    Laptop
    Manufacturer/Model
    ASUS Laptop Zenbook Pro 14
    CPU
    Intel Core i9-13900H 2.6 GHz, 24MB 14 cores 20 threads
    Memory
    32GB DDR5
    Graphics Card(s)
    NVIDIA GeForce RTX 4060 and Intel Iris Xe Graphics
    Monitor(s) Displays
    14.5" 2.8K OLED 16:10 120Hz 400nits
    Screen Resolution
    2880 x 1800 pixels
    Hard Drives
    1TB M.2 NVMe™ PCIe® 4.0 Performance SSD
    Mouse
    Bluetooth
    Browser
    Microsoft Edge
    Antivirus
    McAfee , Malwarebytes , Ccleaner

Latest Support Threads

Back
Top Bottom