I have a directory containing over 30,000 files with the following naming convention:
apple_CQ2rtQFD15H_1.txt
apple_CQVku8Qjdzx_1.txt
apple_CQ2rtQFD15H_3.txt
bananna___Bo3-mKXnozt___.txt
bananna___CPf6gN5L3SP___.txt
bananna___CTu8APZMomD___.txt
bananna_rotten___Byj7BPXNnpE___.txt
I want to create a script that will create a new directory based on the first part of the filename before the first uppercase character and move all corresponding files into that directory.
For example all files starting with apple_ will be inserted into a newly created directory called apple_.
Has anyone got a solution to this? I was thinking of modifying this Tcl script:
cd "C:/Development/test"
# glob is a tcl command to list all functions that match the requirements
set files [glob TTT*_*]
foreach f $files {
# use the underscore as a separator to split f and store the parts in dir and fnew
lassign [split $f "_"] dir fnew
if {![file exist $dir]} {
file mkdir $dir
}
file rename $f [file join $dir $fnew]
}
And using the regular expression [A-Z]\S+ in place of the [split $f "_"] but I don't really know how to implement it.