how to make a batch file to rename a folder when I start and close a program? [closed]
Andrew Mclaughlin
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
- start the program
- wait until the program is fully loaded
- rename the folder
- the program waits until I close it
- when I'm about to close the program it must not close (wait) until the batch rename the folder back
- close the program
3 Answers
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.
4See 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 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,