Hi,
I am trying to rename files located in different directories using Stata 12.1. I've looked at previous posts about renaming files and tried to apply those here, and my code seems to run. However, the names of my files do not change in the directory. The blue screen pops up so quickly after the !rename command that I can't tell what the error is.
I would like my code to (1) determine all the folders in a particular directory (2) look through each folder and compile all the files located within that folder (3) rename the files using the name of the folder within the new name. Examples of file names, folder names, and directories include: IMG_5996.jpg (file name), "All Lab Tests" (folder name), and TZA_01 Mark Laboratory (directory). There are instances where a folder will also have subfolders, but I haven't yet coded that part given the issues I'm having here.
Thanks in advance,
Simone
I am trying to rename files located in different directories using Stata 12.1. I've looked at previous posts about renaming files and tried to apply those here, and my code seems to run. However, the names of my files do not change in the directory. The blue screen pops up so quickly after the !rename command that I can't tell what the error is.
I would like my code to (1) determine all the folders in a particular directory (2) look through each folder and compile all the files located within that folder (3) rename the files using the name of the folder within the new name. Examples of file names, folder names, and directories include: IMG_5996.jpg (file name), "All Lab Tests" (folder name), and TZA_01 Mark Laboratory (directory). There are instances where a folder will also have subfolders, but I haven't yet coded that part given the issues I'm having here.
Code:
global main "G:/Tanzania Data/" global FAC f1 f2 f3 f4 f5 f6 f7 f8 f9 f10 local tza 01 03 05 06 07 08 09 10 11 12 15 local num: word count `tza' forvalues i = 1/`num' { local j: word `i' of `tza' local k: word `i' of ${FAC} local mydirs: dir "${main}TZA`j'_${`k'}/Photos" dirs "*" foreach dir of local mydirs { local path "${main}TZA`j'_${`k'}/Photos/`dir'" local myfiles: dir "`path'" files "*.jpg" local n = 1 foreach file of local myfiles { local name = trim("`dir'") local subfile = "TZA`j'_`name'_`n'.jpg" !rename "`path'/`file'" "`subfile'" local n = `n' + 1 } } }
Simone
Comment