Today, I had the challenge to rename a set of files that follow a pattern. These files are accessible through Google Drive on the web, obviously, and I also have them accessible through Google Drive File Stream on Windows.
The most basic option is to do this by hand in the web UI. Even with efficient copy and pasting and some quick keyboard movements, this is tedious and slow. Not a good option for more than 5 - 10 files at most.
Same goes for using Windows Explorer. Sure, this is faster because the Drive File Stream will do the syncing in the background, but still a copy&paste fest.
Windows has a command line of sorts (the venerable CMD window, sometimes known as the console). Although there are a few looping constructs, doing stuff to a predictable stream of input (the bread and butter of a UNIX shell) is almost as painful as typing by hand.
I tried the recently released Windows Subsystem for Linux and the bash you can run on Windows. This is an incredible step forward for running UNIX tools on Windows without having to resort to Virtual Machines or ConEmu. (NB: I had this years and years ago with Cygwin and haven't tried it in this case, but that is not the point of this tale).
The problem with Bash on Windows on the Windows Subsystem for Linux is that Google Drive File Stream is not yet 100% perfect in pretending to be a locally available drive: it mounts a virtual drive on (in my case) G:\, which is accessible to Windows Explorer and even the CMD window (go to the G: drive, cd into My Drive, do things like 'dir' and 'dir /s' -- it all works). The bash shell, however, functions by making actual unrecompiled GNU Bash talk to Windows system primitives. Somehow, Google Drive File Stream is not plugged in deep enough, because in bash, although I can do this:
$ cd /mnt/g/
$ cd My\ Drive
I can not do even a simple 'ls':
$ ls
ls: reading directory '.': Function not implemented
So... my other options were to grab a MacBook with at least High Sierra (to be able to run Google Drive File Stream) and see if its terminal is more cooperative (I expect it is) or give Cygwin a go. The latter option might very well work, but I would have to load Cygwin onto an already groaning laptop. Also, I wanted a solution that is portable, so I made it work in Google Cloud Shell.
The following bit of code does this:
- List the files in a folder specified by ID
- Parse the file names and change them (the 'sed' command spits out the new file name)
- Pipe the output to gam csv. This step both aids in performance, because name changes are done in parallel, but it also saves me from having to deal with quotes and spaces.
I provide this here for those among you who are either comfortable with UNIX shell or are intermediate learners, for inspiration and to not have to invent the same wheel yourself. That said, these are power tools and can do unpredictable things in inexperienced hands, so make you know what you are doing!
$ gam user john...@yourdomain.com show filelist select id 9adaksdjnv9w7dfs9-wn9cm3c3 depth 0 fields id,title delimiter '|' | tail +2 | ( IFS=","; echo "id,newname"; while read USER ID NAME; do echo "${ID},$(echo $NAME | sed 's/^Beren-\(.*\)/Het Rijk van de Grizzly \1/')"; done) | gam csv - gam user john...@yourdomain.com move drivefile id ~id newfilename ~newname
If I find I am doing this over and over again, I might script it, but given that the sed command is highly tied to whatever the situation might be and is very sensitive to spaces and quoting, editing it in the command line history is probable the most efficient way to use this.
--peter