DOS batch for renaming files (sort of complicated)

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
kerket
Posts: 5
Joined: 11 May 2015 02:27

DOS batch for renaming files (sort of complicated)

#1 Post by kerket » 18 May 2015 03:50

I have a folder filled with files taken from a GPS logger for bike rides. My logger outputs to files with extension .nav. The software also can convert it to KMZ files. And then I also use GPSBabel to convert the .nav to GPX so it can be read by various cycling sites.

My usual file naming method is to include the date and then a brief description of the main hills I climbed on the ride. So, for exmaple, I have a set of files from Sunday that are:
5-17 penn - milks - progy.nav
5-17 penn - milks - progy.kmz
5-17 penn - milks - progy.gpx


However, I notice last yea that the KMZ file that was generated by the software had a lot of strange data, as well as cut off the beginning and ends of my ride. But I also noticed that I could also save directly to a .kml file that didn't have any weirdness.

So here is my problem...for all my files prior to last last year, I went back and hastily made KML files for all the rides. But when I named them, I just named them with the date. So, if it was Sunday's ride, it would simply be:
5-17.kml

IS there a way to write a batch file that would rename all the .kml files to add all the extra info/

Basically, what I want it to do is search for all the .kml files, and match them up with any .nav file that has the same first 4 characters (or 3 characters for dates like 5-3, or 5 characters for ones like 10-24) and then rename the kml to the same filename as its corresponding .nav file. So in the example above, it would find 5-17.kml, and then search for any .nav file that starts with "5-17" and rename the .kml to be the same as that corresponding .nav file. Then move on to the next .kml file.

I hope that is somewhat clear. There are a couple hundred files that need to be changed, so it would take awhile doing it manually.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: DOS batch for renaming files (sort of complicated)

#2 Post by foxidrive » 18 May 2015 05:15

A brute force approach should be simple, but a bit slower.

Are the files all in one folder, or maybe a single folder tree?

kerket
Posts: 5
Joined: 11 May 2015 02:27

Re: DOS batch for renaming files (sort of complicated)

#3 Post by kerket » 18 May 2015 05:26

Well...all the KML files are in the same folder, which is the subfolder to the one that the others are in. But that can easily be changed if I had to. But right now, for example, there is a folder called, say 2014 that includes the .NAV, KMZ and GPX file for each ride..all named with the date and roads in the filename.. And then there is a folder named KML that includes the KML for each ride with just the date as the filename.

It honestly didn't a huge deal to even leave it, which I why I haven't bothered doing it manually. But on the rare occasions that I want o go back an check something, or open up multiple KML files, it is a little bit of a pain to have to figure out which ones I want by looking at the other folder, and then going to finding that. But that is why I use the road names in the other filenames. Because it helps me identify my rides a lot easier. I rarely, if ever, do the same roads on any given bike ride. I always try to do SOMETHING different each time. So having the road names of the hills I climb is all I need to identify the ride I am looking for.

So like I said, right now the KML files are in a separate subfolder as the others. But that could easily be changed if I needed. I could either move them into the same folder as all the others, or I could move all the .nav files into that subfolder temporarily so we wouldn't have to deal with the KMZ and GPX ones.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: DOS batch for renaming files (sort of complicated)

#4 Post by foxidrive » 18 May 2015 05:52

Put this in the main folder and run it - you should see rename commands and pause after each one.
Make sure the rename command is right, and then remove the echo and the top pause and run it for real.

You may like to test it in a temp folder with some copies of your files first.

Code: Select all

@echo off
for /r %%a in (*.kml) do (
      for /r  %%b in ("%%~na *.nav") do (
echo          ren "%%a" "%%~nb%%~xa"
pause
      )
)
pause

kerket
Posts: 5
Joined: 11 May 2015 02:27

Re: DOS batch for renaming files (sort of complicated)

#5 Post by kerket » 19 May 2015 17:38

I tried it. And based on what is coming up in the window, it seems like it SHOULD work. I don't get any error messages or anything. But for whatever reason the filenames aren't changed.

Even stranger, if I open a Command Prompt window and manually type the exact same thing that comes up with the script your wrote, it DOES work. So now I am totally baffled.

ShadowThief
Expert
Posts: 1166
Joined: 06 Sep 2013 21:28
Location: Virginia, United States

Re: DOS batch for renaming files (sort of complicated)

#6 Post by ShadowThief » 20 May 2015 06:10

Did you remember to remove the echo from the ren line?

kerket
Posts: 5
Joined: 11 May 2015 02:27

Re: DOS batch for renaming files (sort of complicated)

#7 Post by kerket » 22 May 2015 04:48

ShadowThief wrote:Did you remember to remove the echo from the ren line?



Nope. Forgot that part.

Works fine now.

I know it took me longer to get this working then it would have if I just did it manually..but this adds to my knowledge-base for the future. Thaks.

Post Reply