Solved Renaming the files from date/time format to location name

January 16, 2013 at 07:33:27
Specs: Windows 7
I have a long list of image files. The camera captures the images and stores them automatically on disc as "-01-10-2013-092959.jpg" format. that is "-MM-DD-YYYY-HHMMSS.jpg"format. Stores as date/and time it was taken. But I need to rename those files according to the location it was taken. They are standard format. First camera takes from North Gate D then Hexane Storage then West Gate C then West Gate B then West Roadway then Utility then Meal Loading then Extraction. Now the files have to be renamed according to the this order. Is there a way to do this if I do once a week. The camera takes the pictures twice a day. So there would be ~112 files?

Any help?


See More: Renaming the files from date/time format to location name

Report •


#1
January 16, 2013 at 08:48:05
What would be the whole name of the image, as that would be the way to sort it. Or what differentiates which camera is that the suffix? As in are all the camera photos from 092959 desiganted as North Gate D?

:: mike


Report •

#2
January 16, 2013 at 13:35:38
✔ Best Answer
Here's a "starter-kit" that might be useable. It doesn't rename the files, it's only meant as a "demo model". Not sure how you wanted to handle the different days in regard to the new filenames. It will pause every 48 files, and it's set up to run from the working directory (the one with the actual jpg files themselves).
::===== begin batch
@echo off & setlocal enabledelayedexpansion
:: set these names to suit you
set n0=northD
set n1=hexane
set n2=westC
set n3=westB
set n4=westRoad
set n5=utility
set n6=meal
set n7=extract
set c1=0
for %%a in (*.jpg) do call :xx %%~na
goto :eof

:xx
set /a b="c1%%8"
set new=!n%b%!
echo rename %1.jpg %new%_%1.jpg
set /a c1+=1
set /a z="c1%%48"
rem if %z% equ 0 pause > nul
::====== end batch


Report •

#3
January 16, 2013 at 13:59:18
The Camera takes the image and stores them as file with file name as date/time format. The order of taking pictures as I said earlier is that pattern. Now I need a program or method to rename those files which has date time format as name to the location based because we know the sequence/order of the pictures taken.

May be this clarifies the doubt?

The timing is 24 hour format. So AM and PM will out of question.


Report •

Related Solutions

#4
January 18, 2013 at 13:46:55
@nbrane Thanks for the idea. But I could not understand why I have to take the modulus of 16 when I want only for 8? I just need to have the location file names as prefix to the existing file names. May be it became a big job. I do not need that much hard work. Just prefix is enough.

Report •

#5
January 18, 2013 at 18:32:26
Oh. I thought you wanted to get rid of the time-stamp, but on re-reading your post, I think I was wrong. (after all, timestamps are very important when dealing with monitoring devices, althought the file's date/time could also serve). So, you want to keep the time/date in the filename and just add the location? I will modify my code (in response #2) to address this. It is still a demo (does not rename).

Report •

#6
January 19, 2013 at 00:22:01
Thanks. Yes I just want to add the location as prefix to te file name!

Report •

#7
January 19, 2013 at 13:06:23
ok, try response #2, and if it looks right, just remove the "echo" from line 3 of "xx".

Report •

#8
January 19, 2013 at 15:23:59
Sure I will give update after testing. Mostly it will OK. But tell me why we have to use name "%%~na"?

Report •

#9
January 19, 2013 at 21:20:51
That was an artifact left over from when I thought you wanted location to be after the time-stamp, in which case I neede to remove the extension (which we know is .jpg anyway, due to the qualifier in the for-loop: *.jpg). It is no longer needed, you can just use %%a but if you do, take out the .jpg from the rename operation:
rename %1 %new%_%1
Either way should work, it doesn't really affect anything to leave it as it is or not. If you try to rename down a path, you can't include a path in the second portion:
rename c:\work\xyz c:\work\abc
fails, but "move" will let you do that: move c:\work\xyz c:\work\abc

Report •

#10
January 22, 2013 at 14:26:11
I had two more locations and modified the program like this...

::===== begin batch
@echo off & setlocal enabledelayedexpansion
:: set these names to suit you
set n0=site
set n1=NorthGateD
set n2=WestGateB
set n3=WestGateC
set n4=WestRoadway
set n5=Utility
set n6=Extraction
set n7=Meal
set n8=HexaneStorage
set n9=site
set c1=0
for %%a in (*.jpg) do call :xx %%a
goto :eof
:xx
set /a b="c1%%10"
set new=!n%b%!
rename %1.jpg %new%_%1.jpg
set /a c1+=1
set /a z="c1%%20"
rem if %z% equ 0 pause > nul
::====== end batch

But the system is saying could not find the file. I had placed the file in the same directory as the files. Secondly may be I have to take out the rem command from the last but one line as it is not stopping after the said number of files.

Any suggestion?


Report •

#11
January 22, 2013 at 14:35:09
I had corrected the file like this, used ~n option and removed rem then it worked. But I do not know how to come out of pause? Any command I have to press to release the pause?...

::===== begin batch
@echo off & setlocal enabledelayedexpansion
:: set these names to suit you
set n0=site
set n1=NorthGateD
set n2=WestGateB
set n3=WestGateC
set n4=WestRoadway
set n5=Utility
set n6=Extraction
set n7=Meal
set n8=HexaneStorage
set n9=site
set c1=0
for %%a in (*.jpg) do call :xx %%~na
goto :eof
:xx
set /a b="c1%%10"
set new=!n%b%!
rename %1.jpg %new%_%1.jpg
set /a c1+=1
set /a z="c1%%20"
if %z% equ 0 pause > nul
::====== end batch


Report •

#12
January 22, 2013 at 17:39:10
Any key on the keyboard (except ctrl, alt, shift, and the locks-keys) should release it until the next 20 lines, then it will pause again. However, there is nothing being displayed (since echo is off). If you want to see what is happening, you need to put an echo statement showing the rename operation, in addition to the rename operation itself. You might prefer to remove the "> nul" from the pause as well, so you know what's going on.

Report •

#13
January 22, 2013 at 17:53:46
Thank You I will try this. Instead of using nativity option ~n, I tried "dir" command and the system said could not find the file. I want have this file on my desktop so that I can easily remember.

Report •

#14
January 22, 2013 at 18:24:05
I'm not real clear what you mean. If you want an icon on the desktop that links to the batch? (Paths always seem to present the most glitches!) ~n vs. "dir" i don't understand. maybe if you can explicate in more detail what is not working and what you want to do. I'm not the brightest bulb on the string, so I may need help.

Report •

#15
January 23, 2013 at 07:57:19
The following is the program I used. May be I am wrong, but other program worked good while copying files from one location to the other (that is other program).

::===== begin batch
@echo off & setlocal enabledelayedexpansion
:: set these names to suit you
set n0=site
set n1=NorthGateD
set n2=WestGateB
set n3=WestGateC
set n4=WestRoadway
set n5=Utility
set n6=Extraction
set n7=MealLoading
set n8=HexaneStorage
set n9=site
set c1=0
for %%a in ('dir /s /b /o:n e:\Images\*.jpg') do call :xx %%a
goto :eof
:xx
set /a b="c1%%10"
set new=!n%b%!
rename %1.jpg %new%%1.jpg
set /a c1+=1
set /a z="c1%%20"
if %z% equ 0 pause > nul
::====== end batch


Report •

#16
January 23, 2013 at 13:58:27
a couple of things got crossed up. Here's what it needs, i think:
:: change the sort-order from name to file-date, otherwise every newyears you will
:: be greeted with a headache: 01-01-2014 comes before 12-31-2013
for %%a in ('dir /s /b /o:d e:\Images\*.jpg') do call :xx %%a
goto :eof
:xx
set /a b="c1%%10"
set new=!n%b%!
:: i believe %%a now looks like this: e:\images\-01-23-2013nn.jpg, so, you need
:: to strip off the path in part 2 of the rename operation {or use "move" instead}
:: {see my final comment in response # 9}
rename %1 %new%%~nx1
:: this helps you to see what the system is seeing, not always what we intended!
echo rename %1 %new%%~nx1
set /a c1+=1
set /a z="c1%%20"
if %z% equ 0 pause
:: ===== end

Report •

#17
January 23, 2013 at 17:56:57
Maybe I have to try this. The file is of the following format.

"-01-10-2013-092959.jpg" format. that is "-MM-DD-YYYY-HHMMSS.jpg"format.

No I have some doubts....

What do you mean "-01-23-2013nn.jpg"?

What do you mean "rename %1 %new%%~nx1"

Got it this file has to be in the native directory.

Could not understand second part in the rename command

But will try this comment.


Report •

#18
January 23, 2013 at 18:41:29
It's a "batch thing": for system variables (like %%a or %1), %~nx1 would mean: Name Xtension only, so:
e:\images\-01-23-2013-192345.jpg
would then (using %~nx1) look like:
-01-23-2013-192345.jpg
And, sorry, I just used "nn" to substitute for time-of-day (lazy, I didn't want to put all that extra stuff. I tend to over-assume people know what I'm getting at, but that's not really fair).

Report •

#19
January 25, 2013 at 09:43:24
If I use "*.jpg" as the file revoke in "for %%a in (*.jpg) do call :xx %%~na", the files revoked randomly. Now if I want to sort so that we can rename in proper order then I think I have to use dir command. And dir command string should not be in quotes then only it can see the files else it is saying file not found.

To sort is big challenge.. "for %%a in ('dir /s /b /o:d e:\Images\*.jpg') do call :xx %%a is not woking..Any idea how we can approach


Report •

#20
January 25, 2013 at 11:46:58
You can do one of two things, (but not both!) One, remove the /s:
for %%a in ('dir /b /o:d e:\Images\*.jpg') do call :xx %%a
because the /s option of "dir" puts the full path in front of the filenames, but this
will also restrict it to the one base directory, "e:\images", no branches.
OR, two, put the ~nx onto the var in the "rename":
rename %1 %new%%~nx1
(well, for that matter, you can just leave the ~nx on, because it won't hurt
regardless.) This pretty much brings us back to resp #16. If you've made other modifications, please post your code, because things should be up and flying at this point.


Report •

#21
January 25, 2013 at 11:56:04
This is the code I am using.....
::===== begin batch
@echo off & setlocal enabledelayedexpansion
:: set these names to suit you
set n0=site
set n1=NorthGateD
set n2=WestGateB
set n3=WestGateC
set n4=WestRoadway
set n5=Utility
set n6=Extraction
set n7=MealLoading
set n8=HexaneStorage
set n9=site
set c1=0
for %%a in ('dir /b /s /on *.jpg') do call :xx %%a
goto :eof
:xx
set /a b="c1%%10"
set new=!n%b%!
rename %1.jpg %new%%%~nx1
echo rename %1.jpg %new%%%~nx1
set /a c1+=1
set /a z="c1%%20"
pause
if %z% equ 0 pause
::====== end batch

I am getting could not find file "dir.jpg %%~nx1"

This is the output I am getting.


Report •

#22
January 25, 2013 at 12:22:14
you just need to get rid of the .jpg in the rename, otherwise it will look like
rename e:\images\-01-25-2013-153020.jpg.jpg site-01-25-2013-153020.jpg

rename %1 %new%%~nx1
echo rename %1 %new%%~nx1

(Did the "echo" not show this error?)
Now it should look like:
rename e:\images\-01-25-2013-153020.jpg site-01-25-2013-153020.jpg


Report •

#23
January 28, 2013 at 09:37:32
First of all I am not getting the sorted files, it is taking random file and renaming. so I changed the program like this...

::===== begin batch
@echo off & setlocal enabledelayedexpansion
:: set these names to suit you
set n0=site
set n1=NorthGateD
set n2=WestGateB
set n3=WestGateC
set n4=WestRoadway
set n5=Utility
set n6=Extraction
set n7=MealLoading
set n8=HexaneStorage
set n9=site
set c1=0
FOR /F ^"tokens^=*^, eol^=^

^" %%a in ('dir /b /o:n *.jpg' ) do call :xx %%a
goto :eof
:xx
set /a b="c1%%10"
set new=!n%b%!
rename %1 %new%%~nx1
echo rename %1 %new%%~nx1
set /a c1+=1
set /a z="c1%%10"
pause
if %z% equ 0 pause
::====== end batch

Using tokens landed in another trouble. Now delimiter character or end-of-line character is the problem. By using the dir command I am getting sorted files output but after every file renaming it is paused. Just one file and not 10 as per the program. I could not understand this. Any help?


Report •

#24
January 28, 2013 at 09:38:43
Got it!. I did not see another pause in the program.

Thanks


Report •

#25
January 28, 2013 at 10:36:22
The final program looks like this...

::===== begin batch
@echo off & setlocal enabledelayedexpansion
:: set these names to suit you
set n0=site
set n1=NorthGateD
set n2=WestGateB
set n3=WestGateC
set n4=WestRoadway
set n5=Utility
set n6=Extraction
set n7=MealLoading
set n8=HexaneStorage
set n9=site
set c1=0
FOR /F "tokens=*" %%a in ('dir /b /o:n *.jpg' ) do call :xx %%a
goto :eof
:xx
set /a b="c1%%10"
set new=!n%b%!
rename %1 %new%%~nx1
echo rename %1 %new%%~nx1
set /a c1+=1
set /a z="c1%%10"
if %z% equ 0 pause
::====== end batch

Thanks for your help in research.....


Report •

#26
January 28, 2013 at 11:17:31
Great! glad it's working now. Remember, thought, that you might encounter problem when the week is split between december and january due to the camera's naming sequence of mm-dd-yyyy, but if 'dir /o:d' doesn't work, i guess this will suffice for 51 weeks out of the year, and run the program mid-week on Dec. 31.

Report •


Ask Question