I'm trying to hash out a batch / .bat script that renames a single file to a random string of characters. Everything I have found upon searching so far is to rename all files within a single directory - which is just a bit more than I'm looking for.
The following example script is the basis of what I'm working off of. It basically renames all .txt files in the working directory to some random 8 character length file name, preserving the file extension:
@echo off setlocal disableDelayedExpansion set "chars=ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789" for /f "eol=: delims=" %%F in ('dir /b /a-d *.txt') do call :renameFile "%%F" exit /b :renameFile setlocal enableDelayedExpansion :retry set "name=" for /l %%N in (1 1 8) do ( set /a I=!random!%%36 for %%I in (!I!) do set "name=!name!!chars:~%%I,1!" ) echo if exist !name!.jpg goto :retry endlocal & ren %1 %name%.txt
The reason why I'm looking to only rename a single file is because my ultimate goal is to create a shell extension that uses the script - I'm wanting to be able to right-click a single file, click "Rename Random", and it calls the batch file to rename the selected file to some random name.
If anyone has any input or modifications to the above mentioned code that would help me out, that would be great! Thanks