Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: Move program files folder in Windows 7 batch file  (Read 8756 times)

0 Members and 1 Guest are viewing this topic.

Linux711

    Topic Starter


    Mentor

    Thanked: 59
    • Yes
    • Programming Blog
  • Certifications: List
  • Computer: Specs
  • Experience: Experienced
  • OS: Windows 7
Move program files folder in Windows 7 batch file
« on: June 26, 2013, 11:19:52 PM »
I am looking to create a batch file that can move the program files and program files (x86) folders to another location. I only intend on using this right after a fresh installation of win 7, so no programs are effected by the move. I know the basic process of doing this in command prompt, but I need to turn this into a batch.

Here are the commands I need to use in order:

ckill explorer.exe
sleep 3000
xcopy program files
xcopy program files (x86)
takeown /f * /r program files
cacls program files /t /g %username%:F
takeown /f * /r program files (x86)
cacls program files (x86) /t /g %username%:F
rd /s program files
rd /s program files (x86)
mklink /d program files [destination]
mklink /d program files (x86) [destination]
explorer.exe

(ckill and sleep are my own if you were wondering)

That's is the basic idea, but I'm really bad at writing batches. I can program pretty well in other languages, but I don't like dealing with the strange nuances of batch. I want to make the batch prompt for the destination program files and have me enter in the path. So I could enter D:\Programs and it would copy all the files to D:\Programs and D:\Programs (x86). I'm probably missing some attrib, quotes, etc. So if anyone can help me make this into a working batch, I'd appreciate it.

EDIT: Just realized I probably wasn't clear about how this "moves" the program files folder. It basically copies all the files to the destination and then creates a symlink Program Files and Program Files (x86), so when windows installs to C:\Program Files it is actually installing to another directory. This is different than simple shortcuts because it fools windows into thinking C:\Program Files is a real folder, when it's not.
YouTube

"Genius is persistence, not brain power." - Me

"Insomnia is just a byproduct of, "It can't be done"" - LaVolpe

Geek-9pm


    Mastermind
  • Geek After Dark
  • Thanked: 1026
    • Gekk9pm bnlog
  • Certifications: List
  • Computer: Specs
  • Experience: Expert
  • OS: Windows 10
Re: Move program files folder in Windows 7 batch file
« Reply #1 on: June 27, 2013, 01:30:45 AM »
Move program files?  :o
Sorry, I don't understand.  ???
Have you tested this concept?
Did you see where somebody else did that?
Why do you want to do that?


Squashman



    Specialist
  • Thanked: 134
  • Experience: Experienced
  • OS: Other
Re: Move program files folder in Windows 7 batch file
« Reply #2 on: June 27, 2013, 09:36:28 AM »
Why use CKILL and SLEEP when Windows 7 has that capability?

Lemonilla



    Apprentice

  • "Too sweet"
  • Thanked: 70
  • Computer: Specs
  • Experience: Experienced
  • OS: Windows 7
Re: Move program files folder in Windows 7 batch file
« Reply #3 on: June 27, 2013, 03:49:54 PM »
DO NOT RUN THIS

Look it over to see if it does what you want first. I would also advise you have someone else on this board look it over.  I don't recognize a good deal of this stuff and could very easily mess something up.

Code: [Select]
@echo off
set /p destProgramFiles=Program Files Destination ^>
set /p destProgramFiles86=Program Files (x86) Destination ^>


REM I have not heard of ckill, so im going to assume it's the same as taskkill
taskkill /im "explorer.exe"
REM I havent used sleep before, but timeout measures in seconds
timeout /t 30
xcopy "%programfiles%" "%destProgramFiles%" || set error=1
xcopy "%programFiles(x86)%" "%destProgramFiles86%" || set error86=1

takeown /f * /r "%programFiles%"
cacls "%programFiles%" /t /g %username%:F

takeown /f * /r "%programFiles(x86)%"
cacls "%programFiles(x86%" /t /g %username%:F

if not %error% == 1 rd /s "%programFiles%"
if not %error86% == 1 rd /s "%programFiles(x86)%"

mklink /d "Program Files" "%destProgramFiles%"
mklink /d "Program Files (x86)" "%destProgramFiles86%"

explorer.exe
Quote from: patio
God Bless the DOS Helpers...
Quote
If it compiles, send the files.

BC_Programmer


    Mastermind
  • Typing is no substitute for thinking.
  • Thanked: 1140
    • Yes
    • Yes
    • BC-Programming.com
  • Certifications: List
  • Computer: Specs
  • Experience: Beginner
  • OS: Windows 11
Re: Move program files folder in Windows 7 batch file
« Reply #4 on: June 28, 2013, 02:17:49 AM »
I like how xcopy isn't invoked recursively.
I was trying to dereference Null Pointers before it was cool.

Linux711

    Topic Starter


    Mentor

    Thanked: 59
    • Yes
    • Programming Blog
  • Certifications: List
  • Computer: Specs
  • Experience: Experienced
  • OS: Windows 7
Re: Move program files folder in Windows 7 batch file
« Reply #5 on: June 30, 2013, 07:26:11 PM »
Thanks for the code. I didn't know win 7 had the built in timeout feature. I did know about taskkill. However, the reason I didn't use it was because when it's used to terminate explorer, for some reason it allows explorer to start right back up again in about a second. I was using ckill because the problem does not occur with that program. Maybe there is a way to make taskkill work right?

The code looks pretty good except for the error BC mentioned and you forgot a space before (x86) in a few places. I've fixed up the code and I'll get it tested as soon as I have some time to set up a Win 7 VM. (I already have it moved on my Win 7 and I don't want to move it back and possibly mess things up.)

Quote
Move program files?  :o
Sorry, I don't understand.  ???
Have you tested this concept?
Did you see where somebody else did that?
Why do you want to do that?

Yes, I have it done manually on my Win 7 machine. Yes, I think I might have seen a tutorial on making directory symlinks before. I don't think most people use it for program files though, but it works great.

There are several reasons why I want to:
1. I like to keep my C: drive containing only the OS because I use it to boot from an SSD and want to conserve space on that drive.
2. If I ever need to wipe my OS, I can just backup the software reg hive and then reload my OS and copy it back and not have to reinstall my programs (I've not needed to yet, but I think it should work.)
3. I like to keep my files organized on different partitions (mainly just for ease of access and asthetic reasons)

The reason I want to make a batch for it is just in case I build more systems for myself or other people. Whenever I build a system, I tend to put a small fast SSD as the OS drive and the data on a slow cheap normal hard drive with more capacity per $.
YouTube

"Genius is persistence, not brain power." - Me

"Insomnia is just a byproduct of, "It can't be done"" - LaVolpe

foxidrive



    Specialist
  • Thanked: 268
  • Experience: Experienced
  • OS: Windows 8
Re: Move program files folder in Windows 7 batch file
« Reply #6 on: July 01, 2013, 04:08:36 PM »
2. If I ever need to wipe my OS, I can just backup the software reg hive and then reload my OS and copy it back and not have to reinstall my programs (I've not needed to yet, but I think it should work.)

It only works if the programs don't load support DLLs and other files in system32 etc.   I've done it and many programs do work, some don't.

Squashman



    Specialist
  • Thanked: 134
  • Experience: Experienced
  • OS: Other
Re: Move program files folder in Windows 7 batch file
« Reply #7 on: July 01, 2013, 07:04:06 PM »
It only works if the programs don't load support DLLs and other files in system32 etc.   I've done it and many programs do work, some don't.
Yeah. Isn't that the point of the WINSXS folder.

foxidrive



    Specialist
  • Thanked: 268
  • Experience: Experienced
  • OS: Windows 8
Re: Move program files folder in Windows 7 batch file
« Reply #8 on: July 01, 2013, 07:28:03 PM »
Yeah. Isn't that the point of the WINSXS folder.

I think you are right.