Rename files and folders to random names

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
david.lynch
Posts: 13
Joined: 08 Jul 2012 21:00

Rename files and folders to random names

#1 Post by david.lynch » 16 Jul 2012 16:25

Hello everyone! Image

I'm looking for a way to rename all files and folders from a given folder (%1), including subfolders. The purpose is to help obfuscate what they are. Names are really unwanted in this scenario.

It should work even on +r, +h , +s files and folders.

These names should look like nothing - or for instance like a damaged file system, with a wide range of characters.

Does exist a script like this? is this possible?

Image

TB99
Posts: 1
Joined: 16 Jul 2012 15:36

Re: Rename files and folders to random names

#2 Post by TB99 » 16 Jul 2012 18:29

Code: Select all

C:\test>type  randomname.bat
@echo off
cd c:\test\
dir /b ab*.txt > testdata.txt
type testdata.txt

for /f %%i in ( testdata.txt) do (

echo copy %%i %%i%random%.txt
echo ren %%i %%i%random%.txt

)
C:\test> randomname.bat
abc.txt
abc2.txt
copy abc.txt abc.txt15611.txt
ren abc.txt abc.txt23205.txt
copy abc2.txt abc2.txt15611.txt
ren abc2.txt abc2.txt23205.txt

C:\test>

Liviu
Expert
Posts: 470
Joined: 13 Jan 2012 21:24

Re: Rename files and folders to random names

#3 Post by Liviu » 16 Jul 2012 21:08

@david, you may want to elaborate on that "scenario". The requirements sound quite odd, and you might get better help if you described the context a bit more.

@bill, you don't need a temp file, the code skips s/h files, ignores directories, doesn't recurse into subdirectories, %random% is evaluated before the "for" loop runs so it's not that random at all, and there is no safeguard against clashing (re)names. Other than that, it's a starting point, and you've got the board 'code' tags right at least ;-)

Liviu

Ocalabob
Posts: 79
Joined: 24 Dec 2010 12:16
Location: Micanopy Florida

Re: Rename files and folders to random names

#4 Post by Ocalabob » 16 Jul 2012 21:14

Greetings David.Lynch,

The purpose is to help obfuscate what they are. Names are really unwanted in this scenario.


Do you mean the file name only or the file name and extension?

If you don't mind another question, I'm curious as to why would you want to do this as this may well be irreversible.

Best wishes, and I look forward to your reply!

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Rename files and folders to random names

#5 Post by foxidrive » 17 Jul 2012 02:35

david.lynch wrote:Hello everyone! Image

I'm looking for a way to rename all files and folders from a given folder (%1), including subfolders. The purpose is to help obfuscate what they are. Names are really unwanted in this scenario.

It should work even on +r, +h , +s files and folders.

These names should look like nothing - or for instance like a damaged file system, with a wide range of characters.

Does exist a script like this? is this possible?


It's possible to build random filenames from a character set - but it would be terribly destructive and could be the basis for malware.

Another way to destroy a file system would be to exchange the filenames randomly amongst all the files.

Squashman
Expert
Posts: 4465
Joined: 23 Dec 2011 13:59

Re: Rename files and folders to random names

#6 Post by Squashman » 17 Jul 2012 06:08

foxidrive wrote:
david.lynch wrote:Hello everyone! Image

I'm looking for a way to rename all files and folders from a given folder (%1), including subfolders. The purpose is to help obfuscate what they are. Names are really unwanted in this scenario.

It should work even on +r, +h , +s files and folders.

These names should look like nothing - or for instance like a damaged file system, with a wide range of characters.

Does exist a script like this? is this possible?


It's possible to build random filenames from a character set - but it would be terribly destructive and could be the basis for malware.

Another way to destroy a file system would be to exchange the filenames randomly amongst all the files.

On most forums I belong to, they would consider this destructive and lock the thread.

Squashman
Expert
Posts: 4465
Joined: 23 Dec 2011 13:59

Re: Rename files and folders to random names

#7 Post by Squashman » 17 Jul 2012 06:17

Liviu wrote:@bill, you don't need a temp file, the code skips s/h files, ignores directories, doesn't recurse into subdirectories, %random% is evaluated before the "for" loop runs so it's not that random at all, and there is no safeguard against clashing (re)names. Other than that, it's a starting point, and you've got the board 'code' tags right at least ;-)

Liviu

And it doesn't remove the original file extension and you would think that you would not want to use any portion of the original file name if the OP wants to obfuscate what the original file name is or what the the file type was. But obfuscating the file extension is like doing security through obscurity. There are utilities that you can use to tell you what type of file a file is.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Rename files and folders to random names

#8 Post by foxidrive » 17 Jul 2012 06:26

Squashman wrote:
foxidrive wrote:It's possible to build random filenames from a character set - but it would be terribly destructive and could be the basis for malware.

Another way to destroy a file system would be to exchange the filenames randomly amongst all the files.

On most forums I belong to, they would consider this destructive and lock the thread.


I'm not surprised. I wouldn't write something like that which could be used by a disgruntled employee on his last day at the office.

david.lynch
Posts: 13
Joined: 08 Jul 2012 21:00

Re: Rename files and folders to random names

#9 Post by david.lynch » 17 Jul 2012 10:22

Sorry, it seems that I'm a little late for replies here :)

The long history:

The purpose is really not bad, as file contents remains intact. This would be a more elaborated version of what we see on this How-To Geek article:

http://www.howtogeek.com/?post_type=post&p=57661

@Ocalabob: filenames and extensions.

This will be used for a initial preparation of a custom database, the choice of a internal developer. The names *need* to look completely strange, and then the developer will identify them by content on his application and they will not be renamed anymore.

Since there are a lot of files and subfolders, indexes and tables, a batch would do the job nicely.

I know that this is a strange question, but that's how things are sometimes Image

Image

Liviu
Expert
Posts: 470
Joined: 13 Jan 2012 21:24

Re: Rename files and folders to random names

#10 Post by Liviu » 17 Jul 2012 12:52

@bill, I'do it with a plain for loop inside a for/d loop called recursively. There are switches to deal with r/s/h files. I'd use !random! instead of %random%, or better still uuidgen.exe from the sdk, plus check for existence of the target name in advance and avoid overwriting.

@david, since your internal developer must already have code to recurse into subdirectories and iterate over each set of files, it would only take a tiny extra function for him to handle the rename part.

Regarding the "stupid geek tricks" link, it's always a good idea to take such random sightings with a grain of caution. Their claim that the operation is "undoable" is, to put it mildly, exaggerated. Since the "undo" file is plain text, any original filenames using characters outside the current codepage are lost. Also, the way it's written, even some plain ASCII characters like ^%! can trip the code.

david.lynch
Posts: 13
Joined: 08 Jul 2012 21:00

Re: Rename files and folders to random names

#11 Post by david.lynch » 17 Jul 2012 14:31

Liviu wrote:@david, since your internal developer must already have code to recurse into subdirectories and iterate over each set of files, it would only take a tiny extra function for him to handle the rename part.


You're right. When he stumbled into this I've told him that some tool could already exist and do it faster and surprise: we found nothing. So I've asked here.

But since then, would be faster to him write it himself Image

Thanks anyway! Image

Post Reply