Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

How to construct batch or macro in Win OS

  1. Nov 13, 2009 #1


    User Avatar
    Science Advisor
    Homework Helper

    In a windows (98,XP,VISTA) OS, is there a simple procedure to create a macro to rename files by adding a string of character(s) at the beginning (or end) of each existing filename within a folder?

    I often run into repetitive tasks on my computer(s), for example; renaming files within
    a folder in a sequence. I have a folder with 120 songs in it and don't want the OS manager to sort them alphabetically, but in a specific order designated by number e.g. 1,2,3....120
    When I created each file, I began numbering them this way. However when I was done, the file system sequenced them in a way I didn't appreciate.. e.g. 100-120 is listed before 20-99, things like that... So I have begun to rename my files, with a leading 3 digit sequence.. 001,002,...120

    This is the tedious task that I would like write a batch or macro to accomplish the renaming.
    I recall being able to create small macros similar to this, in UNIX on the university mainframe.
    Last edited: Nov 13, 2009
  2. jcsd
  3. Nov 13, 2009 #2
    One thing you could do is to use a text editor. Use something like this:

    dir /b >t.bat

    That gets you a list of files. You can then use a text editor's macro recording to construct the rename statement and then replay it.
  4. Nov 13, 2009 #3
    .. or maybe use the editors search/replace with wildcards and regular expressions
  5. Nov 13, 2009 #4


    User Avatar
    Science Advisor
    Homework Helper

    Thanks for your suggestion. I just tried a sample directory with using this modified syntax:
    dir /b >folder_list.txt and see what you mean about it listing the file names in a text file (alphabetically). (where "> " is used to redirect the dir list output to a file instead of the terminal window). Now I need to teach myself about recording macros.

    You've also alerted me that DOS command language is alive an well in modern Window's OS's.
    I will review some of these. e.g. http://www.windowsvistauserguide.com/vista_dos_commands.htm".
    Also http://commandwindows.com/batchfiles-iterating.htm" [Broken] seems like a useful idea for my application.
    Last edited by a moderator: May 4, 2017
  6. Nov 14, 2009 #5


    Staff: Mentor

    Yes, the old DOS batch files are alive and well, as you have found out. There is also something new, Windows Power Shell, which is available for Win Server 2003 and later, and Win XP SP3, and Vista. Using Power Shell you can write what are called cmd-lets. I don't know much about them other than they are tied into .NET Framework and can be used and written to do batch-file sorts of things.

    If that's of interest, here's link: http://support.microsoft.com/kb/926139
  7. Nov 14, 2009 #6
    other recommendations:

    1) Download free Visual Studio or eclipse or perl and make compile the source in your preferred language

    2) Write a macro in excel using vba that should also do the job
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook