Rename files in a folder with random letters, numbers

New to FreeBASIC? Post your questions here.
Post Reply
gerry
Posts: 70
Joined: Oct 04, 2021 7:29

Rename files in a folder with random letters, numbers

Post by gerry »

Hello!

I have files with different names in the folder

Task:
1. Find these files.
2. Rename the file names with random letters and numbers (at the same time, write down the original file name and the one it will be changed to in a separate file).
3. Restore the file names from random to the original ones before the file name change.
Laurens
Posts: 17
Joined: Mar 16, 2022 9:16
Location: Flevoland, the Netherlands

Re: Rename files in a folder with random letters, numbers

Post by Laurens »

An easy way to find files is with the built-in libraries, over here one combining them. This module can remember up to 64K file names simultaneously. The code is a 1:1 copy-paste from the Geany IDE.

sdir.bi:

Code: Select all


'Simple directory browser
'Thanks to the FreeBasic community for their clear documentation and examples.

'The files dir.bi and file.bi are included here, in order to keep everything simple and short. 

#include once "../include/dir.bi"
#include once "../include/file.bi"

'Extension to dir.bi:
'------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

/'
This is the dir.bi extension named DirList. 

		Usage: DirList Filename, Attrib. Retrieving information will become very simple, for all software performing automatic search actions.
'/ 

'Preparing variables, for internal and external use:
Dim Shared FileNames(65535) As String				'Put the first 65536 items in this dimensioned string
Dim Shared As Integer FileNumber, FileActive		'File counter and active file number being processed (the last one is meant for the code outside only)

'The actual retrieving of the information:
Sub DirList (ByRef FileSpec As String, ByVal attrib As Integer)
	Dim As String FileName = Dir(FileSpec, attrib) 	'Start a file search with the specified FileSpec/attrib *AND* get the first filename.
	FileNumber = 0
	Do While Len(FileName) > 0 						'If len(filename) is 0, exit the loop: no more filenames are left to be read.
		If FileNumber <= 65535 Then
			FileNames(FileNumber) = filename		'Start counting at zero
			FileNumber += 1							'Add one position to the totals
			FileName = Dir() 						'Search for (and get) the next item matching the initially specified FileSpec/attrib.
		Else
			FileName = ""
		End If
	Loop
End Sub

Add the next lines in your main program, at the beginning:

Code: Select all

#include once "../include/sdir.bi"

And where the files have to be listed in:

Code: Select all

DirList "*.*", fbNormal

The number of found files is put into the FileNumber variable, and getting the names can be done by using FileNames (your variable). Now it's quite easy to put them into a for-next loop, renaming them and storing them into a file.

Example:

Code: Select all

' Saving your data
Open fileDB.dat for output As #1
Print #1, FileNumber
For ActualFile = 0 To FileNumber
Print #1, FileNames (ActualFile) 
Print #1, FileNamesNewName (ActualFile)
Close #1

' Loading your data
Open fileDB.dat for input As #1
Line Input #1, FileNumber
For ActualFile = 0 To FileNumber
Line Input #1, FileNames (ActualFile) 
Line Input #1, FileNamesNewName (ActualFile)
Close #1

I hope this will be useful to you.
gerry
Posts: 70
Joined: Oct 04, 2021 7:29

Re: Rename files in a folder with random letters, numbers

Post by gerry »

Laurens wrote: Jul 04, 2022 10:59 An easy way to find files is with the built-in libraries, over here one combining them. This module can remember up to 64K file names simultaneously. The code is a 1:1 copy-paste from the Geany IDE.

sdir.bi:

Code: Select all


'Simple directory browser
'Thanks to the FreeBasic community for their clear documentation and examples.

'The files dir.bi and file.bi are included here, in order to keep everything simple and short. 

#include once "../include/dir.bi"
#include once "../include/file.bi"

'Extension to dir.bi:
'------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

/'
This is the dir.bi extension named DirList. 

		Usage: DirList Filename, Attrib. Retrieving information will become very simple, for all software performing automatic search actions.
'/ 

'Preparing variables, for internal and external use:
Dim Shared FileNames(65535) As String				'Put the first 65536 items in this dimensioned string
Dim Shared As Integer FileNumber, FileActive		'File counter and active file number being processed (the last one is meant for the code outside only)

'The actual retrieving of the information:
Sub DirList (ByRef FileSpec As String, ByVal attrib As Integer)
	Dim As String FileName = Dir(FileSpec, attrib) 	'Start a file search with the specified FileSpec/attrib *AND* get the first filename.
	FileNumber = 0
	Do While Len(FileName) > 0 						'If len(filename) is 0, exit the loop: no more filenames are left to be read.
		If FileNumber <= 65535 Then
			FileNames(FileNumber) = filename		'Start counting at zero
			FileNumber += 1							'Add one position to the totals
			FileName = Dir() 						'Search for (and get) the next item matching the initially specified FileSpec/attrib.
		Else
			FileName = ""
		End If
	Loop
End Sub

Add the next lines in your main program, at the beginning:

Code: Select all

#include once "../include/sdir.bi"

And where the files have to be listed in:

Code: Select all

DirList "*.*", fbNormal

The number of found files is put into the FileNumber variable, and getting the names can be done by using FileNames (your variable). Now it's quite easy to put them into a for-next loop, renaming them and storing them into a file.

Example:

Code: Select all

' Saving your data
Open fileDB.dat for output As #1
Print #1, FileNumber
For ActualFile = 0 To FileNumber
Print #1, FileNames (ActualFile) 
Print #1, FileNamesNewName (ActualFile)
Close #1

' Loading your data
Open fileDB.dat for input As #1
Line Input #1, FileNumber
For ActualFile = 0 To FileNumber
Line Input #1, FileNames (ActualFile) 
Line Input #1, FileNamesNewName (ActualFile)
Close #1

I hope this will be useful to you.
I don't quite understand how it should work?
grindstone
Posts: 862
Joined: May 05, 2015 5:35
Location: Germany

Re: Rename files in a folder with random letters, numbers

Post by grindstone »

gerry wrote: Jul 04, 2022 5:53 ...I have files with different names in the folder

Task:
1. Find these files....
Do you mean all the files in the folder or only some certain ones?
gerry
Posts: 70
Joined: Oct 04, 2021 7:29

Re: Rename files in a folder with random letters, numbers

Post by gerry »

grindstone wrote: Jul 04, 2022 12:18
gerry wrote: Jul 04, 2022 5:53 ...I have files with different names in the folder

Task:
1. Find these files....
Do you mean all the files in the folder or only some certain ones?
Yes, all files in the current folder
grindstone
Posts: 862
Joined: May 05, 2015 5:35
Location: Germany

Re: Rename files in a folder with random letters, numbers

Post by grindstone »

This is how you can get a list of all the files (and only the files) of a directory. Don't use the attrib mask to filter the files, it doesn't work correctly. The rest shouldn't be rocket science to you.

Code: Select all

#Include "vbcompat.bi"

ReDim As String filelist(1)
Dim As String temp, searchdir = "c:\*.*" 'name of the directory to scan
Dim As Integer attrib

temp = Dir(searchdir, -1, @attrib) 'get 1st entry
Do While Len(temp)
	If (attrib And fbDirectory) <> fbDirectory Then 'dir entry is not a directory
		filelist(UBound(filelist)) = temp 'write file name to array
		ReDim Preserve filelist(UBound(filelist) + 1) 'create new array member
	EndIf
	temp = Dir("", -1, @attrib) 'get next entry
Loop

'print the file list
For x As Integer = 1 To UBound(filelist) - 1
	Print filelist(x)
Next
Sleep
gerry
Posts: 70
Joined: Oct 04, 2021 7:29

Re: Rename files in a folder with random letters, numbers

Post by gerry »

grindstone wrote: Jul 05, 2022 10:54 This is how you can get a list of all the files (and only the files) of a directory. Don't use the attrib mask to filter the files, it doesn't work correctly. The rest shouldn't be rocket science to you.

Code: Select all

#Include "vbcompat.bi"

ReDim As String filelist(1)
Dim As String temp, searchdir = "c:\*.*" 'name of the directory to scan
Dim As Integer attrib

temp = Dir(searchdir, -1, @attrib) 'get 1st entry
Do While Len(temp)
	If (attrib And fbDirectory) <> fbDirectory Then 'dir entry is not a directory
		filelist(UBound(filelist)) = temp 'write file name to array
		ReDim Preserve filelist(UBound(filelist) + 1) 'create new array member
	EndIf
	temp = Dir("", -1, @attrib) 'get next entry
Loop

'print the file list
For x As Integer = 1 To UBound(filelist) - 1
	Print filelist(x)
Next
Sleep
2. Rename the file names with random letters and numbers (at the same time, write down the original file name and the one it will be changed to in a separate file).
3. Restore the file names from random to the original ones before the file name change.
gerry
Posts: 70
Joined: Oct 04, 2021 7:29

Re: Rename files in a folder with random letters, numbers

Post by gerry »

Help!!!
gerry
Posts: 70
Joined: Oct 04, 2021 7:29

Re: Rename files in a folder with random letters, numbers

Post by gerry »

grindstone
Posts: 862
Joined: May 05, 2015 5:35
Location: Germany

Re: Rename files in a folder with random letters, numbers

Post by grindstone »

gerry wrote: Jul 09, 2022 13:312. Rename the file names with random letters and numbers (at the same time, write down the original file name and the one it will be changed to in a separate file).
3. Restore the file names from random to the original ones before the file name change.
To be honest, I can't imagine what sense it would make to rename a file and then restore the old name without copying or moving the file.

But alright, here an other snippet to create a valid random file name:

Code: Select all

Dim As Integer minlen = 5, maxlen = 20, namelen, x
Dim As Integer minchar = Asc("#"), maxchar = Asc("~")
Dim As String newfilename, g, forbiddencharacters = "\/?:*<>|" + Chr(34)

Randomize

Do
	namelen = Int(Rnd * (maxlen - minlen) + minlen) 'random length of the new filename
	newfilename = ""
	For x = 1 To namelen
		Do 'get a random character
			g = Chr(Int(Rnd * (maxchar - minchar) + minchar))
		Loop While InStr(forbiddencharacters, g) 'character is not allowed in file names -> try again
		newfilename += g 'add character to filename
	Next
	? newfilename
	Sleep
Loop 
Post Reply