1

Basically I am trying to create an installer. I have an EXE file and I need to do the following steps through a bat file:

  1. create a folder at C:\Data
  2. Copy a file in C.\Data
  3. Create a folder inside C:\Program Files
  4. Copy exe file in C:\Program Files\My Project Folder
  5. Create shortcut to exe on Desktop

My code is as follows:

@echo off
if not exist "%PROGRAMFILES%\MyFolder" mkdir %PROGRAMFILES%\MyFolder
if not exist "C:\Data" mkdir C:\Data
copy /q /y ".\MyFile.exe" "%PROGRAMFILES%\MyFolder\MyFile.exe"
copy /q /y ".\MyFileDb.db" "C:\Data\MyFileDb.db"

The problem is that it shows "invalid path" error and says 0 files copied for MyFolder. However, it successfully creates Data folder and copies MyFileDb.db inside it.

Second problem is that I cannot figure out how to execute the Step 5 of my problem statement.

Compo
  • 36,585
  • 5
  • 27
  • 39
ITSagar
  • 673
  • 2
  • 10
  • 29
  • 2
    Look at where you put quotes, then look at where you didn't put quotes. Remember that if a path has spaces, then you _have_ to surround it with quotes. – SomethingDark Nov 03 '18 at 11:50
  • 1
    Are you running the script As Administrator? _The `Program Files` directory is protected from normal users!_ As a side note, there are things which I would consider wrong with lines `2`, `3` ,`4` and `5`. – Compo Nov 03 '18 at 11:54
  • `If Not Exist "%ProgramFiles%\MyFolder\" MD "%ProgramFiles%\MyFolder"`, `If Not Exist "C:\Data\" MD "C:\Data"`, `Copy /Y "MyFile.exe" "%ProgramFiles%\MyFolder"`, `Copy /Y "MyFileDb.db" "C:\Data"`. However you could use `XCopy` which, if used correctly, would create non existing directories as part of its copying process, _thus removing two of those four lines completely_. – Compo Nov 03 '18 at 12:03
  • As previously mentioned, in order for you to copy a file to the Program Files directory, the script needs to run with elevated privileges. When that happens, the current working directory becomes the SYSTEM32 directory so your executable and database file are no longer in the current working directory. Assuming the batch file is in the same directory as the two files you need to copy, you can do a change directory back to that folder or you can also use the PUSHD command. `CD /D %~dp0` or `pushd %~dp0`. – Squashman Nov 03 '18 at 12:03
  • ...or for an extra degree of safety you may be able to use `Copy /Y "%~dp0MyFile.exe" "%ProgramFiles%\MyFolder"`, `Copy /Y "%~dp0MyFileDb.db" "C:\Data"`, which will pick up the files from the running batch file's own directory. – Compo Nov 03 '18 at 12:12
  • As for creating the shortcut, you should research and attempt it first before asking the question. This site is designed to help you to fix your code, not to provide it for you. Also, as you should be aware, there is more than one Desktop location on Windows, _and depending upon the Windows version and method used, may be accessed differently_; which one do you want to create a shortcut on? – Compo Nov 03 '18 at 12:26

1 Answers1

0
  1. MSI: I would use a proper MSI deployment tool. Here are some of the major ones.

  2. Non-MSI: You can also use non-MSI deployment technologies such as Inno Setup or NSIS.

  3. Self-Extraction Tools: Some recommend self-extracting archives.


Some Further Links:

Stein Åsmul
  • 39,960
  • 25
  • 91
  • 164