2

How to make a batch file to rename a folder when I start a program? - Let's say I want to start my word.exe, at the same time I need to rename a folder c:\users\my profile\my word documents to be my documents and when I exist the word.exe the batch rename the folder to its original name.

--edited later--

here what I need to do

  1. start the program
  2. wait until the program is fully loaded
  3. rename the folder
  4. the program waits until I close it
  5. when I'm about to close the program it must not close (wait) until the batch rename the folder back
  6. close the program
11
  • Please point out if you want the batch script to start your program, or the batch script should detect when the program is run. The latter is a lot more difficult. Commented Feb 28, 2015 at 16:20
  • You NEED to give an explanation of what you are trying to accomplish better. I suggest asking a new question with exactly what you want explained. You've already messed this one up beyond redemption.
    – krowe
    Commented Feb 28, 2015 at 16:48
  • please check my edited question. -Thank you-
    – hsawires
    Commented Feb 28, 2015 at 16:49
  • Also, the part where we need to wait for it to close is going to require that we find the name of the program (the Windows HWND name [or the window title] not the file name). Because of this a batch file is going to be a horrible way to do this. I'd use .NET or C myself. Tell us EXACTLY which program you are using (include the version). Also, tell us what it is listed as in Task Manager > Processes.
    – krowe
    Commented Feb 28, 2015 at 16:53
  • 1
    Renaming the directory before starting Word is easy as shown in the answers listed. Even renaming after Word starts is easy enough using something like start "rename" cmd.exe /c sleep.exe 500 ren "dir1" "dir2". But, I don't think you can rename the folders back before Word has exited without writing a Word plugin/extension. Or, without using something like AutoHotkey or AutoIt, where the Alt+F4 is intercepted by AHK which in turn renamed the directories back and then exits Word. (Starting Word from a shortcut using AHK would allow for much more accurate renaming of the directories as well..)
    – kodybrown
    Commented Feb 28, 2015 at 17:12

3 Answers 3

1

Just use the CALL statement to tell the batch script to wait while the program is open:

@ECHO OFF
RENAME "C:\users\my profile\my word documents" "my documents"
CALL "C:\Program Files\Microsoft Office\Office15\winword.exe"
RENAME "C:\users\my profile\my documents" "my word documents"

Just don't close the batch window yourself or else the folder won't be renamed back afterwards. You can minimize the likelihood of that happening by using a shortcut to run the script minimized or use VBS to run it hidden.

Update

I think this should accomplish the (nearly) equivalent task as you asked for without all of the headache. By simply keeping the intermediate copies around you should be able to achieve what you really want (which we are all still guessing at).

@ECHO OFF
SET source=C:\test
SET dest=C:\test_tmp

MKDIR "%dest%"
COPY /Y "%source%\*.*" "%dest%\"
CALL "C:\Program Files\Microsoft Office\Office15\winword.exe"
RMDIR /S /Q "%source%"
MKDIR "%source%"
COPY /Y "%dest%\*.*" "%source%\"
RMDIR /S /Q "%dest%"

If this isn't good enough for you then your best bet is to make a VBA script or .NET Application-Level Add-Ins for part of this because this can't really be done in BATCH alone.

4
  • the batch is working so fine -Thank you- ... but here is my need .. the winword.exe is running so fast .. I need to rename the folder after the winword.exe is fully loaded and the time I choose to close winword.exe it must rename the folder back before it is fully closed.
    – hsawires
    Commented Feb 28, 2015 at 16:34
  • please check my edited question
    – hsawires
    Commented Feb 28, 2015 at 16:50
  • thank you so much ... but it didn't work .. it still wait the program to close then copy the folder. ... but it could work if the batch run without waiting the program to close. something different then CALL and SLEEP for a while then execute the rest of the batch.( don't have to wait the program to close) maybe your first solution work either.
    – hsawires
    Commented Feb 28, 2015 at 17:54
  • No. It copies the folder before it even runs word. You have all of the data. Now you just need to understand your own problem well enough to solve it. This does everything you need. At any point you have two copies: one working copy and one from before you loaded word. It replace the original folder after you close word. Aside from this, your question can only be best solved from outside of a batch file (no matter what it is that you really want) IMHO.
    – krowe
    Commented Feb 28, 2015 at 18:01
2

See below, you need to change the 4 variables to match your needs. Save this code, suitably modified, as a batch file (.bat extension), and then run it from the command line (or from Run/Start).

@ECHO OFF
SETLOCAL
REM location is the directory below the one you want to rename
SET location=%USERPROFILE%\Documents
SET mydir_pre=testdir1
SET mydir_post=testdir2
SET myprogram=C:\windows\system32\notepad.exe

RENAME "%location%\%mydir_pre%" "%mydir_post%" 2>NUL||ECHO Oops - failed to rename "%location%\%mydir_pre%"&&GOTO :eof
REM start the program, while you are using this instance you will see testdir2
START "My Program" /WAIT "%myprogram%"||ECHO Unable to start %myprogram%&&GOTO :eof
RENAME "%location%\%mydir_post%" "%mydir_pre%" 2>NUL||ECHO Oops - failed to rename "%location%\%mydir_post%"&&GOTO :eof
ECHO Successful completion
ENDLOCAL
8
  • Maybe you point out that the user has to start the batch script instead the desired programm. Commented Feb 28, 2015 at 16:18
  • how could i let that batch wait until the program is completely loaded and then rename. and how to rename the folder back exactly before I close the program.
    – hsawires
    Commented Feb 28, 2015 at 16:19
  • the batch is easy to use and it is working so fine. ... but as I need is to wait the software notepad.exe to fully loaded before the folder is being loaded.
    – hsawires
    Commented Feb 28, 2015 at 16:40
  • Not sure why you are trying to achieve this? Notepad (which is a very small program) should load very quickly anyway - Word might take longer of course. It would be easy to start Notepad or Word running and then rename the folder, but the problem comes then with telling when you exit the program to rename the folder back to its original name, I can think of ways to do it but none that are easy and why does it matter? Using START /WAIT gets round the problem simply but the initial folder rename has to precede it.
    – gogoud
    Commented Feb 28, 2015 at 16:45
  • 2
    absolutely I just used notepad because everyone using Windows has it and as it happens I don't have Word on the machine I am writing this from... I still don't understand why the order of renaming and starting/ending the program is so important to you? i.e. why you want to start the program, then do the folder rename, and then rename the folder back before you close the program. Seems a strange requirement as well as a hard one to meet?
    – gogoud
    Commented Feb 28, 2015 at 16:52
1

here is a simple workaround ,

in a .bat file :

rename "path to the file" "NewName"  
"Path to your program" 
rename "path to the file with the new name" "defaultName"

an example from my computer:

rename "C:\Users\myAccount\Desktop\defaultFolder" "NewName" 
"C:\Program Files\BreakPoint Software\Hex Workshop v6.8\HWorks64.exe"
rename "C:\Users\myAccount\Desktop\NewName" "defaultFolder" 

when running the .bat file the file will be renamed and the program start, after the program is closed, the first name is reset.

GOOD LUCK,

Not the answer you're looking for? Browse other questions tagged .