How to batch copy & rename files?

15,660

All it does increment by numbers but I want to increment by the alphabet

The following batch file (test.cmd) should get you started:

@echo off
setlocal enableDelayedExpansion
set "chars=abcedefhijklmnopqrstuvwxyz"
for /l %%i in (0,1,25) do (
  echo copy source-file.html folder\poll!chars:~%%i,1!.html
  )
endlocal

Notes:

  • This is a partial answer because your requirements are not clear.
  • Use the above batch file as a starting point
  • It shows how to construct the file names using incremental letters of the alphabet.

Example output:

copy source-file.html folder\polla.html
copy source-file.html folder\pollb.html
copy source-file.html folder\pollc.html
copy source-file.html folder\polle.html
copy source-file.html folder\polld.html
copy source-file.html folder\polle.html
copy source-file.html folder\pollf.html
copy source-file.html folder\pollh.html
copy source-file.html folder\polli.html
copy source-file.html folder\pollj.html
copy source-file.html folder\pollk.html
copy source-file.html folder\polll.html
copy source-file.html folder\pollm.html
copy source-file.html folder\polln.html
copy source-file.html folder\pollo.html
copy source-file.html folder\pollp.html
copy source-file.html folder\pollq.html
copy source-file.html folder\pollr.html
copy source-file.html folder\polls.html
copy source-file.html folder\pollt.html
copy source-file.html folder\pollu.html
copy source-file.html folder\pollv.html
copy source-file.html folder\pollw.html
copy source-file.html folder\pollx.html
copy source-file.html folder\polly.html
copy source-file.html folder\pollz.html

Further Reading

  • An A-Z Index of the Windows CMD command line - An excellent reference for all things Windows cmd line related.
  • enabledelayedexpansion - Delayed Expansion will cause variables to be expanded at execution time rather than at parse time.
  • for /l - Conditionally perform a command for a range of numbers.
  • set - Display, set, or remove CMD environment variables. Changes made with SET will remain only for the duration of the current CMD session.
  • variables - Extract part of a variable (substring).
Share:
15,660

Related videos on Youtube

EdibleMuffin
Author by

EdibleMuffin

Updated on September 18, 2022

Comments

  • EdibleMuffin
    EdibleMuffin over 1 year

    I'm using windows server 2012 R2. I have a folder with a bunch of files and I want to copy for every file in that folder 20 times into another folder but the newly copied file has to be renamed using single alphabetical orders. For example a file called "orange.html" gets copied 20 times and moved to another folder. The new folder would contain 20 new copied files with file names such as a.html, b.html, c.html etc.

    This is the code but all it does increment by numbers but I want to increment by the alphabet

    @echo off
    
    for /L %%i IN (1,1,100) do call :docopy %%i
    goto end
    
    :docopy
    set FN=00%1
    set FN=%FN:~-3%
    
    copy source-file.html poll%FN%.html
    
    :end
    
    • DavidPostill
      DavidPostill over 8 years
      Welcome to Super User. We are not a script writing service. We expect users to tell us what they have tried so far (including any scripts they are using) and where they're stuck so that we can help with specific problems. Questions that only ask for scripts are too broad and are likely to be put on hold or closed. Please read How do I ask a good question?.
    • EdibleMuffin
      EdibleMuffin over 8 years
      I've edited my question. Is this helpful now?
    • DavidPostill
      DavidPostill over 8 years
      That is much better ;)
    • dbenham
      dbenham over 8 years
      You cannot copy both file1.html, file2.html, etc 20 times to the same names a.html, b.html, c.html ... in the same folder. You need to clarify your requirements.
  • dbenham
    dbenham over 8 years
    Why mess with FOR /L and substring? You could simply use for %%C in (a b c ... etc ... x y z) do ..., and then you don't need any delayed expansion.
  • DavidPostill
    DavidPostill over 8 years
    @dbenham True, but I was using the OPs batch file as a starting point. There is always more than one way do something in batch, and this way the OP also learns about substrings ;)