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


GeneralLee01

Well-known member
Member
VIP
Local time
8:40 PM
Posts
223
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 My Computer

At a glance

Windows 11 Home 64-bitIntel Core i7 13700H32GB DDR4Intel Iris Xe Graphics
OS
Windows 11 Home 64-bit
Computer type
Laptop
Manufacturer/Model
MSI Laptop - Commercial 14 H A13MG vPRO-098
CPU
Intel Core i7 13700H
Motherboard
MSl Co., Ltd. MS-14L1 (U3E1)
Memory
32GB DDR4
Graphics Card(s)
Intel Iris Xe Graphics
Sound Card
Intel Smart Sound Technology - Realtek High Definition Audio
Monitor(s) Displays
Generic 1920x1200 @ 60Hz
Screen Resolution
1920 x 1200 pixels
Hard Drives
1TB NVMe WD SSD
Mouse
Bluetooth
Browser
Microsoft Edge
Antivirus
McAfee , Malwarebytes
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 My Computer

At a glance

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

My Computer My Computer

At a glance

Windows 11 Home 64-bitIntel Core i7 13700H32GB DDR4Intel Iris Xe Graphics
OS
Windows 11 Home 64-bit
Computer type
Laptop
Manufacturer/Model
MSI Laptop - Commercial 14 H A13MG vPRO-098
CPU
Intel Core i7 13700H
Motherboard
MSl Co., Ltd. MS-14L1 (U3E1)
Memory
32GB DDR4
Graphics Card(s)
Intel Iris Xe Graphics
Sound Card
Intel Smart Sound Technology - Realtek High Definition Audio
Monitor(s) Displays
Generic 1920x1200 @ 60Hz
Screen Resolution
1920 x 1200 pixels
Hard Drives
1TB NVMe WD SSD
Mouse
Bluetooth
Browser
Microsoft Edge
Antivirus
McAfee , Malwarebytes
Back
Top Bottom