Help Creating "Drag & Drop" .bat file to Append File Names


  1. Posts : 3
    Windows 10
       #1

    Help Creating "Drag & Drop" .bat file to Append File Names


    I have been attempting to come up with a way to be able to drag and drop a file to append 3 letters to it. The issue is that the extent of my programming knowledge lies in VBA and not much else. While I could probably make it work in VBA, I'd prefer to not require the application to be open when it's needed - which leads me to the usage of a .bat file.

    The following are examples of what I was thinking of before and after file names. It would need to append ABC after the file name, but prior to the file extension.

    test1.pdf ---> test1 ABC.pdf
    test2 - substr.pdf ---> test2 - substr ABC.pdf
    6.9.21 test3.pdf ---> 6.9.21 test3 ABC.pdf

    This will be on my office PC, so my PC specs may be different than what's in my bio, although I have a feeling it wouldn't have made much of a difference either way.

    Windows 10 Pro
    Version 20H2
    Build 19042.985

    Thanks for any assistance and your time.

    - - - Updated - - -

    Some additional info. My thought was the .bat file would remain on my desktop. I would select 1 or more .pdf files from any folder or location and drag them and drop them into this file. The .bat file would go to work and automatically rename them by appending 'ABC' at the end of the file name (but before the ext).

    I found some topics online that are similar to my issue, but I am not familiar enough with this particular process to modify to my needs.
    Batch script, file drag&drop, change extension and reload file - Stack Overflow
      My Computer


  2. Posts : 16,966
    Windows 10 Home x64 Version 22H2 Build 19045.4170
       #2

    I have a similar renaming tool so here are my thoughts on what you are considering:

    1 When I tried using a batch file to do the job, I found that it kept hitting the buffers because it was trying to rename a file that was currently not free to be worked on [which was never logical to my mind].
    2 If you have got to learn anything new then learn PowerShell not batch files.
    3 If you put a shortcut to your script in SendTo then, instead of dragging & dropping, you can just right-click, SendTo, <choose your renaming script>
    Code:
    %userprofile%\AppData\Roaming\Microsoft\Windows\SendTo
    4 You ought to put your script in a folder that is protected against unauthorised us. This is how I do that - Set up my Tools folder ditty - TenForums
    5 I use vbs for my equivalent tool. You cannot use my script as it stands but the similarity with VBA will probably help you see how you could write one for yourself in vbs quite quickly.
    RenameDLM.zip
    You are in a different region so extracting the date-time stamp would require different parts of the DLM variable to be extracted.
    I had always intended to re-implement this in PowerShell one day but the vbs has been so reliable that it hasn't risen in my list of priorities.

    PowerShell Scripting - The Basics - TenForumsTutorials
    PowerShell Scripting - Run a Script from Shortcut - TenForumsTutorials
    PowerShell commands index - SS64
    PowerShell forum - SS64
    PowerShell Tutorials - Windows10Forums [2017] - I am normally wary of this website for reasons that I cannot remember [lots of content copied from this forum possibly?] but their PS tutorial seems OK.
    Mastering PowerShell with Dr. Tobias Weltner [2009]PowerShell Commands - ScriptRunner


    Denis
      My Computer


  3. Posts : 3
    Windows 10
    Thread Starter
       #3

    Thanks Try3.

    While I basically rewrote the code for the most part, you definitely lead me to the answer with what you provided. Thanks for your help!

    Here's the result with comments:

    Code:
    Option Explicit
    
    Dim FSO: Set FSO = CreateObject("Scripting.FileSystemObject")
    Dim sInit: sInit = GetInitials
    
    Dim FileCounter, ObjectInUse, OldName, NewName
    For FileCounter = 0 to WScript.Arguments.Count-1
    
    	Set ObjectInUse = fso.GetFile(WScript.Arguments.Item(FileCounter))
    	
    	OldName = ObjectInUse.Name
    	
    	'Use regex to get the file name (.*) and the file ext (\.[A-Z]{1,4})$
    	'Then reconstruct the string with the file name .Item(0), the initials (with a space) " " & sInit, and the file ext .Item(1)
    	With New RegExp
    		.IgnoreCase = True
    		.Pattern = "(.*)(\.[A-Z]{1,4})$"
    		With .Execute(oldname)(0).SubMatches
    			NewName = .Item(0) & " " & sInit & .item(1)
    		End With
    	End With
    	
    	'Use FSO to rename the file
    	ObjectInUse.Name = NewName
    	
    Next
    
    Function GetInitials
    
    	'Get user's app folder to load/save initalials to
    	Dim sAppData: sAppData = CreateObject("WScript.Shell").ExpandEnvironmentStrings("%AppData%")
    
    	'Check if the file that stores the user's initials is saved. If so, read the file to be used as the return value for function.
    	'Otherwise, create a new file and prompt the user for their initials and save to the new text file for later use.	
    	With FSO
    		If Not .FolderExists(sAppData & "\KeithMacros\") Then .CreateFolder sAppData & "\KeithMacros\"
    		If Not .FileExists(sAppData & "\KeithMacros\SendToInitials.txt") Then 
    			With .CreateTextFile(sAppData & "\KeithMacros\SendToInitials.txt")
    				Do
    					sInit = InputBox("This is your first time using this program. Please enter your initials", "Enter Initials")
    				Loop until MsgBox("Are you sure you want to save the initials [" & sInit & "]?", vbYesNo, "Please Confirm") = vbYes
    				.Write sInit
    				.Close
    			End With
    		Else
    			With .OpenTextFile(sAppData & "\KeithMacros\SendToInitials.txt")
    				GetInitials = .ReadAll
    				.Close
    			End With
    		End If
    	End With
    	
    End Function
      My Computer


  4. Posts : 16,966
    Windows 10 Home x64 Version 22H2 Build 19045.4170
       #4

    Glad to have helped.

    Since you are providing for several users, do you also want to provide for several computers in the filename change by including the ComputerName property?

    I do not want to be a bad influence by encouraging you to use vbs too much [instead of PS] but -
    I've still got the vbs helpfile from WInXP. It is excellent; it's the best vbs help they ever produced.
    - You can download a copy from Windows Scripting 5.6 Documentation by Ryan Farley
    - Just to get a quick feel for the help file when you get it, you could search it for ComputerName
    - You will probably notice a great similarity in its approach & presentation with the built-in VBA help in MS Office applications.
    The help file omits the ShellExecute command - see ShellExecute (Undocumented) - SS64
    - SS64 is a very good resource for Windows commands, vbs & PS.


    All the best,
    Denis
      My Computer


 

  Related Discussions
Our Sites
Site Links
About Us
Windows 10 Forums is an independent web site and has not been authorized, sponsored, or otherwise approved by Microsoft Corporation. "Windows 10" and related materials are trademarks of Microsoft Corp.

© Designer Media Ltd
All times are GMT -5. The time now is 17:59.
Find Us




Windows 10 Forums