Avatar of thenelson
thenelson

asked on 

Change file name and date created in batch or script

I have several thousand files with the following name format:

Doe, Jane 2006-02-25

I wish to rename all the files stripping the date and setting the file created date to the date in the name. For the example above:

File name:   Doe, Jane              Date created 02-25-2006

Here is a piece of VB script I wrote but I am having problems with the lines that change the file name and create date:

Dim FileName As String, NewFileDate As String, NewFileName As String
Dim pos As Integer

ChDir "\Temp1"
FileName = Dir("\temp1\*.*")
While FileName <> ""
    pos = InStrRev(FileName, " ")
    NewFileName = Left(FileName, pos - 1)
    Trim NewFileName
    NewFileDate = Mid(FileName, pos + 1)
    NewFileDate = Left(NewFileDate, Len(NewFileDate) - 4)
    Debug.Print FileName, "]" & NewFileName & "[", "]" & NewFileDate & "["
    '***** Shell "ren """ & FileName & """ """ & NewFileName & ""  *****   PROBLEM HERE
    '***** Shell "Touch """ & NewFileName & """ /d:" & NewFileDate   ***** PROBLEM HERE
    FileName = Dir
Wend

Can someone either help me with my code or provide a different approach?

Thanks!
Scripting LanguagesWindows BatchVB Script

Avatar of undefined
Last Comment
thenelson
Avatar of sirbounty
sirbounty
Flag of United States of America image

one thing I spot is that ren is an enternal command...thus if you're going to use it, you'll need to pass it through the command processor...

Shell "ren " & chr(34) & FileName & chr(34) & " " & chr(34) & NewFileName & chr(34)

I wouldn't think that touch would cause a problem though, unless it's not in your path...have you tried pointing to the container folder for touch?
Avatar of thenelson
thenelson

ASKER

On the line:
Shell "ren " & chr(34) & FileName & chr(34) & " " & chr(34) & NewFileName & chr(34)
I am getting "File not found"
Same thing for
Shell "Command ren " & chr(34) & FileName & chr(34) & " " & chr(34) & NewFileName & chr(34)

Touch works fine except it only excepts short (8.3) file names.
ASKER CERTIFIED SOLUTION
Avatar of sirbounty
sirbounty
Flag of United States of America image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of thenelson
thenelson

ASKER

Shell "cmd /c ren " & chr(34) & FileName & chr(34) & " " & chr(34) & NewFileName & chr(34)

took cure of that line - thanks!

Now I have to find a utility that changes the file date and accepts long file names or a way to feed the corset short name to Touch.
Avatar of thenelson
thenelson

ASKER

sirbounty,
With your help, I got it. Instead of Touch I am now using DirDate:

ChDir "C:\Temp1"
FileName = Dir("\temp1\*.*")
While FileName <> ""
    pos = InStrRev(FileName, " ")
    NewFileName = Left(FileName, pos - 1)
    Trim NewFileName
    NewFileDate = Mid(FileName, pos + 1)
    NewFileDate = Left(NewFileDate, Len(NewFileDate) - 4)
    NewFileDate = Mid(NewFileDate, 6) & "-" & Left(NewFileDate, 4)
    Shell "cmd /c ren """ & FileName & """ """ & NewFileName & ".tif"""
    Shell """C:\Program Files\DirDate\dirdate.exe""" & " Date=" & NewFileDate & " """ & NewFileName & ".tif"""
    FileName = Dir
Wend:

Thanks for your help.
Nelson
VB Script
VB Script

VBScript (Visual Basic Scripting Edition) is an interpreted scripting language developed by Microsoft that is modeled on Visual Basic, but with some important differences. VBScript is commonly used for automating administrative and other tasks in Windows operating systems (by means of the Windows Script Host) and for server-side scripting in ASP web applications. It is also used for client-side scripting in Internet Explorer, specifically in intranet web applications.

39K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo