Executing a regfile from within .bat - batch-file

I have the following code in the batch-file:
#echo on
regedit.exe /S abc.reg
If I run it, I get an error.
I searched the google for this and found nothing. Can someone help me please?
I tried:
- removing /S (the I can read (of course) the errormessage
- Run the file as administrator (I'm admin per default)
I am using windows 8.
The erros message: "Cannot import C:\users....abc.reg". Error opening the file. There may be a disk or a file system error.
Both Files are in the same folder.

The problem was, that I handled the %~dp0 in a false way. %~dp0 stands for the default path, but you don't have to set a backslash after it!
If you want to run a .reg file in the same folder as the .bat file, just write it like this:
#echo on
REGEDIT /S "%~dp0ABC.reg"
This will run the file ABC.reg in the same folder.
If you wan't to run something in a subfolder, you have to do it like this:
#echo on
REGEDIT /S "%~dp0SUBFOLDER\ABC.reg"
The brackets ("") are only needed, if there are whitespaces in the pathname.

Much easier way is run it as a Scheduled task.
In this way you can get your batch file to run as admin without annoying password prompts.
For more valuable information, please read here...
http://forums.whirlpool.net.au/archive/2037875
http://social.technet.microsoft.com/Forums/systemcenter/en-US/be854b0a-5d41-4b03-9a84-8fe79732f33c/run-bat-file-to-call-reg-file-in-sccm?forum=configmgrswdist
http://www.myitforum.com/articles/1/view.asp?id=12036

Right click on aix2_mm.bat and select "Run As Administrator", you will get a full screen UAC prompt that you must agree with, then it will work.
If you don't see 'Run As Administrator' as an option, hold the shift key prior to right clicking.
and it wouldn't hurt to make your batch more portable. The %~dp0 parameter will expand to the full path to ABC.reg if it's in the same folder as the .bat file.
#echo on
regedit.exe /S "%~dp0abc.reg"

Be careful the working directory
My bat file always has the code on first line
cd /d %~dp0
It can safely change current directory to your bat file directory

Related

How to get the current path of my batch file? [duplicate]

I have a batch file that I intend to distribute to our customers to run a software task.
We distribute them as a folder or .zip with the files inside. Inside, there is the batch files and another folder with the files needed to run the batch.
Normally, when you make a batch, you type the path where the files are. But I won't know where the files are. The files will still be kept inside the master folder, but I need to have the batch find that folder to run the files.
So for example: If they have the master folder on the desktop and they run it, it would need to be something like "C:\Users\Username\Desktop" to run. You would have the batch CD to that location.
But what if they run it from documents? I don't know the username, so I have to somehow have the batch find this. Any code and/or instructions would be great.
There is no need to know where the files are, because when you launch a bat file the working directory is the directory where it was launched (the "master folder"), so if you have this structure:
.\mydocuments\folder\mybat.bat
.\mydocuments\folder\subfolder\file.txt
And the user starts the "mybat.bat", the working directory is ".\mydocuments\folder", so you only need to write the subfolder name in your script:
#Echo OFF
REM Do anything with ".\Subfolder\File1.txt"
PUSHD ".\Subfolder"
Type "File1.txt"
Pause&Exit
Anyway, the working directory is stored in the "%CD%" variable, and the directory where the bat was launched is stored on the argument 0. Then if you want to know the working directory on any computer you can do:
#Echo OFF
Echo Launch dir: "%~dp0"
Echo Current dir: "%CD%"
Pause&Exit
ElektroStudios answer is a bit misleading.
"when you launch a bat file the working dir is the dir where it was launched"
This is true if the user clicks on the batch file in the explorer.
However, if the script is called from another script using the CALL command, the current working directory does not change.
Thus, inside your script, it is better to use %~dp0subfolder\file1.txt
Please also note that %~dp0 will end with a backslash when the current script is not in the current working directory.
Thus, if you need the directory name without a trailing backslash, you could use something like
call :GET_THIS_DIR
echo I am here: %THIS_DIR%
goto :EOF
:GET_THIS_DIR
pushd %~dp0
set THIS_DIR=%CD%
popd
goto :EOF
You can also do
Pushd "%~dp0"
Which also takes running from a unc path into consideration.
Try in yourbatch
set "batchisin=%~dp0"
which should set the variable to your batch's location.

Start a batch file in a certain location

I want to start a batch file in a certain location. I tried start /d C:\Windows C:\Windows\branding\readWin..bat The batch file which launches this in on my desktop. Any way to do this?
Thanks.
I'm not sure exactly what you mean. You shouldn't need to change the current/start-in directory before invoking a script. If that script needs to define its working directory it should do so within.
If that's the case then just enter the full or relative batch file name:
"C:\Windows\branding\readWin..bat"
If the batch file you're wanting to invoke does not define its own current directory and you feel it's necessary then you could be sure by defining it yourself first:
CD /D "C:\Windows"
Or:
PushD "C:\Windows"
After that just run your batch file using its full or relative path as previously mentioned.
Are you running this from command prompt?
start /d "path" file.bat
The quotes around the file path are important, and just the file name follows the path.

Create a batch file to run an .exe with an additional parameter

I need a batch file which will do the following:
1. Open CMD and navigate to a location C:/Users/...../program.exe
2. Run the program.exe with an additional command to point it to a config file:
e.g. "program.exe C:/Users/..../configFile.bgi"
How can I do this?
I tried this but with no luck:
start "C:\Users\Ben\Desktop\BGInfo\bginfo.exe C:\Users\Ben\Desktop\BGInfo\dc_bginfo.bgi"
pause
Update
I've used the solution provided by Ganesh (below) and came up with this:
cd C:\Users\Ben\Desktop\BGInfo\
bginfo.exe C:\Users\Ben\Desktop\BGInfo\dc_bginfo.bgi
I've tested it on a local machine (changing the directories) but on the server (with the directory above) it does not work...
The folder directory with batch file:
The error
in batch file abc.bat
cd c:\user\ben_dchost\documents\
executible.exe -flag1 -flag2 -flag3
I am assuming that your executible.exe is present in c:\user\ben_dchost\documents\
I am also assuming that the parameters it takes are -flag1 -flag2 -flag3
Edited:
For the command you say you want to execute, do:
cd C:\Users\Ben\Desktop\BGInfo\
bginfo.exe dc_bginfo.bgi
pause
Hope this helps
You can use
start "" "%USERPROFILE%\Desktop\BGInfo\bginfo.exe" "%USERPROFILE%\Desktop\BGInfo\dc_bginfo.bgi"
or
start "" /D "%USERPROFILE%\Desktop\BGInfo" bginfo.exe dc_bginfo.bgi
or
"%USERPROFILE%\Desktop\BGInfo\bginfo.exe" "%USERPROFILE%\Desktop\BGInfo\dc_bginfo.bgi"
or
cd /D "%USERPROFILE%\Desktop\BGInfo"
bginfo.exe dc_bginfo.bgi
Help on commands start and cd is output by executing in a command prompt window help start or start /? and help cd or cd /?.
But I do not understand why you need a batch file at all for starting the application with the additional parameter. Create a shortcut (*.lnk) on your desktop for this application. Then right click on the shortcut, left click on Properties and append after a space character "%USERPROFILE%\Desktop\BGInfo\dc_bginfo.bgi" as parameter.
Found another solution for the same. It will be more helpful.
START C:\"Program Files (x86)"\Test\"Test Automation"\finger.exe ConfigFile="C:\Users\PCName\Desktop\Automation\Documents\Validation_ZoneWise_Default.finger.Config"
finger.exe is a parent program that is calling config solution.
Note: if your path folder name consists of spaces, then do not forget to add "".

how to copy file to system folder in batch

i want to copy a file to system32 folder using a batch file. but when i try to do this it says "Access denied" i also can not copy anything to my C:\ drive also. i tried using xcopy and copy to do this. i don't know what is the parameter to get the administrator permission. so, can anyone help me?
program:
#echo off
xcopy abc.dll "C:\Windows\System32\" -y
pause
output:
D:abc.dll
Access denied
Press any key to continue . . .
Should be doable one of two ways.
The first is to make a shortcut to your batch file, right click -> properties and on the Shortcut tab of the properties, click the Advanced button to find a checkable "run as admin" option.
The second (NOT TESTED) is to add a bunch of stuff to the top of your batch file (Reference) - I don't want to include it in the answer because I have not tested it.
#echo off
copy /Y abc.dll "C:\Windows\System32\"
pause
right click the batch file and run as administrator
and make sure the abc.dll is in the same folder as your batch file.
hope this helps.

batch file to copy files to another location?

Is it possible to create a batch file to copy a folder to another location everytime I login, or when the folder is updated?
It could be written in VB or Java as well if not an easy solution.
Any ideas?
Two approaches:
When you login: you can to create a copy_my_files.bat file into your All Programs > Startup folder with this content (its a plain text document):
xcopy c:\folder\*.* d:\another_folder\.
Use xcopy c:\folder\*.* d:\another_folder\. /Y to overwrite the file without any prompt.
Everytime a folder changes: if you can to use C#, you can to create a program using FileSystemWatcher
#echo off
copy con d:\*.*
xcopy d:\*.* e:\*.*
pause
Open Notepad.
Type the following lines into it (obviously replace the folders with your ones)
#echo off
rem you could also remove the line above, because it might help you to see what happens
rem /i option is needed to avoid the batch file asking you whether destination folder is a file or a folder
rem /e option is needed to copy also all folders and subfolders
xcopy "c:\New Folder" "c:\Copy of New Folder" /i /e
Save the file as backup.bat (not .txt)
Double click on the file to run it. It will backup the folder and all its contents files/subfolders.
Now if you want the batch file to be run everytime you login in Windows, you should place it in Windows Startup menu. You find it under: Start > All Program > Startup
To place the batch file in there either drag it into the Startup menu or RIGH click on the Windows START button and select Explore, go in Programs > Startup, and copy the batch file into there.
To run the batch file everytime the folder is updated you need an application, it can not be done with just a batch file.
It's easy to copy a folder in a batch file.
#echo off
set src_folder = c:\whatever\*.*
set dst_folder = c:\foo
xcopy /S/E/U %src_folder% %dst_folder%
And you can add that batch file to your Windows login script pretty easily (assuming you have admin rights on the machine). Just go to the "User Manager" control panel, choose properties for your user, choose profile and set a logon script.
How you get to the user manager control panel depends on which version of Windows you run. But right clicking on My Computer and choosing manage and then choosing Local users and groups works for most versions.
The only sticky bit is "when the folder is updated". This sounds like a folder watcher, which you can't do in a batch file, but you can do pretty easily with .NET.
Batch file to copy folder is easy.
xcopy /Y C:\Source\*.* C:\NewFolder
Save the above as a batch file, and get Windows to run it on start up.
To do the same thing when folder is updated is trickier, you'll need a program that monitors the folder every x time and check for changes. You can write the program in VB/Java/whatever then schedule it to run every 30mins.
robocopy yourfolder yourdestination /MON:0
should do it, although you may need some more options. The switch at the end will re-run robocopy if more than 0 changes are seen.
#echo off
cls
echo press any key to continue backup !
pause
xcopy c:\users\file*.* e:\backup*.* /s /e
echo backup complete
pause
file = name of file your wanting to copy
backup = where u want the file to be moved to
Hope this helps
#echo off
xcopy ...
Replace ... with the appropriate xcopy arguments to copy what you want copied.

Resources