# Solved: Help with VBS script to rename files based on other files



## ScoutsOA (Jun 20, 2012)

I am pretty new to vbs, but I am trying to make a script that would rename some .jpgs based on a .wmv that would go with them. Basically, I'll have X amount of wmvs and x*2 jpgs. Two of the jpgs go with a wmv, they are pictures of the beginning and the end of the video. So I figure I need to use the creation time to grab the images and rename them, but the code is just not coming together for me. Attached is the code, any help would be great. 


Jpgs are named by the digital camera, while the videos have the naming scheme of "T44T-294-HHDDMMYY", where the last part is the hour and date. I would want the jpgs to be renamed "T44T-294-1-HHDDMMYY", or -3-, depending on which photo it is. 

The files only have a timestamp to link them together, one photo is made a few minutes before the video, and the other is a few minutes after.

Thanks!


----------



## Squashman (Apr 4, 2003)

Not much of a VBscript person but may be able to do it with a batch file but I need a better example of what your input file names look like and how you want the output file names to look like. Please provide a couple of examples if you can.


----------



## ScoutsOA (Jun 20, 2012)

Input jpgs are going to be generic names from a digital camera, aka 100-104, 100-105, etc.
Input wmvs are named like "F22S-121-03200212.wmv"
Output jpgs need to be renamed to "F22S-121-M1-03200212.jpg" and "F22S-121-M3-03200212.jpg" respective to time taken, where M1 is the first and the M3 is the last. The only link I have with the pictures is the creation date, so the M1 is before and M3 is after.

Another example-
Input Output
G44T-842-12130612.wmv ---> No changes
100-234.jpg ----------------------> G44T-842-M1-12130612.jpg
100-235.jpg ----------------------> G44T-842-M3-12130612.jpg


----------



## Squashman (Apr 4, 2003)

So the only way to match the WMV to the two JPG files is by creation date within the file attributes?
You won't have more than one WMV for each day and you won't have more than two jpg's with that same date in the folder?

So if I pull the creation date from the WMV's file attributes I should then be able to search for two JPG files with the same date?


----------



## ScoutsOA (Jun 20, 2012)

Sorry, I left out some pretty important bits on accident, haha.

Yes, that would be the only way, unless you can actually see the pictures and movies. There would be multiple wmvs and twice that number of jpg for each day. The jpgs would be ~15 minutes or less away from the wmv file. wmv's are spaced out ~30-45 from eachother.


----------



## Squashman (Apr 4, 2003)

Ugh. That makes it about a 1000% more difficult. I have never been good with time based math in batch files. Basically because you have to convert the Date and time into a more usable math base format. Like YYYYMMDD HHMM and it makes it lot easier if the time is in 24 hour format. Most people don't use 24 time format. I know it is not impossible but makes it more complicated.
I post over on the DosTips.com forums as well and they have a lot of examples over there on converting the file dates and times to usable formats.
http://www.dostips.com/DtTipsDateTime.php

Can you open up a cmd prompt and show me the output of this command in the directory that has your WMV and JPG files.

```
dir /a-d /-C *.wmv *.jpg>filelog.txt
```
The output is being redirected to a text file. You can then upload that file by using the Go Advanced button and then using the Manage Attachments feature.


----------



## Squashman (Apr 4, 2003)

Let me revise that.

```
dir /a-d /-C /OD /TC *.wmv *.jpg>filelog.txt
```


----------



## ScoutsOA (Jun 20, 2012)

Thanks for all the help, it's really appreciated


----------



## Squashman (Apr 4, 2003)

I need to know if your JPG file names are going to have a hyphen in them or not. Your written examples showed them as having hyphens but the text file you sent me doesn't have hyphens for the file names.

It is critical that you replicate your data as accurately as possible. I have already re-written 3 batch files this week on this forum and other forums because people were afraid to show me their real data and didn't think it would matter.


----------



## Squashman (Apr 4, 2003)

Nevermind. I think I worked around that.

I thought about doing this batch file several different ways but the one I chose is the most efficient in terms of processing speed. It is heavily reliant on the example you provided me in that text file.

From the text file your provided me, I am assuming I will always see a JPG file, then a WMV file and then another JPG file. As long as that sequence holds true for all your files then this batch file will work. So I guess I am assuming that the first JPG is created, then your WMV is created and then your 2nd JPG is created in that order for each set of files.

The nice thing about it, is I don't have to do any time based math to see if there are two jpg files created +/- 15 minutes of the WMV file.

```
@echo off
setlocal enabledelayedexpansion

set fcount=0

FOR /F "TOKENS=* delims=" %%G in ('dir /a-d /OD /TC /B *.wmv *.jpg') DO (
	set /a fcount+=1
	IF "!fcount!"=="1" set jpg1name=%%G
	IF "!fcount!"=="2" (
		set wmvname=%%~nG
		set wmvname1=!wmvname:~0,-8!
		set wmvname2=!wmvname:~-8,8!
	)
	IF "!fcount!"=="3" (
		set fcount=0
		set jpg2name=%%G
		echo rename "!jpg1name!" "!wmvname1!M1!-wmvname2!.jpg"
		echo rename "!jpg2name!" "!wmvname1!M3!-wmvname2!.jpg"
	)
)
pause
```
As a safety precaution I am just echoing to the screen what the file will be renamed to. Once you feel the output looks correct you can remove the two echo's before the rename commands so that it will actually execute the rename. You can also remove the pause at the end. Just putting that in there so you can see the output before the cmd prompt closes.


----------



## ScoutsOA (Jun 20, 2012)

Thanks so much for the help. I'll put the batch through the grinder and see what comes out. The creation dates are exactly like you said, jpg then wmv then jpg. I'll post my results when I get them  Also, sorry for not getting back to you sooner.


----------



## Squashman (Apr 4, 2003)

ScoutsOA said:


> Also, sorry for not getting back to you sooner.


No biggie. It actually made me rethink how to do the batch file.
Let me know if you need something changed.


----------



## ScoutsOA (Jun 20, 2012)

Doesn't work, just echos the process but nothing gets changed.

I ran into a much bigger problem, as the files I will be getting are getting uploaded onto a FTP and that changes the creation date. Ill have to try to figure out how to get the EXIF data and metadata...this is going to be miserable, haha.

Thanks for all the help, I do appreciate it all


----------



## Squashman (Apr 4, 2003)

ScoutsOA said:


> Doesn't work, just echos the process but nothing gets changed.


Reread my comments underneath the batch file I posted!


----------



## Squashman (Apr 4, 2003)

ScoutsOA said:


> I ran into a much bigger problem, as the files I will be getting are getting uploaded onto a FTP and that changes the creation date. Ill have to try to figure out how to get the EXIF data and metadata...this is going to be miserable, haha.


If you can find me a command line based utility to extract the date from the EXIF information I can rewrite the utility to work with that.


----------



## ScoutsOA (Jun 20, 2012)

> rename "1.jpg" "M1.jpg"
> rename "3.jpg" "M3.jpg"
> rename "4.jpg" "M1.jpg"
> rename "6.jpg" "M3.jpg"
> ...


That doesn't seem right. EXIF should be easy, it's finding something that reads wmv's in the command line that may be the problem. I don't know if MediaInfo can be command lined


----------



## Squashman (Apr 4, 2003)

ScoutsOA said:


> That doesn't seem right. EXIF should be easy, it's finding something that reads wmv's in the command line that may be the problem. I don't know if MediaInfo can be command lined


No. that is not correct output because your create dates are not like you told me they would be. I explicitly told you that the batch file worked just like the example you gave me from the DIR command I asked you to run. The first file has to be a JPG, the 2nd file has to be a WMV. The Third file has to be JPG. Has to keep rotating like that for each set of files in created date order. You even verified that in a previous post.

Like I said in one of my previous posts. I NEED ACCURATE DATA to do what you need me to do. If you don't give me accurate data, you get inaccurate results!


----------



## ScoutsOA (Jun 20, 2012)

> rename "1.jpg" "G22S-371-M1.jpg"
> rename "2.jpg" "G22S-371-M3.jpg"
> rename "3.jpg" "F22S-146-M1.jpg"
> rename "4.jpg" "F22S-146-M3.jpg"
> ...


Sorry sorry, I was trying it in a folder with FTP'd creation dates. It does work, sorryyyy!


----------



## Squashman (Apr 4, 2003)

If they would zip them up into a zip file first and then upload them to your FTP site they should maintain the original time stamps when they are unzipped on your end. At least they do for all the data files I get from our clients.

With the FTP,if they didn't FTP the files in the same order as they were created we are going to have problems unless we can extract the EXIF info from the JPG's. Not sure if WMV files maintain that information or not?


----------



## ScoutsOA (Jun 20, 2012)

Zipping and burning to a CD are looking like the best ways to avoid the timestamp issue from my research. WMVs do keep such info as Metadata, but programs to harvest the information are few. MediaInfo seems to be the best, may be able to get something out of it.

Here is a sample of the script on an freshly FTP'd batch.



> rename "image (4).jpg" "iM1.jpg"
> rename "image (6).jpg" "iM3.jpg"
> rename "image (7).jpg" "iM1.jpg"
> rename "image (9).jpg" "iM3.jpg"
> ...


I can't guarantee the naming convention of the JPGs sadly. Attached is the logfile of the folder

Edit: I meant I can't guarantee the naming convention of the input JPGs. Also, just noticed that the output JPG name isn't entirely correct, I need the date appended onto it from the WMV. For example, Ill use the above.

"Z02S-002-08210612.wmv" -----> "Z05S-004-M1*-08210612*.jpg" "Z05S-004-M3*-08210612*.jpg"


----------



## Squashman (Apr 4, 2003)

Got some good ideas in here for changing the file date to the EXIF creation date.
http://www.velocityreviews.com/forums/t431195-changing-file-date-to-exif-date.html

Still don't know about the WMV.


----------



## ScoutsOA (Jun 20, 2012)

http://www.relliksoftware.com/exifdatechanger/
Worked a tab better, GUI wise anyway. Did exactly what we want. Don't know if it could be command lined though


----------



## Squashman (Apr 4, 2003)

ScoutsOA said:


> http://www.relliksoftware.com/exifdatechanger/
> Worked a tab better, GUI wise anyway. Did exactly what we want. Don't know if it could be command lined though


That looks like it changes the EXIF information. I need to pull the EXIF create date and change the file create date. Kind of backwards.


----------



## ScoutsOA (Jun 20, 2012)

I just used the GUI to set a batch of photos to the proper "Date Created", so it has the capabilities.


----------



## Squashman (Apr 4, 2003)

So are you saying all you have to do is point that software at a folder of photos and it will set the File Create date within Windows to the File Create date of the EXIF information? If that is the case then all you need to do is figure out the WMV file date and my batch file will work.


----------



## ScoutsOA (Jun 20, 2012)

Correct sir. Been looking all morning for something promising. Because video files don't have a standard for that kind of data like EXIF, it's a real treasure hunt.


----------



## Squashman (Apr 4, 2003)

Why don't you ask the person hosting the ftp files to run your batch file before putting them up on the server.


----------



## ScoutsOA (Jun 20, 2012)

I want everything done in-house if need be, to make sure there is as little operator error as possible. Ive convinced them to just send zip files now, so everything is good. Could you take a look at post #20 on the edit? I've been messing around with the code trying to get it to display the last part, but I am hopeless it seems


----------



## Squashman (Apr 4, 2003)

Ugh. I transposed the hyphen and exclamation

```
@echo off
setlocal enabledelayedexpansion

set fcount=0

FOR /F "TOKENS=* delims=" %%G in ('dir /a-d /OD /TC /B *.wmv *.jpg') DO (
	set /a fcount+=1
	IF "!fcount!"=="1" set jpg1name=%%G
	IF "!fcount!"=="2" (
		set wmvname=%%~nG
		set wmvname1=!wmvname:~0,-8!
		set wmvname2=!wmvname:~-8,8!
	)
	IF "!fcount!"=="3" (
		set fcount=0
		set jpg2name=%%G
		echo rename "!jpg1name!" "!wmvname1!M1[color=red][b]-![/b][/color]wmvname2!.jpg"
		echo rename "!jpg2name!" "!wmvname1!M3[color=red][b]-![/b][/color]wmvname2!.jpg"
	)
)
pause
```


----------



## ScoutsOA (Jun 20, 2012)

Looks great sir, thanks a lot for your help  I really appreciate it <3


----------

