

Copy the code into Notepad and save it as get_dirs.bat:įor /d %%a in (*) do dir /ad /on /s /b "%%a" > get_dirs.txt List all folders and sub-folders in a directory and subdirectoryĪ slight variation on the first script, this second one lists all folders and sub-folders in a directory and subsequent subdirectories. The final code > get_files.txt directs and appends all the output to a text file called get_files.txt which is necessary because the script is repeated several times as it recurses each folder and so we want to collate all the names into a single file. The do echo %%a script executes the echo %%a command that simply displays the current file being read in the current folder name which is represented by the variable. The in (*) bit is the set of files to match, which in this case is everything denoted by using the * wildcard character. The variable name %%a is used to hold the current folder being traversed. The addition of the /r switch is to tell the script to recurse all directories and subdirectories from where the script is run. The for command here is used to run a further command for each file or folder in the set.


Copy the code into Notepad or other text editor and save it as get_files.bat:įor /r %%a in (*) do echo %%a > get_files.txt Simple and straightforward this one, a script to list all files in a directory and all corresponding subdirectories. List all files in a directory and subdirectory Notice that first group with the ‘hat’ symbol to negate the node_modules folder and it’s content? What a lovely, simple and powerful ZSH function.DOS has always been handy for creating the type of batch files that XP could never handle well so here are a few scripts you might find useful. So if you wanted to do our prior command but dodge files in node_modules it could be amended thus for a test run, then again without the -n when you are happy: zmv -n '(^node_modules)/(**/)(*).css' '$1_$2.scss'
#Batch file rename files in subfolders how to
If you want a few more examples, Filipe has some more examples, including how to ignore folders and more. Zmv is obviously capable of much more but if it’s something you only need to do once every so often this will give you enough to go on!. Then, once you are happy everything is as you need, run it again without the flag. So we would first run that prior command like this: zmv -n '(**/)(*).css' '$1_$2.scss'Īnd that will give you a display of what is going to happen to each file. You can run zmv commands first with the -n flag. Now, if you went with that prior command, typed it in and pressed enter, zmv will do its thing, and you’d better keep everything crossed you got everything right! Thankfully, zmv lets you perform a ‘dry run’. In my case this was because I wanted them as Sass partials, which are designated with the underscore. Each thing we ‘captured’ is represented by a $ in the second set of quotes, so you can see in this case that not only did I change the file extension, I added a proceeding _ before it. Then we can manipulate what we have ‘captured’ in the second set of quotes. Each is within a set of parenthesis, the first is the file path, (**/) and the second the file name, (*). Within the first one, zmv is letting us do two sets of replacements/groups. Zmv lets us swap one thing with another, so everything in the first set of quotes with everything in the second set of quotes. But don’t go doing that yet!! zmv '(**/)(*).css' '$1_$2.scss' The basics of how zmv works First, get into a parent folder of all the files you want to rename. Now, I’ll show you the command that did my bidding, and then hopefully explain what’s going on. First you will need to load the zmv function by running autoload zmv from the command prompt. So assuming you have ZSH, let’s take a look at what we can do. It’s the default shell in macOS these days so if you are on Catalina onwards – you already have it. So, as zmv requires ZSH shell, at the risk of stating the obvious, you’ll need a ZSH shell. So, to reiterate my my use case, I had a project where I wanted to change all files ending in. Turns out this is a great little tool I’d not used before, and as it met my needs beautifully, I thought I’d give a brief overview for my future self and any passing travellers. It was suggested I look at zmv which works in the ZSH shell. So I happened to ask in the Sublime Text Discord how people did this. I didn’t fancy renaming them ‘by hand’, and the Finder batch rename is only really useful when all the files are in one folder and/or you can easily select them all. There were around a hundred files nested in many different sub-folders. I had a situation today where I wanted to migrate a large-ish codebase from standard CSS files to Sass files with a *.scss file extension. This is a quick tip rather than an involved tutorial.
