WinRar -sp<> flag not working with self-extractors - winrar

I was using Winrar 3 to create self extractors and was using -sp flag to pass the arguments to the executable bundled inside. It was working fine.
After I updated WinRar to 5.1, it stopped working. -sp<> flag is no longer working for me.
Does anyone else face a similar issue?
Are there any other flags I can use to pass the parameters to the executable that is called by the self extractor.
I read the following documentation about the available flags.
http://www.winrar-tr.com/winrar/Help/ENG/html/HELPGUISFXCmd.htm

The parameters for the executable can be specified directly in the comment file with the parameters for the SFX module.
Here is an example batch file demonstrating this technique:
#echo off
cd /D "%TEMP%"
rem Create the file for the SFX module with the SFX options.
echo ;The comment below contains SFX script commands.>TestSetup.txt
echo.>>TestSetup.txt
echo Setup=Test.bat Switch "One more parameter">>TestSetup.txt
echo Overwrite=1>>TestSetup.txt
echo Title=Test Installation>>TestSetup.txt
echo Text>>TestSetup.txt
echo {>>TestSetup.txt
echo ^<font face='Arial'^>An SFX test which just shows how SFX module runs the installer.^<br^>^<br^>Just click on button Install or hit RETURN.^</font^>>>TestSetup.txt
echo }>>TestSetup.txt
rem Create the batch file executed by SFX archive.
echo #echo %%0 %%*>Test.bat
echo #pause>>Test.bat
echo #del %%0 ^>nul>>Test.bat
rem Create the SFX archive.
RAR.exe a -sfx -c -zTestSetup.txt TestSetup.exe Test.bat
rem Delete the created batch and comment file.
del Test.bat
del TestSetup.txt
rem Run the self-extracting archive. User has to press only RETURN.
start /wait TestSetup.exe
rem Delete the self-extracting archive.
:DeleteLoop
del TestSetup.exe >nul
if exist TestSetup.exe goto DeleteLoop
This batch file first creates in directory for temporary files the text file TestSetup.txt with the content:
;The comment below contains SFX script commands.
Setup=Test.bat Switch "One more parameter"
Overwrite=
Title=Test Installation
Text
{
<font face='Arial'>An SFX test which just shows how SFX module runs the installer.<br><br>Just click on button Install or hit RETURN.</font>
}
Important for you is the line starting with Setup=.
Test.bat is the file to execute after extraction.
Switch is an option to pass as first parameter to Test.bat.
"One more parameter" is a second parameter with spaces inside to pass to Test.bat which must be enclosed in double quotes because of the spaces.
Next the batch file continues with creation of Test.bat with content:
#echo %0 %*
#pause
#del %0 >nul
This little batch file just outputs with first line how it was called by the SFX archive, next waits for a key hit by the user and last deletes itself. So it does not really matter to which directory the batch file is extracted. The default is current directory which is the directory of the temporary files.
Then the batch file creates the SFX archive TestSetup.exe. For details on the used switches see Rar.txt in program files directory of WinRAR.
Pleae note that the line with Rar.exe works only without modification if the program files directory of WinRAR is included in environment variable PATH, or Windows fails to find Rar.exe which is the console version of WinRAR. Modify the line with complete path to Rar.exe in double quotes to get this line of the batch file working independent on included directories in PATH.
After the SFX RAR archive is created, the files Test.bat and TestSetup.txt are deleted as not needed anymore.
Now the created SFX archive TestSetup.exe is called and after hitting key RETURN and you see that the Test.bat is called with the 2 parameters as specified in TestSetup.txt.
The batch file deletes finally also the created SFX archive.

Related

I'm trying to use Robocopy to replace a cmd file

I've created a cmd file which uses the Robocopy command to update some files on the PC, but I can't replace the cmd files, because this contains the Robocopy script which is doing the updating. How do you replace a file which is doing the replacing?
I've moved the cmd file to another directory, which allows me to update most of the files, but I still can't replace the cmd file.
The Flags I'm using in Robocopy are /MIR /Copy:DAT /DCOPY:T
The Robocopy stopped at the cmd file and I can't replace it.
I don't see any reason for %SystemRoot%\System32\robocopy.exe failing to copy the batch file currently processed by cmd.exe than this batch file is additionally opened in an application like a text editor which prevents write access and deletion of the file as long as it is opened in the application.
However, the following code added to your batch file with unknown content could solve the problem.
#echo off
if /I not "%~dp0" == "%TEMP%\" (
copy /Y "%~f0" "%TEMP%" >nul 2>&1
if exist "%TEMP%\%~nx0" (
set "CurrentDirectory=%CD%"
set "InitialExecution=%~dp0"
cd /D "%TEMP%"
"%TEMP%\%~nx0" %*
)
)
rem Insert here other commands to execute by the batch
rem file now running from directory of temporary files.
rem The next three commands are only for demonstration.
if defined CurrentDirectory echo Initial current directory: %CurrentDirectory%
if defined InitialExecution echo Initial execution path: %InitialExecution%
pause
set "InitialExecution="
if defined CurrentDirectory set "CurrentDirectory=" & cd /D "%CurrentDirectory%" 2>nul & (goto) 2>nul & del "%~f0"
This batch file first checks if it is started from directory for temporary files. This is not the case on double clicking on the batch file, except the batch file is stored by chance in directory for temporary files by the user and double clicked on it in this directory. If batch file is not stored in directory for temporary files, it does following:
The batch file copies itself to directory of temporary files (only read access).
It verifies if the file copy was really successful which should be always true.
It defines two environment variables with path of current directory and initial execution path for later usage.
It sets the current directory to directory for temporary files.
This makes it possible to even delete the directory containing batch file on batch file directory being also current directory as typical on double clicking on a batch file stored on a local drive executed by current user.
The batch file runs itself from within directory for temporary files with passing all arguments passed to the batch file on initial execution further on its copy.
The Windows command processor cmd.exe executing the batch file continues batch file processing on its copy in temporary files directory with first line #echo off returning never to the initial batch file as started by the user.
Now with batch file processing done on a copy of initial batch file in temporary files directory and with current directory being also the directory for temporary files, the other commands in batch file can do everything in initial current directory respectively initial execution directory of the batch file like updating the files in these directories or even deleting these directories temporarily or permanently.
The three comment lines with command rem and the next three lines just demonstrate what can be done here and how to use the environment variables set by the batch file on initial execution. The two environment variables do not exist (most likely) on batch file being initially stored in directory for temporary files and executed by the user from this directory.
The batch file deletes the environment variable InitialExecution independent on its existence to restore initial environment in case of batch file executed from within a command prompt window.
Finally with batch file initially not executed from temporary files directory it deletes also the environment variable CurrentDirectory, changes the current directory back to initial current directory, if that directory still exists, and deletes itself from directory for temporary files.
(goto) 2>nul & del "%~f0" for batch file deletion without any error message output by Windows command processor was copied by me from Dave Benham's answer on How to make a batch file delete itself?
For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.
call /? ... explains %~dp0, %~f0 and %~nx0
cd /?
copy /?
del /?
echo /?
goto /?
if /?
pause /?
set /?
See also the Microsoft article about Using command redirection operators.

How to maintain a file association when calling a program from a batch file?

Is there a way to maintain the original file association when running a program from a batch file?
I created a batch file that calls a Windows program and performs some file maintenance. I changed the file association to the batch file. When I click on a file that's associated with that program, the batch file executes and opens the program but the file I click on isn't loaded. The original file association is lost.
This sorta makes sense because the CALL command within the batch file is once removed from the initial mouse-click that initiated the batch file.
Is there a syntax I can add that would pass the target file name to the batch file as a variable and append it to the CALL command line?
BTW, this is for an XP machine. Any assistance would be appreciated!
EDIT: here's the code I'm trying to write:
call "C:\Program Files\CorelDRAW X4\Programs\CorelDRW.exe"
:loop
if exist "C:\Documents and Settings\<user>\My Documents\corel user files\*.cdr" copy "C:\Documents and Settings\<user>\My Documents\corel user files\*.cdr" "C:\Documents and Settings\<user>\My Documents\corel user files\*.sav"
ping localhost -n 300 > nul
goto loop
I'm trying to protect CorelDraw's auto-save file. There's a bug whereby CorelDraw sometimes deletes the auto-save file during abnormal shut-down. I changed the .cdr file association so that clicking on a cdr file calls the batch file, which in turn calls Coreldraw and copies the auto-save file to a different filename. That part works, but I have to manually open the file I clicked on.
Ideally, I'd like to figure out a way to terminate the loop when I close CorelDraw, but I'll cross that bridge once I solve the file association problem.
EDIT2: Here is the result of echo %CMDCMDLINE%:
C:\WINDOWS\system32\cmd.exe /c ""C:\Documents and Settings\<user>\My Documents\corel user files\protect_autosave.bat" "C:\Documents and Settings\<user>\My Documents\filename.cdr""
As far as I have understood the requirements for the task the code to use in batch file %ProgramFiles%\CorelDRAW X4\Programs\CorelFile.bat is:
#echo off
if "%~1" == "" goto :EOF
"%ProgramFiles%\CorelDRAW X4\Programs\CorelDRW.exe" %*
for %%I in (%*) do if exist %%I copy /Y "%%~I" "%%~dpnI.sav" >nul
This batch file must be associated with file extension .cdr for example by importing following registry file on Windows XP and later Windows versions with administrator privileges:
REGEDIT4
[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\.cdr]
#="CorelDrawFile"
[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\CorelDrawFile]
#="Corel Draw Image"
[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\CorelDrawFile\DefaultIcon]
#=hex(2):22,25,50,72,6f,67,72,61,6d,46,69,6c,65,73,25,5c,43,6f,72,65,6c,44,52,\
41,57,20,58,34,5c,50,72,6f,67,72,61,6d,73,5c,43,6f,72,65,6c,44,52,57,2e,65,\
78,65,22,2c,30,00
[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\CorelDrawFile\shell\open\command]
#=hex(2):22,25,50,72,6f,67,72,61,6d,46,69,6c,65,73,25,5c,43,6f,72,65,6c,44,52,\
41,57,20,58,34,5c,50,72,6f,67,72,61,6d,73,5c,43,6f,72,65,6c,46,69,6c,65,2e,\
62,61,74,22,20,22,25,31,22,00
It is also possible to register file extension .cdr with absolute paths with REG_SZ instead of using REG_EXPAND_SZ and %ProgramFiles% in paths.
REGEDIT4
[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\.cdr]
#="CorelDrawFile"
[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\CorelDrawFile]
#="Corel Draw Image"
[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\CorelDrawFile\DefaultIcon]
#="\"C:\\Program Files\\CorelDRAW X4\\Programs\\CorelDRW.exe\",0"
[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\CorelDrawFile\shell\open\command]
#="\"C:\\Program Files\\CorelDRAW X4\\Programs\\CorelFile.bat\" \"%1\""
Windows Explorer calls for each selected *.cdr file the batch file on using context menu Open respectively on double clicking a single *.cdr file.
The batch file starts Corel Draw with all the arguments passed to the batch file passing to Corel Draw. This is usually just the file name of the *.cdr file with full path and file extension enclosed in double quotes.
After Corel Draw terminated, the batch file checks for existence of each file specified as command line argument and copies the file with same name in same directory with different file extension .sav.
The batch file is designed for being started with multiple *.cdr file names specified as arguments on command line. I don't know if Corel Draw supports multiple *.cdr files being specified on command line.
For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.
call /? ... explains %* (batch file) and %1 (Windows registry).
copy /?
echo /?
for /?
if /?

Change batch file directory in system32 to CMD command execution point

So basically I have a .bat that is inside my System32 folder.
This batch file accepts a parameter input, this input is a file.
I wanted it to be that I could open my Command Prompt, and for example do
batchfile text.txt
And it would pass test.txt into batchfile.bat. Obviously for my terminal to do this it needs to be in System32.
That is where my issue is. Because the batch file is in System32, when executing the command, it changes my directory to System32.
However the parameter I give it is a file. And when the command is executed and it changes the directory to System32, obviously it can no longer access the file.
How can I get around this?
Before you change directories, expand the filename to its fully-qualified filename and then reference it that way:
#echo off
for %%F in (%1) do set USR_REQFNM=%%~dpnxF
C:
cd \Windows\System32
echo The requested file is "%USR_REQFNM%"
goto ENDIT
REM Cleanup
:ENDIT
set USR_REQFNM=

How to edit a .bat file in the Winrar window explorer?

Has Winrar another way to edit bat file directly in explorer?
I click in a zip file it's show me a Winrar popup with many files, double click in some file open it in default program (.txt in Notepad++, .sql in Toad, etc.), but double click in bat files run the bat.
Alt + v is a shortcut to view file, but it does not allow me to edit the bat file.
The only way is drag the file to Windows, edit and drag back to Windows.
WinRAR has the feature to temporarily extract a double clicked file to Folder for temporary files as defined on tab Paths in Settings opened from menu Options, then start the application associated with that file, and after started application terminated, repack the file if modified at all back into the archive.
That is also done for executable files except the option File types to exclude from extracting on tab Security is enabled in Settings opened from menu Options with extracting by default the entire archive as defined under Unpack everything for on tab Viewer of Settings.
But the default behavior on double click can be customized on tab Viewer of Settings. After selecting there Ask instead of Internal viewer for Viewer type and entering C:\Windows\Notepad.exe as External viewer name a double click on a *.bat file inside an archive results in prompting the user for viewing with internal or external viewer or opening the file with associated program. Of course *.bat should not be in list of setting Unpack everything for.
By clicking now on button External viewer (Notepad.exe) the batch file is extracted to Folder for temporary files, then Notepad is started with that file for viewing and for editing. When finishing viewing/editing the batch file and exiting Notepad, WinRAR detects a modification and asks the user if the modified file should be updated in the archive.
There is the button Help on each Settings dialog. Please make use of it and read in help of WinRAR.
If always the same batch file inside an archive file should be edited, I suggest to automate this task as much as possible by using a batch file.
#echo off
setlocal
set "FileToEdit=Test.bat"
set "PathInArchive="
set "DefaultArchive=C:\Temp\Test.zip"
rem Set a title for the command prompt window and determine name
rem of this batch file with full path for a possible error message.
title Update %FileToEdit%
set "BatchFile=%~f0"
rem Use a standard archive file if none is specified as first parameter.
if "%~1"=="" (
set "ArchiveFile=%DefaultArchive%"
) else (
set "ArchiveFile=%~f1"
)
rem Test if the archive file exists at all.
if not exist "%ArchiveFile%" (
call :ErrorMessage "Archive file %ArchiveFile% does not exist."
exit /B
)
rem Make sure path in archive ends with a backslash
rem if a path to file in archive is defined at all.
if not "%PathInArchive%" == "" (
if not "%PathInArchive:~-1%" == "\" (
set "PathInArchive=%PathInArchive%\"
)
)
rem Extract the file to edit to directory for temporary files.
"%ProgramFiles%\WinRAR\WinRAR.exe" e -cfg- -ibck -y -- "%ArchiveFile%" "%PathInArchive%%FileToEdit%" "%TEMP%\"
if errorlevel 1 (
call :ErrorMessage "Failed to extract file %PathInArchive%%FileToEdit%"
exit /B
)
rem Start Windows Notepad to edit the temporary extracted file.
start "" /wait %windir%\Notepad.exe "%TEMP%\%FileToEdit%"
rem Define the option -ap with path in archive if needed at all.
set "ArchivePath="
if not "%PathInArchive%" == "" set "ArchivePath=-ap"%PathInArchive%""
rem Update the edited file in archive and delete it on success.
"%ProgramFiles%\WinRAR\WinRAR.exe" u %ArchivePath% -cfg- -df -ibck -ep -y -- "%ArchiveFile%" "%TEMP%\%FileToEdit%"
if errorlevel 1 (
del "%TEMP%\%FileToEdit%" 2>nul
call :ErrorMessage "Failed to update file %PathInArchive%%FileToEdit%"
exit /B
)
rem Exit batch processing.
exit /B
rem Subroutine to output an error message.
:ErrorMessage
echo Error detected by: %BatchFile%
echo On processing file: %ArchiveFile%
echo.
echo Error: %~1
echo.
endlocal
pause
exit /B
The values assigned to the variables FileToEdit, PathInArchive and DefaultArchive at top of the batch file should be defined appropriately.
By creating a shortcut (*.lnk) to this batch file in SendTo subfolder of folder %USERPROFILE% with a suitable name, right clicking on an archive file with the batch file to modify with fixed name and path inside archive and clicking in submenu Send To on this shortcut results in extracting the batch file to folder for temporary files, opening Windows Notepad for editing and after repacking finally the modified batch file into the archive file.
Open Help topics in menu Help in WinRAR and click on tab Contents on item Command line mode. The help pages under this contents list item explain the used Commands e and u as well as the used Switches.
For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.
call /?
del /?
echo /?
endlocal /?
exit /?
if /?
pause /?
rem /?
set /?
setlocal /?
start /?
title /?
From WinRAR, click Options -> Settings -> Viewer.
Activate Ask" into Viewer type.
Paste %WINDIR%\notepad.exe or the path of your favourite editor in External viewer name.
Click OK. Done.
Now, when you double click to any non exe file in the archive, a popup lets you choose how to open it:
Internal
External (via notepad or your editor)
Associated program

Write a batch file to do call another batch file and install a program

I am having trouble with a batch file. I have 2 files the first batch file runs and it creates the directory and copies the files needed. It appears to call the second batch file correctly because it opens the instructions.txt but then it stops. I will be running the first batch file from a CD and then the dbinstall.bat from the C:\testing folder.
this is my setup.bat
#echo off
md "C:\testing"
xcopy *.* C:\testing
CALL "C:\testing\dbinstall.bat"
Which in turns should call and run this
REM ***PLEASE REPLACE %DWVerFileName.exe WITH THE PROPER VERSION OF THE EXE FILE***
REM ***MAKE SURE THE 7z FILE INCLUDES THE CUSTOMER NAME AND THEN CHANGE %filename%.7z TO THE FILE NAME***
CALL "C:\testing\Instructions.rtf"
start /b /wait "C:\testing\7z423.exe"
SET AppExePath="%ProgramFiles(x86)%\7-zip\7z.exe"
IF NOT EXIST %AppExePath% SET AppExePath="%ProgramFiles%\7-zip\7z.exe"
%AppExePath% e database.7z
start /b /wait "setup.exe"
SQLCMD -E -S touch -Q "RESTORE DATABASE testing FROM DISK='C:\testing\database.bak'"
I am stuck and any help would be appreciated. Thanks
Since we have no idea of what "it stops" means, or where "it stops", I'd guess
CALL "C:\testing\Instructions.rtf"
should be
start "instructions" "C:\testing\Instructions.rtf"
which would then invoke whatever program is associated with .rtf, no doubt dislaying the instructions and keeping the displaying mechanism open while the 7z423 executable runs.

Resources