open QGIS project in batch file - batch-file

I create portable QGIS. It is done by copy QGIS2 folder in other computer. I have startup file so that QGIS will do the command. I can run QGIS in batch file. The code :
echo D | xcopy /s/e/y "%~dp0DATA\.qgis2" C:\Users\%username%\.qgis2
start /d "Running QGIS2" /D "%~dp0DATA\QGIS2\bin" qgis.bat
Then I want to open 1.qgs project. Then I add the code :
start /d "Running QGIS2" /D "%~dp0DATA\QGIS2\bin" qgis.bat "%~dp0DATA\PROJECT" 1.qgs
But, there is a message. Unable to open D:/./././1.qgs. So, what's wrong in my code?

Since you are starting the process, there is no guarantee that the directory that qgis sees as "current" is the same as the batch directory.
Fully-qualify the parameter 1.qgs - prefix it with the the appropriate directoryname.
Alternatively, modify qgis.bat to display %cd% and pause. That will show you where qgis believes its current directory is. That information should allow you to make appropriate adjustments.

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.

Batch script does not run in temporary folder after decompressing SFX WinRAR file

I have a program with a separate setup for 32 and 64 bits. My goal is to create a single executable that can run the appropriate setup. So I created a folder and placed the two setups inside, then wrote the following script:
#echo off
if %PROCESSOR_ARCHITECTURE%==AMD64 goto :x64
if %PROCESSOR_ARCHITEW6432%==AMD64 goto :x64
:x86
"%cd%"\setup.exe
exit
:x64
"%cd%"\setup-x64.exe
exit
Afterwards, I created the SFX file with this folder in WinRAR, pointing to the BAT file. But when I run it, a command line window pops up and shuts down instantly and nothing happens. I go to the temporary folder and double click the BAT file and the setup starts. The same happens in the original folder. What is happening and how can I fix it? Thanks!
%cd% reffers to the directory of the call of the batch-file.
For example a batch-file is in %USERPROFILE%\Desktop\Folder\bat.bat:
echo %cd%
pause
and you start it for example from the command-line like this:
C:\>%USERPROFILE%\Desktop\Folder\bat.bat
it should echo C:\ as that is where you called it from.
Two ways from the comments to solve the problem:
Push the directory of the batch-file using pushd "%~dp0" -> will result in a change of the variable value of %cd%
or
do not use "%cd%" but "%~dp0"
Both ways use the fact that the zeroth argument of a batch-file is its path.
You can prevent the command-line window from closing if you are debugging the file from the command-prompt itself if possible. With that you should have seen an error that state something like ...\setup.exe not found. After that nothing had to be done from the batch-file so it closed.

Why does 'Run as administrator' change (sometimes) batch file's current directory?

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 :)

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