Need some critique on this bat file


  1. Posts : 322
    Windows 10 Home
       #1

    Need some critique on this bat file


    It works in a folder with a image, will ask duplicate x many times. done.


    @echo off

    set /p num_of_copies=Enter number of copies to create:

    setlocal enableDelayedExpansion
    set i=1
    for %%f in (*.*) do (
    set "name=%%~nf"
    set "ext=%%~xf"
    if not "!name!"=="%~n0" (
    for /l %%n in (1 1 %num_of_copies%) do (
    copy "%%f" "!name!_copy!i!%%~xf"
    set /a i+=1
    )
    )
    )

    echo %num_of_copies% copies created.
      My Computer


  2. Posts : 17,099
    Windows 10 Home x64 Version 22H2 Build 19045.5371
       #2

    Looks alright to me. You seem to have provided for cases of paths-filenames containing spaces or containing special characters such as ampersands.

    I never use delayed expansion. I do all my For-related processing in subroutines so I don't have to. I find subroutines easier to debug.
    Other people have different opinions.
    So a generic For command in one of my batch files would look like
    Code:
    For %%A in (*.*) do Call :Processing "%%A"
    GoTo EndProcessing
    
    :Processing
    Set "ThisFile=%~1"
    echo "%ThisFile%"
    :: Do whatever I need to do with the variable
    GoTo :EOF
    
    :EndProcessing
    :: Pause during testing to see what happens when it runs

    I never use echo off either. I run all my batch files minimised and will only want to look in their windows if I think something is going wrong - in which case I want to see everything.


    All the best,
    Denis
      My Computer


 

  Related Discussions
Our Sites
Site Links
About Us
Windows 10 Forums is an independent web site and has not been authorized, sponsored, or otherwise approved by Microsoft Corporation. "Windows 10" and related materials are trademarks of Microsoft Corp.

© Designer Media Ltd
All times are GMT -5. The time now is 00:56.
Find Us




Windows 10 Forums