Automatically rename files incrementally when dropped into folder

Dear all,

Every day I run a program that I then export the results into a specific folder on my computer.

The problem i have is that there are lots of exports I may do in one time, and the way I do it is save the file as
1.htm then 2.htm, 3.ht and so on.

All I need to do here really is just save the numbers, as the extension gets saved automatically.

Basically what I am doing is exporting, saving as 1, click save and then do the same for the next file, export, save as 2 and so on.

The problem I am having is that there maybe 30 or 40 of these files in a folder and it takes ages.

Can I have a batch file that I have in the destination folder that I will start once I am about to export...

When a file gets dropped in the batch file will automatically rename it to 2, then when the next one comes in, it will auto increment it and rename it to three.

All I will need to do is save it as number 1 each time...

this would save me a heck of a lto of time, and I would really appreciate it if you professionals could help me out with this.

I would also be running this on windows 98 SE.

Thanks in advance.

Kevin
LVL 2
oconnork00Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

RobSampsonCommented:
Hi Kevin,

Which program are you running to do this?  Is it a Microsoft Office product?  If so, this could automated better than a third-party application.  A third-party application would require SendKeys which can be *very* unreliable because it only sends keystrokes to the active window, and if it's not the right window, you'll have problems.

Regards,

Rob.
0
oconnork00Author Commented:
The program is third party - Keyword Elite...

I have just found a paid solution - Fold Monkey. It allows me to monitor a folder and while I save the files into the folder it will rename them.

That software will do exactly what I need, but it's paid software. I was hoping it could be done using a batch file - can it?


Also, you mention automating a MS application - this is something I will need too. Where would you recommend posting a question on this? MS Office?


Many thanks,

Kevin
0
RobSampsonCommented:
Oh ok.....so you're looking at it from the other side....

See, I was thinking that from your application, you'd want to automate the file name to save to, whereas you're thinking from the folder level, you rename files automatically then save as the same name from the application.....that can be done with VBS, for free!

I'll work on that in a minute...in the meantime, for Windows 98, you need to make sure your Windows Script Host is running: http://support.microsoft.com/kb/188135

Try a VBS file like this (save code into Notepad, then save file with a VBS extension):

Set objShell = CreateObject("WScript.Shell")
MsgBox "Hello " & objShell.ExpandEnvironmentStrings("%USERNAME%")

Um, and for automating Office, post in the General Office Suites zone, and the VBScript zone.

Regards,

Rob.
0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

RobSampsonCommented:
OK, here's something for you to test......

Save the code I post below into a VBS file.

I know it looks complicated, but it does most of the work for you.

The only lines you need to modify are these two:
strProgramToRun = "notepad.exe"
strFolder = "N:\Scripting\Test Scripts\Rename Files Added to Folder"

where in strProgramToRun you specify the full path to the program executable, and in strFolder
you specify the full folder where you will be placing your output files from the program.

What this script will do is launch your program, and monitor the folder for any files that you output.  It will then rename a new file to <filename>_Auto####.<ext>

Then, when you have finished exporting files, close your program, and the script should quit itself, and no longer monitor the folder.

'==========================
Set objShell = CreateObject("Wscript.Shell")
Set objFSO = CreateObject("Scripting.FileSystemObject")
strProgramToRun = "notepad.exe"
strFolder = "N:\Scripting\Test Scripts\Rename Files Added to Folder"
Set objExec1 = objShell.Exec(strProgramToRun)

strThisScript = WScript.ScriptFullName
strMonitor = Replace(WScript.ScriptFullName, WScript.ScriptName, "") & "FileMonitor.vbs"

'***** FileMonitor.vbs code for this script to write *****
strCode = "If WScript.Arguments.Count = 0 Then" & VbCrLf & _
      "      MsgBox ""Script cannot run without arguements.""" & VbCrLf & _
      "      WScript.Quit" & VbCrLf & _
"End If" & VbCrLf & _
"" & VbCrLf & _
"Set objFSO = CreateObject(""Scripting.FileSystemObject"")" & VbCrLf & _
"" & VbCrLf & _
"strCallingScript = WScript.Arguments.Item(0)" & VbCrLf & _
"strFolder = WScript.Arguments.Item(1)" & VbCrLf & _
"strComputer = "".""" & VbCrLf & _
"intSeconds = 2" & VbCrLf & _
"" & VbCrLf & _
"Set objWMIService = GetObject(""winmgmts:\\"" & strComputer & ""\root\cimv2"")" & VbCrLf & _
"" & VbCrLf & _
"Set colMonitoredEvents = objWMIService.ExecNotificationQuery _" & VbCrLf & _
"      (""SELECT * FROM __InstanceCreationEvent WITHIN "" & intSeconds & "" WHERE "" _" & VbCrLf & _
"            & ""Targetinstance ISA 'CIM_DirectoryContainsFile' and "" _" & VbCrLf & _
"                  & ""TargetInstance.GroupComponent= "" _" & VbCrLf & _
"                        & ""'Win32_Directory.Name="""""" & Replace(strFolder, ""\"", ""\\\\"") & """"""'"")" & VbCrLf & _
"" & VbCrLf & _
"Do" & VbCrLf & _
"      Set objLatestEvent = colMonitoredEvents.NextEvent" & VbCrLf & _
"      strNewFile = objLatestEvent.TargetInstance.PartComponent" & VbCrLf & _
"      arrNewFile = Split(strNewFile, ""="")" & VbCrLf & _
"      strFileName = arrNewFile(1)" & VbCrLf & _
"      strFileName = Replace(strFileName, ""\\"", ""\"")" & VbCrLf & _
"      strFileName = Replace(strFileName, Chr(34), """")" & VbCrLf & _
"      If LCase(WScript.ScriptFullName) <> LCase(strFileName) And _" & VbCrLf & _
"      LCase(strCallingScript) <> LCase(strFileName) Then" & VbCrLf & _
"            'MsgBox ""New file added: "" & strFileName" & VbCrLf & _
"            strBaseName = objFSO.GetBaseName(strFileName)" & VbCrLf & _
"            boolRename = False" & VbCrLf & _
"            If Len(strBaseName) > 9 Then" & VbCrLf & _
"                  strCheckAuto = Right(strBaseName, 9)" & VbCrLf & _
"                  If Left(strCheckAuto, 5) <> ""_Auto"" Then" & VbCrLf & _
"                        boolRename = True" & VbCrLf & _
"                  End If" & VbCrLf & _
"            Else" & VbCrLf & _
"                  boolRename = True" & VbCrLf & _
"            End If" & VbCrLf & _
"            If boolRename = True Then" & VbCrLf & _
"                  For intNum = 0 To 9999" & VbCrLf & _
"                        If intNum < 10 Then" & VbCrLf & _
"                              strNewName = objFSO.GetParentFolderName(strFileName) & ""\"" & strBaseName & ""_Auto000"" & intNum & ""."" & objFSO.GetExtensionName(strFileName)" & VbCrLf & _
"                        ElseIf inNum < 100 Then" & VbCrLf & _
"                              strNewName = objFSO.GetParentFolderName(strFileName) & ""\"" & strBaseName & ""_Auto00"" & intNum & ""."" & objFSO.GetExtensionName(strFileName)" & VbCrLf & _
"                        ElseIf inNum < 1000 Then" & VbCrLf & _
"                              strNewName = objFSO.GetParentFolderName(strFileName) & ""\"" & strBaseName & ""_Auto0"" & intNum & ""."" & objFSO.GetExtensionName(strFileName)" & VbCrLf & _
"                        Else" & VbCrLf & _
"                              strNewName = objFSO.GetParentFolderName(strFileName) & ""\"" & strBaseName & ""_Auto"" & intNum & ""."" & objFSO.GetExtensionName(strFileName)" & VbCrLf & _
"                        End If" & VbCrLf & _
"                        If objFSO.FileExists(strNewName) = False Then Exit For" & VbCrLf & _
"                  Next" & VbCrLf & _
"                  objFSO.MoveFile strFileName, strNewName" & VbCrLf & _
"            End If" & VbCrLf & _
"      End If" & VbCrLf & _
"Loop"
'***** End of FileMonitor code *****

Set objFileMonitor = objFSO.CreateTextFile(strMonitor, True)
objFileMonitor.Write strCode
objFileMonitor.Close
Set objFileMonitor = Nothing

Set objExec2 = objShell.Exec("wscript """ & strMonitor & """ """ & strThisScript & """ """ & strFolder & """")

While objExec1.Status = 0
      WScript.Sleep 100
Wend

objExec2.Terminate
MsgBox "Program has been closed. Folder will no longer be monitored."
objFSO.DeleteFile strMonitor, True
'==========================

Regards,

Rob.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
oconnork00Author Commented:
Morning Rob,

That script looks good - it is essentially doing what's needed. Only problem is that when I go to start the program again, the script has changed the code inside the file automatically: See the snippet attached.

When I initially put this code into notepad I had the path of the folder there, c:\renamedfolder

Also if it doesnt start notepad, will it not work?

Thanks for your help,

Kevin



If WScript.Arguments.Count = 0 Then
      MsgBox "Script cannot run without arguements."
      WScript.Quit
End If
 
Set objFSO = CreateObject("Scripting.FileSystemObject")
 
strCallingScript = WScript.Arguments.Item(0)
strFolder = WScript.Arguments.Item(1)
strComputer = "."
intSeconds = 2
 
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
 
Set colMonitoredEvents = objWMIService.ExecNotificationQuery _
      ("SELECT * FROM __InstanceCreationEvent WITHIN " & intSeconds & " WHERE " _
            & "Targetinstance ISA 'CIM_DirectoryContainsFile' and " _
                  & "TargetInstance.GroupComponent= " _
                        & "'Win32_Directory.Name=""" & Replace(strFolder, "\", "\\\\") & """'")
 
Do
      Set objLatestEvent = colMonitoredEvents.NextEvent
      strNewFile = objLatestEvent.TargetInstance.PartComponent
      arrNewFile = Split(strNewFile, "=")
      strFileName = arrNewFile(1)
      strFileName = Replace(strFileName, "\\", "\")
      strFileName = Replace(strFileName, Chr(34), "")
      If LCase(WScript.ScriptFullName) <> LCase(strFileName) And _
      LCase(strCallingScript) <> LCase(strFileName) Then
            'MsgBox "New file added: " & strFileName
            strBaseName = objFSO.GetBaseName(strFileName)
            boolRename = False
            If Len(strBaseName) > 9 Then
                  strCheckAuto = Right(strBaseName, 9)
                  If Left(strCheckAuto, 5) <> "_Auto" Then
                        boolRename = True
                  End If
            Else
                  boolRename = True
            End If
            If boolRename = True Then
                  For intNum = 0 To 9999
                        If intNum < 10 Then
                              strNewName = objFSO.GetParentFolderName(strFileName) & "\" & strBaseName & "_Auto000" & intNum & "." & objFSO.GetExtensionName(strFileName)
                        ElseIf inNum < 100 Then
                              strNewName = objFSO.GetParentFolderName(strFileName) & "\" & strBaseName & "_Auto00" & intNum & "." & objFSO.GetExtensionName(strFileName)
                        ElseIf inNum < 1000 Then
                              strNewName = objFSO.GetParentFolderName(strFileName) & "\" & strBaseName & "_Auto0" & intNum & "." & objFSO.GetExtensionName(strFileName)
                        Else
                              strNewName = objFSO.GetParentFolderName(strFileName) & "\" & strBaseName & "_Auto" & intNum & "." & objFSO.GetExtensionName(strFileName)
                        End If
                        If objFSO.FileExists(strNewName) = False Then Exit For
                  Next
                  objFSO.MoveFile strFileName, strNewName
            End If
      End If
Loop

Open in new window

0
RobSampsonCommented:
Hi, I'm at home, so can only explain this quickly, but what you appear to be doing is saving the code I posted into a file called "FileMonitor.vbs".

It won't work that way.  You must call your script something else.  The script will create it's only "child" script call "FileMonitor.vbs", which it will then terminate and destroy, once the program specified by
strProgramToRun
is closed by the user.

So, what you need to do is change
strProgramToRun = "notepad.exe"

to something like
strProgramToRun = "C:\program files\keyword elite\kelite.exe"

and then when you run the script, Keyword Elite will start. As you export files to the specified folder, it will rename them.  Then when you close Keyword Elite, the temp script FileMonitor.vbs gets deleted, and the script terminates itself.

I hope that makes things clearer.

Regards,

Rob.
0
RobSampsonCommented:
Oh yeah, and the entire
'***** FileMonitor.vbs code for this script to write *****
section is actually the "middle" of the full script, which is the full code you need to be saving into a file other than FileMonitor.vbs

Regards,

Rob.
0
oconnork00Author Commented:
Ahhhh I get it now... I inadvertantly called the file filemonitor.vbs...


Ok that's great thanks Rob.

One more thing if you dont mind me asking, how long does it monitor the folder? Is it every three seconds? Can I change this to every one second?
Thanks for everything,

Kevin
0
RobSampsonCommented:
Hi, it's every 2 seconds.  In the full script, you see this line:
"intSeconds = 2" & VbCrLf & _

you can just change that to
"intSeconds = 1" & VbCrLf & _

to make it every second.

Regards,

Rob.
0
oconnork00Author Commented:
Thanks Rob, excellent code and solution.
0
oconnork00Author Commented:
Hi Rob,

I've also posted a question about the importing part I mentioned earlier, it's here:
http://www.experts-exchange.com/Software/Office_Productivity/Office_Suites/MS_Office/Excel/Q_23153002.html

I would appreciate your comments on this too :)

0
oconnork00Author Commented:
Hey Rob,

I have opened another VB question here:
http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/Q_23240121.html

I am running into a problem with the VB script and if you have time would appreciate it if you could help me!

Thanks Rob,

Kevin
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Windows Batch

From novice to tech pro — start learning today.