2

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

2

How can I change my batch file to rename a single file to a random string of characters?

You can pass the name of the file you want to change as an argument %1 to the batch file.

Here is the modified batch file:

@echo off
setlocal enableDelayedExpansion
set "chars=ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
: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!"
)
if exist !name!%~x1 goto :retry
endlocal & ren %1 %name%%~x1

Notes:

  • Only the filename part of %1 is randomised.
  • The existing extension is preserved.

Example:

F:\test>dir *.dat
 Volume in drive F is Expansion
 Volume Serial Number is 3656-BB63

 Directory of F:\test

15/12/2015  12:59                 0 random.dat
               1 File(s)              0 bytes
               0 Dir(s)  1,776,913,768,448 bytes free

F:\test>test random.dat

F:\test>dir *.dat
 Volume in drive F is Expansion
 Volume Serial Number is 3656-BB63

 Directory of F:\test

15/12/2015  12:59                 0 IYSPA2FS.dat
               1 File(s)              0 bytes
               0 Dir(s)  1,776,913,768,448 bytes free

Further Reading

1
  • This was exactly what I needed for a good starting point. Also the A-Z Index link you provided looks extremely helpful. Thank you!
    – amy
    Dec 15 '15 at 18:08
-1

It's very simple. Here is the batch file:

@echo off
set name=%1
set newname=%random%%random%
ren %name% %newname%.txt

After that you need to go to the registry to the section with the name of the file extension you want to rename. In my case I go to HKEY_CLASSES_ROOT\.txt and check what is the value for Default key. In my case it is Notepad++_file. Then I go to HKEY_CLASSES_ROOT\Notepad++_file, create a new subkey, e.g. "Rename to Random" and subkey "command" in it (without quotes). In HKEY_CLASSES_ROOT\Notepad++_file\shell\Rename to Random\command I edit Default value to "C:\Temp\random_rename.bat" "%1" (with quotes!)

After that when I right-click on every txt file and choose Rename to Random option, it is renamed to 8-digit name.

P.S. Edit the path to the real location of your batch file when you add it to the registry. Also, you can edit a variable %newname% to whatever you want. In my case it's name is combined of two 4-digit groups, each of them is generated by system variable %random%.

Check this for step-by-step actions in the registry.

3
  • This doesn't work. %RANDOM% generates a random integer from 0 to 32,767 so %random%%random% is not guaranteed to return exactly 8 digits. In addition the OPs script produces 8 characters not 8 digits.
    – DavidPostill
    Dec 15 '15 at 12:47
  • @DavidPostill That wasn't stated in the requirements that it should obligatory generate only 8 characters.
    – Hardoman
    Dec 15 '15 at 12:52
  • No, but he did say he wanted to use a modifed version of his existing script.
    – DavidPostill
    Dec 17 '15 at 20:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.