Copy a .bat off a USB and copy it to the Startup folder script - WinXP - windows

I'm a Mac/iPhone dev so I don't know very much about Windows scripting...
The point is I have to install a startup app on many computers, so I'd like to have a USB stick with two .bat files:
would be the actual "app"
would be the script that would copy the 1st.bat off my USB to the Windows startup folder...
How can I do that?
the name of my usb is "USB" and the name of my startup app is "startup.bat".
How I already said, I'm extremely lame in Windows programing, and I need it acutely ;)
Thanks A LOT!

Try the following script. This will cause the application to run whenever the current user logs in. Without administrative privilages, you won't be able to do it for all users in one go.
#Echo Off
CD /D %~dp0
Set StartupFolder=%AppData%\Microsoft\Windows\Start Menu\Programs\Startup
If Exist "%StartupFolder%" Goto :FoundStartup
Set StartupFolder=%UserProfile%\Start Menu\Programs\Startup
If Exist "%StartupFolder%" Goto :FoundStartup
Echo Cannot find Startup folder.
Exit /B
:FoundStartup
Copy "MyApp" "%StartupFolder%"
Each line does the following:
Turn off command echoing, making the script look cleaner to the end user.
Set the current directory to wherever this script is located.
Set the Startup folder's path as expected in Windows Vista or later.
If this folder exists, jump to the copying stage.
Set the Startup folder's path as expected in Windows 2000 or later.
If this folder exists, jump to the copying stage.
Report that the Startup folder can't be found.
Exit the batch script.
A label that can be jumped to.
Copy "MyApp" from the current folder (USB) to the Startup folder.

I don't want to take credit for Hand-E-Food's answer, but I figured out why his code didn't work and I can't reply to his answer, so here it is. Instead of using quotes around the %StartupFolder% variable in the Copy line, use them around the path for Set StartupFolder. Therefore, the code would be as follows.
#Echo Off
CD /D %~dp0
Set StartupFolder="%AppData%\Microsoft\Windows\Start Menu\Programs\Startup"
If Exist %StartupFolder% Goto :FoundStartup
Set StartupFolder="%UserProfile%\Start Menu\Programs\Startup"
If Exist %StartupFolder% Goto :FoundStartup
Echo Cannot find Startup folder.
Exit /B
:FoundStartup
Copy "MyApp" %StartupFolder%
The only reason I figured this out is because it wasn't doing anything for me either, so I tried removing the quotes around %StartupFolder% and that resulted in an error message that it couldn't find the folder, but at least I knew it was doing something at the end. Once I figured out that it was looking for the wrong folder because it thought the folder name stopped at the first space in its name, I simply added in the quotes and voila!

Try this (replace app.bat with whatever your actual app is called). This should work on Windows 2000 and up.
IF EXIST "C:\ProgramData\Microsoft\Windows\Start Menu\Programs\*.*" COPY APP.BAT "C:\ProgramData\Microsoft\Windows\Start Menu\Programs\"
IF EXIST "C:\Documents and Settings\All Users\Start Menu\Programs\Startup\*.*" COPY APP.BAT "C:\Documents and Settings\All Users\Start Menu\Programs\Startup\"

Related

Batch | Why can't a script running in admin access other scripts? [duplicate]

I have a batch file that is in the same directory as the file I want to xcopy. But for some reason the file is not being found.
I thought that current directory was always where the batch file was located.
I run batch file as administrator. This occurs on a Windows 7 64-bit desktop computer.
Batch file:
#ECHO OFF
XCOPY /y "File1.txt" "File2.txt"
PAUSE
Error:
File not found - File1.txt
0 File(s) copied
Which directory is current working directory on starting a batch file with context menu item Run as administrator depends on User Account Control (UAC) setting for the current user.
This can be demonstrated with following small batch file C:\Temp\Test.bat:
#echo Current directory is: %CD%
#pause
With having selected in User Account Control Settings
Default - Notify me only when programs try to make changes to my computer
Don't notify me when I make changes to Windows settings
and using Run as administrator, Windows uses registry key
HKEY_CLASSES_ROOT\batfile\shell\runasuser\command
This registry key does not contain a default string for executing the batch file. Instead there is the string value DelegateExecute with the CLSID {ea72d00e-4960-42fa-ba92-7792a7944c1d}.
The result is opening a dialog window with title User Account Control and text:
Do you want to allow the following program to make changes to this computer?
Program name: Windows Command Processor
Verified publisher: Microsoft Windows
After confirmation by the user, Windows opens temporarily a new user session like when using on command line RunAs.
In this new user session the current working directory is %SystemRoot%\System32 on executing now the command defined in Windows registry with default string of key
HKEY_CLASSES_ROOT\batfile\shell\runas\command
which is:
%SystemRoot%\System32\cmd.exe /C "%1" %*
Therefore a console window is opened with title C:\Windows\System32\cmd.exe and the 2 lines:
Current directory is: C:\Windows\System32
Press any key to continue . . .
After hitting any key, batch execution finishes which results in closing cmd.exe which results in closing the user session.
But with having selected in User Account Control Settings
Never notify me when
Programs try to install software or make changes to my computer
I make changes to Windows settings
the behavior is different as the user has already elevated privileges.
Now Windows uses directly the command
%SystemRoot%\System32\cmd.exe /C "%1" %*
according to default string of key
HKEY_CLASSES_ROOT\batfile\shell\runas\command
in current user session.
The result is opening a console window also with title C:\Windows\System32\cmd.exe, but displayed in window is:
Current directory is: C:\Temp
Press any key to continue . . .
The current working directory of the parent process (Windows Explorer as desktop) is used for executing of the batch file because no switch to a different user session was necessary in this case.
PA has posted already 2 possible solutions in his answer which I replicate here with a small improvement (pushd with directory in double quotes) and with adding a third one.
Change current directory to directory of batch file using pushd and popd:
pushd "%~dp0"
%SystemRoot%\System32\xcopy.exe "File1.txt" "File2.txt" /Y
popd
This works also for UNC paths. Run in a command prompt window pushd /? for an explanation why this also works for UNC paths.
Use directory of batch file in source and destination specifications:
%SystemRoot%\System32\xcopy.exe "%~dp0File1.txt" "%~dp0File2.txt" /Y
Change working directory to directory of batch file using cd:
cd /D "%~dp0"
%SystemRoot%\System32\xcopy.exe "File1.txt" "File2.txt" /Y
This does not work for UNC paths because command interpreter cmd does not support a UNC path as current directory by default, see for example CMD does not support UNC paths as current directories for details.
The error message is very self explanatory. The file file1.txt is not found.
Because the file name does not include an absolute path, the system tries to find it on the current directory. Your current directory does not contain this file.
Your misconception is that the current directory is not the directory that contains the bat file. Those are two unrelated concepts.
You can easily check by adding this two commands in your bat file
echo BAT directory is %~dp0
echo Current directory is %CD%
you can notice they are different, and that there is a subtle difference in the way the last backslash is appended or not.
So, there are esentially two ways to cope with this problem
either change the current directory to match the expected one
pushd %~dp0
XCOPY /y "File1.txt" "File2.txt"
popd
or specify the full path in the command
XCOPY /y "%~dp0File1.txt" "%~dp0File2.txt"
For the sake of completeness and obscurity, I add another workaround, confirmed as working under Windows 8.1 and expected to work elsewhere, as it relies on documented functionality:
You can change the runas command definition keys
HKEY_CLASSES_ROOT\batfile\shell\runas\command and
HKEY_CLASSES_ROOT\cmdfile\shell\runas\command into
%SystemRoot%\System32\cmd.exe /S /C "(for %%G in (%1) do cd /D "%%~dpG") & "%1"" %*
Which results in the bat or cmd file starting in its containing directory when started using the runas verb, respectively the "Run as Administrator" menu entry.
What the additions to the original command exactly do:
cmd /S strips away first and last (double) quote in command string after /C
for %%G in (%1) do enumerates its single entry, the %1 argument,
making it available for expansion as %%G in the loop body; the letter is arbitrary but some may be "reserved"
%%~dpG expands to the drive and path of %%G, the ~ tilde stripping away quotes if present, which is why we add them back explicitly
cd /D changes both the drive and directory to its argument, and finally
& runs the second command "%1" %* regardless of success of the first one.
You can use pushd which will even support UNC paths, but a stray popd would land any script in the system32 directory, not a behavior I would be fond of.
It should be possible to do this for the exefile entry as well, but frankly, I'd rather live with the inconsistency than to attempt this on my system, as any error there could break a lot.
Enjoy defeating the security mechanics of your operating system :)

How to run a .bat (that runs an .exe that is in the same directory) as administrator from a pendrive?

I'm able to run a .bat (that runs an .exe that is in the same directory) as administrator: I right-click in the bat file and select "Run as administrator".
To be able to do that, I used the following answer: Run exe from current directory in batch
Here's the code:
#echo off
:A
cls
echo This will start the program.
pause
cd %~dp0
start %1myprogram.exe
exit
However, this will only work if the .bat file and the program are in the system drive.
Because if they are, for instance, in a pendrive, and I right-click and select "Run as Administrator", I get the error:
"Windows cannot find 'myprogram.exe'. Make sure you've typed the name correctly, then try again."
Why this happens and how can I fix it?
I thought that by using cd %~dp0 it would always point to the folder in which the bat .file resides.
Thanks in advance.
Solution
Change cd %~dp0 to cd /d %~dp0
Explanation
When you run something with administrator privileges, the working directory changes to:
'C:\Windows\System32'
Although %~dp0 still points to the drive and the directory containing the batch file, cd %~dp0 does not work, because it only changes the directory, but stays on the same drive.
Using the /d parameter, you can tell the cd-command to change the drive, too.
You may need to tell cd to also change drives:
cd /d %~dp0
If the current drive is C: (e.g., the prompt says C:\>), and you do CD D:\FOO, the current directory on drive D: is set to \FOO, but you will still be "on" drive C:. Try the following:
#echo off
:A
cls
echo This will start the program.
pause
cd %~dp0
%~d0
start %1myprogram.exe
exit
(also, why %1myprogram.exe instead of just myprogram.exe, or even just myprogram? If you're right-clicking on the batch file to run it, there isn't going to be a %1.)

the installation package could not be open batch file

I've been working on a batch file all day, that I can't get to work open through GPO (another day, another question). So I decided to do it manually with every computer. I have two exe's and one MSI. The exe's work perfectly fine. They get installed, and it all works out. The MSI, however, doesn't. It gives me the error: the installation package could not be opened. Verify that the package exists and that you can access it, or contact the application vendor to verify that this is a valid Windows Installer package.
Now when I go to the network share and use it from there, it works perfectly fine. So there must be an issue with my code.
Here's the code:
#echo off
IF NOT EXIST "C:\Program Files (x86)\Citrix\ICA Client\" (
pushd "\\KOPI-DC01\ACCURO Cloudwerx\ACCURO\1\"
.\CitrixReceiver-4.4.1000.exe /silent
)
IF NOT EXIST "C:\Program Files (x86)\triCerat\Simplify Printing\ScrewDrivers Client v4\" (
pushd "\\KOPI-DC01\ACCURO Cloudwerx\ACCURO\2\"
msiexec.exe /i ".\Screwdriver.msi"
)
IF NOT EXIST "C:\Program Files\Cloudwerx\CloudwerxPlugin\" (
pushd "\\KOPI-DC01\ACCURO Cloudwerx\ACCURO\3\"
.\cloudwerx-setup.exe /silent
)
pause
Any help would be greatly appreciated, thanks.
I am guessing that your problem is the distinction in powershell between the current location (set by the pushd command) and the working directory (unaffected by the pushd command). You can see the working directory of the powershell process using the [Environment]::CurrentDirectory property:
# C:\> [Environment]::CurrentDirectory = "c:\"
# C:\> [Environment]::CurrentDirectory
c:\
# C:\> pushd C:\Temp
# C:\Temp> [Environment]::CurrentDirectory
c:\
# C:\Temp> Get-Location
Path
----
C:\Temp
WHat is probably happening is that msiexec.exe is using the working directory (i.e. [Environment]::CurrentDirectory) and not the current powershell location at invocation. I would just specify the full path to msiexec:
msiexec.exe /i "\\KOPI-DC01\ACCURO Cloudwerx\ACCURO\2\\Screwdriver.msi"
MSI installation packages build with an older WIX utility would throw the error whenever installation was attempted from a batch script that was accessed on a shared drive using UNC path instead of a mapped drive letter. On the other hand whenever the batch file was executed with a mapped drive letter the installation would work normally.
I'm not blaming WIX here because I'm not certain whether they are responsible. I'm just describing symptoms here. It might just be the result of invoking plain vanilla Windows batch script that in turn executes msiexec with a bunch of command line parameters.

Batch file to start all programmes in the start folder of XP

I need start all folders in a the Windows "Start/Programs/Startup folder" of an XP machine, explorer is disabled to stop top people playing and remove the Start and Task-bar.
I can run a batch file at start-up but how do I write the batch to run ALL programs in the "Start/Programs/Startup folder" the programs in the folder may change but the batch needs to remain the same
I am able to open each file individually using the below code but I really need to be able to open everything in the folder to avoid problems in the future
start "" /b "C:\Documents and Settings\User\Start Menu\Programs\Startup\PROG.appref-ms"
I have tried the code below, that batch starts but nothing starts
%DIR%=C:\Documents and Settings\Pete\Start Menu\Programs\Startup
for %%a in (%DIR%\*) do "%%a"
Running the batch from the desktop also doesn't run the programs in the start folder, the DIR address is taken from windows explorer when I navigated to the folder with the short cuts in
This is an interesting request--one that I would question the motive behind, but since you asked, here's a way you could accomplish it:
#echo off
set DIR=C:\Your\Directory
for %%a in ("%DIR%\*") do "%%a"

A batch file that copies another into Start Up folder?

I am making a batch file that needs to copy another batch file into the Start Menu Start Up folder (the one used when a program launches on login/start up). Since the path uses the user's computer name eg. C:\Documents and Settings\User Name I need the batch file to get the user's correct name instead of the "User Name" or * (wildcard). Wildcards doesn't work as the batch file comes up with "the filename directory name or volume label syntax is incorrect".
I hope this is clear enough.
You can also try this:
cd %APPDATA%\Microsoft\Windows\Start Menu\Programs\Startup
It works in Windows 10. The %appdata% variable gives you your required username by default
Open a new command prompt window by executing cmd.exe or using the shortcut in Accessories in Windows start menu. Enter set and look on the list of environment variables predefined by Windows. You are mostly interested in USERPROFILE.
The following command can be used to copy a batch file with name AaRM.bat from a folder available for all users like the all users desktop folder to startup folder of the currently logged in user.
copy "%ALLUSERSPROFILE%\Desktop\AaRM.bat" "%USERPROFILE%\Start Menu\Programs\Startup"
The double quotes are important as the name of the batch file with path and the path to the startup folder both contain spaces.
Copying the batch file from your desktop folder to the startup folder of the other user is most likely not possible as the other user might have no permission to access anything in your user profile directory and below.
You can copy the batch file to distribute also to a different folder accessible for all users like "%ProgramFiles%" or %SystemRoot% as the batch file in all users desktop folder is visible for all user accounts on desktop.
Best would be to put the batch file into Windows directory (%SystemRoot% or %windir%) and create / copy a shortcut file (*.lnk) in / to startup folder of the other user accounts. The Windows start menu folders should contain only *.lnk files and not batch files and applications.
And last it would be also possible to create a shortcut in "%ALLUSERSPROFILE%\Start Menu\Programs\Startup" to the batch file in %windir% to execute this batch file for any user who logs in on this computer. Your batch file could contain at top something like if "%USERNAME%"=="your account name" goto :EOF 1 or more times with various user account names to prevent doing anything for 1 or more specific users.
try the following command if u r using win 7. never tried on win 8 though.
cd C:\Users\%username%\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup
works perfectly for me.
Stumped about nobody answering yet..
Why are we so lost?
echo %userprofile%
To know the name of current user
For copying
Copy /y %~f0 "%USERPROFILE%\%AppData%\Roaming\Microsoft\Windows\Start Menu\Programs\Startup
Alternative
copy "path of file you want to copy" "path of the directory where you want it copied"
copy path of batch file path of startup folder
Copy /y "File-Address-to-copy-here" "where-to-copy-to-here"
(as above)
then save file as .bat
and you have a batch file, run as admin if in system directories.
copy "C:\Documents and Settings\%username%\Desktop\batch.bat" "C:\Documents and Settings\%username%\Start Menu\Programs\Startup\"
This should work.
This works:
#echo off
copy "C:\Users\%username%\Desktop\somefolder\example.bat" "C:\Users\%username%\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup\"
Try this:
copy "CopyPath" "%appdata%\Microsoft\Windows\Start Menu\Programs\Startup"
Also, if you want to skip the username for the copy path, then use %username% instead of the actual username.
#echo off
copy "%~n0%~x0" "%USERPROFILE%\Start Menu\Programs\Startup"
*Use this code!
i know the answer,
type this in your batch file:
copy "copy file path" "paste file path"
if the file path has a username than type %ALLUSERSPROFILE% instead of the username (it will fill up automatically the username for every windows pc)
example: copy "C:\Users%username%\Desktop\New folder\hi.txt" "C:\Users%username%\Desktop"
that did this:
Example to what it did when opened
final answer:
copy "Path of file" "C:\Users%username%\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup"
that would work
This should work:
copy "anything.bat" "%appdata%\Microsoft\Windows\Start Menu\Programs\Startup"
this copies .txt, .exe, .bat, more into startup just change the .bat to anything else
I made this in Windows 10

Resources