Solved Batch to rename files as they are created

April 6, 2012 at 14:13:46
Specs: Windows 7, AMD 2.20Ghz/4.0Gb RAM
I am playing with iCloud and trying to figure out a way to rename files that are added to my photostream folder so they are not deleted if they are deleted from the iPhone. I have found that if they are renamed or copied, they can be deleted from the phone, but remain on my laptop.

I have NO coding experience, but i realize that this will have to be a continually running program to catch files as they come in. Can someone help? Thanks in advance!

--Patch


See More: Batch to rename files as they are created

Report •

#1
April 8, 2012 at 15:56:18
Probably not a good idea to have a batch file run continually (takes up a lot of cpu) but you could append the files with the date or time (or combination of both) or just with a word of some kind

Also, what is the extension of the files??

how would you like the files renamed??


Report •

#2
April 9, 2012 at 13:54:57
✔ Best Answer
Back in '09 I did something similar, only it was limited to a single rename every 5-ish seconds. I took that, removed the 1 file per 5 seconds limit, and set the watch directory to "[My Pictures]\Photo Stream\My Photo Stream."

Save this as "renPhotoStream.vbs" (not renPhotoStream.vbs.txt), and run it. It should rename new files as they come in.

'ssfMYPICTURES = 0x27
dir = Replace(CreateObject("Shell.Application").NameSpace(&H27).Self.Path, _
      "\", "\\\\") & "\\\\Photo Stream\\\\My Photo Stream"

'First and foremost: Make sure we have a closeable Command Prompt window,
'as there is no other way to stop the script.
If Instr(1, WScript.FullName, "WScript", 1) Then _
  WScript.Quit CreateObject("WScript.Shell").Run("cscript.exe """ _
      & WScript.ScriptFullName & """", 4, True)

Dim name, WMI, renamed : Set WMI = GetObject("winmgmts:")
'Renaming file sends a new "file created" event, so we need to remember our work.
Set renamed = CreateObject("Scripting.Dictionary")

'Query shamelessly stolen from 
'http://www.microsoft.com/technet/scriptcenter/resources/qanda/apr05/hey0404.mspx
Set wmiEvent = WMI.ExecNotificationQuery ( _
    "SELECT * FROM __InstanceCreationEvent WITHIN 5 WHERE " _
    & "Targetinstance ISA 'CIM_DirectoryContainsFile' AND " _
    & "TargetInstance.GroupComponent= " _
    & "'Win32_Directory.Name=""" & dir & """'")
While True
  With WMI.Get(wmiEvent.NextEvent.TargetInstance.PartComponent)
    If Not renamed.Exists(.Drive & .Path & .FileName & "." & .Extension) Then
      name = .Drive & .Path & .FileName  & "_" & GetDateTime & "." & .Extension
      WScript.Echo "Attempting to rename " & .Name & " -> " & name
      Do Until .Rename(name) = 0
        WScript.Echo "Failed; will retry..."
        WScript.Sleep 1000
      Loop
      renamed(name) = True
    Else 'File has already been renamed.
      renamed.Remove(.Drive & .Path & .FileName & "." & .Extension)
    End If
  End With
Wend

Function GetDateTime
  Dim d : d = Now
  GetDateTime = DatePart("yyyy", d) & "_" & DatePart("m", d) & "_" & DatePart("d", d) _
      & "_" & DatePart("h", d) & "_" & DatePart("n", d) & "_" & DatePart("s", d)
End Function


You can tell it's old, because it has comments in it.

How To Ask Questions The Smart Way


Report •

#3
April 10, 2012 at 08:21:06
Fantastic Razor! that works like a charm. Thanks so much.

Report •
Related Solutions


Ask Question