2

How to rewrite the first 3 letters of file name

  • Using set alphabet: ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789 to randomise characters
  • Targeting all files of a particular extension in a folder *.ext
  • Duplication of 3 characters by chance does not matter (46,656 variables already)
  • Batch script solution to run in a windows environment.
  • Prefer script to be simple to reduce run time.

Suggested code and breakdown. Note: I still have no idea how to code.

  • Original:

    032_name.ext
    039_name.ext
    0D8_name.ext
    333_other.txt
    
  • Write Over the top of first three characters

    XXX-name.ext
    
  • After

    D7K_name.ext
    L2V_name.ext
    720_name.ext
    333_other.txt
    

Make batch file (SetRename.bat or SetRename.cmd) then run command like:

SetRename %r *.ext   /or/ SetRename %r3%name% *.ext  /or/ SetRename
  • Code 1 adapted from DavidPostill, example of full solution

    @echo off
    setlocal enabledelayedexpansion
    rem initialise counter
    set "alphabet=A B C D E F G H I J K L M N O P Q R S T U V W X Y Z 0 1 2 3 4 5 6 7 8 9"
    rem process jpg and png files
    for /f "usebackq tokens=*" %%i in (`dir /b *.ext *.ex2`) do (
      rem split into name and extension
      set _name=[reduce 3 characters]]%%~ni
      set _ext=%%~xi
      rem do the rename
      ren "%%i" "!y!-!_name!-!_ext!"
      increment counter
      set /alphabet "%Random%"
      )
    endlocal
    
  • Code 2 adaptated from 3 SU posts

    @echo off
    
    setlocal enableextensions enabledelayedexpansion
    
    set "alphabet=A B C D E F G H I J K L M N O P Q R S T U V W X Y Z 0 1 2 3 4 5 6 7 8 9"
    
    for %%a in (%alphabet%) do (
        set "a.!name!=%%a"
    set o=-%3 [reduce by 3] 
    
    )   
    set "y="
    for /l %%a in (1 1 3) do (
        set /a "r=!random!"
        for %%b do set "y=!-%3!!r!!name!"
    )
    echo(%y%
    
    endlocal
    

Current Research:

2
  • You made a right mess of my code :)
    – DavidPostill
    Mar 17, 2019 at 10:55
  • I certainly did, what a mess! :)
    – user900431
    Mar 17, 2019 at 11:23

1 Answer 1

1

How can I rewrite the first 3 letters of a set of file names with random characters?

Use the following batch file (SetRename.cmd):

@echo off
setlocal enableDelayedExpansion
set "_chars=ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
for %%f in (%*) do (
  rem split into name and extension
  set "_name=%%~nf"
  rem remove first 3 characters from _name
  set "_name=!_name:~3!"
  set "_ext=%%~xf"
  rem create a 3 character random prefix
  set "_prefix="
  for /l %%n in (1 1 3) do (
    set /a i=!random!%%36
    for %%i in (!i!) do (
      set "_prefix=!_prefix!!_chars:~%%i,1!" (
      )
    )
  echo ren "%%f" "!_prefix!!_name!!_ext!"
  )
endlocal

Notes:

  • remove echo when you are happy with the ren command

Example usage:

> dir /b
032_name.ext
039_name.ext
0D8_name.ext
333_other.txt
SetRename.cmd

> SetRename *.ext

> dir /b
333_other.txt
C42_name.ext
FZW_name.ext
MYQ_name.ext
SetRename.cmd

> SetRename *.ext

> dir /b
333_other.txt
A6O_name.ext
A8H_name.ext
H5P_name.ext
SetRename.cmd

Further Reading

0

You must log in to answer this question.