0

I would like to rename the name and the extension of all files which are located in the a certain directories and its subdirectories to a random combination of 25 alphamuneric signs (letters and figures) which is equal to the filename. The fileextension should be ".TEST"

I have found the following script which only renames in a parent directory and the it only changes the name to a random integer.

@ echo off
setlocal EnableDelayedExpansion
for %%F in ("%userprofile%\music\*.*") do (
    ren "%%~F" "!RANDOM!.test"
)
endlocal

So how to do it with all child directories and the combination of letters and numbers?

3
  • Apologies, edited your question rather than my answer. all should be back as it was now. Commented Feb 12, 2022 at 23:23
  • which is equal to the filename, does that mean that when you have a file called "my file.ext it should have the same number of random characteres? 7al4asc.test Commented Feb 13, 2022 at 2:25
  • I want that the file "hello.txt" becomes renamed into an combination of random small and capital letters and numbers. The amount of the digits is equal to 100. Commented Feb 14, 2022 at 4:34

1 Answer 1

0

put /R after the word for so it recurses into subdirectories

@ echo off
setlocal EnableDelayedExpansion
for /R "%userprofile%\music\" %%F in (*.*) do (
    ren "%%~fF" "%%~fF%RANDOM%.test"
)
endlocal

see more details on the recursive for loop here: https://ss64.com/nt/for_r.html

for Random number generation, enclose the RANDOM in % signs like %RANDOM%. https://riptutorial.com/batch-file/example/32511/random-numbers

if you want to put a dot between the original file name and the new extension, try ren "%%~fF" "%%~F.%RANDOM%.test". on my system it formats names like Textures\Interface\VUI+\solid_black.dds.230.test

for powershell I'd do something like (this is a dryrun; remove -WhatIf from the line when you are satisfied it works and want to actually execute the rename)

Get-ChildItem $env:USERPROFILE\music\ -Recurse -File | Rename-Item -WhatIf -NewName { $_.Name + "." + (Get-Random -Maximum 1000)+ ".test"}

9
  • Thank you for your answer Frank Thomas, but your adjusted script does not work on my PC. Could there be a mistake? Commented Feb 12, 2022 at 22:54
  • generally in is used to filter files in the current working directory, rather than to specify the full path. try it again now. Commented Feb 12, 2022 at 23:23
  • So you mean: ........... for /R %%F in ..... and over setlocal EnableDelayedExpansion I write: cd %userprofile%\music Commented Feb 12, 2022 at 23:26
  • I tried it again but now it only takes one file of the parent folder and calls it !RANDOM!.test, but I want the filename be a random number Commented Feb 12, 2022 at 23:29
  • Still does not work and I do not know why. Could you tell me how to do it with a powershell script without admin rights? Commented Feb 12, 2022 at 23:48

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .