Saturday, August 3, 2019

windows - How to batch copy & rename files?



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


Answer



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).


No comments:

Post a Comment

hard drive - Leaving bad sectors in unformatted partition?

Laptop was acting really weird, and copy and seek times were really slow, so I decided to scan the hard drive surface. I have a couple hundr...