Discussion forum for all Windows batch related topics.
Moderator: DosItHelp
-
fye
- Posts: 2
- Joined: 27 Feb 2013 12:32
#1
Post
by fye » 27 Feb 2013 12:51
I am trying to write a 7z.bat script to be used in place 7z.exe that simply examines if a working directory has been specified as an argument (-w switch seen below) and inserts a default working directory if one isn't found. I have the script mostly working with one exception, when you enter a special character ( in my case the '!' in '-x!.\Ignore.txt' ) it doesn't appear at the other end. I figure this is due to my immediate evaluation delimiters (using !swap! instead of %swap%) on my variables but I feel they are necessary as the output is not evaluated correctly when they are absent.
Is there a better way to write this script/is there some way to prevent the '!' character from being gobbled up?
Input:
Code: Select all
7z.bat a -x!"Ignore.txt" -y ".\ZipFile.zip" *.*
Desired Output/Command executed:
Code: Select all
7z.exe a -w"..\UpDirectory" -x!"Ignore.txt" -y ".\ZipFile.zip" *.*
Code: Select all
setlocal enabledelayedexpansion
:: Pipe all input parameters to 7z.bat into a findstring function
:: In these parameters, check if any contain the "-w" switch
:: which specifies working directory
echo.%* | findstr /C:"-w" 1>nul
:: If no -w switch exists in the input parameters, select the default
:: current working directory as UpDirectory
if errorlevel 1 (
:: Save the command parameter
set command=%1
set rest=
set append=
set swap=
shift
goto LOOP
:LOOP
::Check if the shifted arguement is blank
set append=%1
if "!append!"=="" (
goto END
)
::check if the shifted arguement is the command
if NOT [!append!]==[!command!] (
set swap=!rest!
set rest=!swap!!append!
)
shift
goto LOOP
:END
set statement=7z.exe !command! -w"..\UpDirectory"!rest!
echo !statement!
!statement!
goto EXIT
)
:: If a -w switch is present, assume the user is conciously selecting
:: the correct working directory and pass commands in as normal
7z.exe %*
:EXIT
-
foxidrive
- Expert
- Posts: 6031
- Joined: 10 Feb 2012 02:20
#2
Post
by foxidrive » 27 Feb 2013 13:49
Note that if you want to call this batch file 7z.bat then you will need to specify the exact path to 7z.exe and it will need to be earlier on the system PATH than 7z.exe is.
A common mistake is calling a batch file the same name as an external command.
Code: Select all
@echo off
set "workingdir="
:: Pipe all input parameters to 7z.bat through findstr
:: if the "-w" switch is not found then set the working folder
echo.%* | findstr /i /C:"-w" >nul && set workingdir=-w"..\UpDirectory"
:: use the first character of the command line as the 7ZIP command and add the working directory if not present
:: otherwise it will use the first character command and the rest of the command line, as the workingdir variable will be empty.
set 7zline=%*
set 7zline=%7zline:~0,1% %workingdir% %7zline:~1%
:: If a -w switch is present then it will be included but if none
:: is present then it will use the default workingdir set above.
"c:\program files\7-zip\7z.exe" %7zline%
-
fye
- Posts: 2
- Joined: 27 Feb 2013 12:32
#3
Post
by fye » 27 Feb 2013 14:24
Thank you for the help! One small note is that
Code: Select all
echo.%* | findstr /i /C:"-w" >nul && set workingdir=-w"..\UpDirectory"
should be
Code: Select all
echo.%* | findstr /i /C:"-w" >nul || set workingdir=-w"..\UpDirectory"
in order for the script to insert the working directory if -w is not found.