Copying files as administrator in a batch file - windows

So I have a batch file that copies files from one folder to a bunch of different folders.
The purpose of this batch file is to be like a simple installer(Without registry edits) to make it easier for others to use a set of files I am distributing. Because this is being widely distributed, it will need to work on windows operatings systems from XP on. The files need to be moved to the program files of a program (This works), their documents folder(Also works), and the program folder in the all users start menu(Does not work).
So far I have been unsuccessful with xcopy and robocopy as they both tell me "access denied". Also running the batch file with "Run as administrator". If you can think of a more effective way to do what I'm trying to do besides a batch file I am open to ideas.
xcopy /q /y ".\add_ui.bat" "%PROGRAMDATA%\Microsoft\Windows\Start Menu\Programs\My Program\Add.bat"
I'm sure there's some simple trick that I'm missing.
Thanks in advance!

You must have local admin rights to write to that folder and your batch file will need to be ran with elevated permissions (Run As Adminstrator).

Related

How to add batch script to context menu of multiple computers

I've been trying to setup a batch script that can be run from the context menu inside a folder. The purpose of the batch script is to populate the folder with a predetermined folder structure.
This will need to be replicated on multiple computers(Windows 7-10), so my idea was to have a network share with two batch files and one .reg file. One of the batch files labeled "install.cmd" would copy the other batch file labeled "Subfolders.cmd" to a folder on the C drive, and run the .reg file to install a shortcut on the context menu to the "Subfolders.cmd" batch file.
I have created the "Subfolders.cmd" batch file and it works, but it has to be run from inside the folder I want the folder structure setup in. I would appreciate any help on how to create the "install.cmd" batch script that would create a folder on the C drive, copy the "Subfolders.cmd" script into it, and run the .reg file to create the item in the context menu that would allow the "Subfolders.cmd" batch script to be run in selected folder.
I'd appreciate any examples and/or suggestions of more efficient ways of doing this. Thanks!
Update: As requested, I have posted the code that generates the subfolders inside an opened folder. It's pretty simple.
md "Folder1" "Folder1/Sub1A" "Folder1/Sub1B" "Folder2" "Folder2/Sub2A" "Folder2/Sub2B"
There is no need for all this copying.
"\\computername\sharename\folder\file.bat"
Will run a batch file stored on another computer.
Ditto the reg command
regedit /s "\\computername\sharename\folder\file.reg"
You can do the above with the older mapped drives as well as UNC.
You need to show your second script so we can see why it NEEDS to be in the folder.
EDIT
You need to specify full paths.
To get the starting folder use %V in the registry command. So (and lets get rid of the reg file) (add your bat instead of echo)
reg add "HKCR\Directory\Background\Shell\Test Command\command" /ve /t REG_EXPAND_SZ /d "cmd /k echo ""%V"""
In your batch use %1 to get the starting folder, and tilde (%~1) to remove quotes (see call /? for documentation). (remember create folders from the lowest level as higer levels get made automatically).
md "%~1\folder1\folder2"

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

How to run robocopy as non admin without having to share folders for read/write access - WINDOWS 7

I have a basic bat file which copies files from one directory to another
set SOURCE=\\T4\D$\TV
set DESTINATION=\\T4\E$\TV
robocopy /MIR %SOURCE% %DESTINATION%
When i run as a user (member of the admin group) it just displays the contents of the bat file in a command prompt window on an endless loop.
However when i right click over the bat file and click run as administrator it executes perfectly.
When i right click over the bat file and click properties> compatibility, the option to run as administrator is greyed out for some reason?
I dont really want to share my folders either :)
Any help?
Since you are using the Administrative shares in your for your SOURCE and DESTINATION locations, you would require Admin privileges. Not just for robocopy, but for ANY copy command.
Try going to either location from the RUN command to verify.

A script to automatically delete folders in another folder

I've came across several posts and even code online about .batch files to delete folders and files. However, I am still pretty confused by the myriad amount of information that's on the Internet. Is there a specific way of writing a script that automatically deletes folders that are older than 30 days (the script has to be smart enough to run itself and not be done manually)
I appreciate your help and I am willing to learn here. Thanks!
P.S. I've attached an example of the kind of folders that I would like to delete. As you can see these folders date back to 2008
This will remove the entire directory tree, so use it with coution and remove the echo, if the output is OK:
forfiles /d -30 /c "cmd /c if #isdir==TRUE echo rd /s /q #fname"
Scripts can not run them selves automatically, but the os provides facilities to do that using scheduled tasks. So you need to create yourself a batch file to delete what you need and then add a recurring scheduled task to do the work.
There is no built-in batch command to work only on files with a certain age. But you can download the forfiles tool from Microsoft. It allows to execute commands on files of a certain age only (on the command line).

Resources