How to escape % in windows10 batch command in jenkins build step? - windows

I defined below command in jenkins windows batch command build step:
for /r %%f in (.\*.exe) do echo %%f
But the echo doesn't print the file name. Instead it just prints %f to the console.
I have tried with single % as below but it doesn't help. It prints f to the console.
for /r %f in (.\*.exe) do echo %f
How can I escape % character on jenkins build step? I am not sure whether it relates to jenkins only.

On Jenkins 2.60.2 on Windows 10, with two Firefox installer executables in my directory, running for /r %%f in (.\*.exe) do echo %%f in a Windows Batch command, I get the following output:
Building in workspace C:\Program Files (x86)\Jenkins\workspace\Test
[Test] $ cmd /c call C:\WINDOWS\TEMP\jenkins4832870824680456992.bat
C:\Program Files (x86)\Jenkins\workspace\Test>for /R %f in (.\*.exe) do echo %f
C:\Program Files (x86)\Jenkins\workspace\Test>echo C:\Program Files (x86)\Jenkins\workspace\Test\.\Firefox Installer.en-US.exe
C:\Program Files (x86)\Jenkins\workspace\Test\.\Firefox Installer.en-US.exe
C:\Program Files (x86)\Jenkins\workspace\Test>echo C:\Program Files (x86)\Jenkins\workspace\Test\.\Firefox Installer.exe
C:\Program Files (x86)\Jenkins\workspace\Test\.\Firefox Installer.exe
As you can see, it correctly treated the double %% as a single % when running the batch command, which makes sense, as it just calls cmd /c call on the temporary batch file that it creates, and that is the correct way of doing % syntax in a batch file.
I don't think it likely that this behavior would have changed much from different versions of Jenkins, but what version are you running? Can you post the full output of your batch command? Maybe there's something in there that can give us a clue.

Related

Bash's "for" for windows

How to can I to write this linux-like loop in the Windows 7 command line?
for docker_path in `ls | grep "docker$"`
do
cd $docker_path
mvn -B -f pom.xml clean deploy -Pdocker
cd ..
done
I need found all *docker/ directory, exec there mvn-command and return into patern directory, but for Windows7 system.
On Windows command line use:
for /D %I in (*docker) do #pushd "%I" & mvn.exe -B -f pom.xml clean deploy -Pdocker & popd
Use the following command line if mvn is not an executable, but a *.bat or *.cmd file:
for /D %I in (*docker) do #pushd "%I" & call mvn.bat -B -f pom.xml clean deploy -Pdocker & popd
Well, on command prompt it is not really necessary to use command CALL to run a batch file in a loop together with two other commands as done here.
The first command line for usage in a batch file:
#for /D %%I in (*docker) do #pushd "%%I" & mvn.exe -B -f pom.xml clean deploy -Pdocker & popd
In a batch file the loop variable I must be referenced with two percent signs instead of just one as on command prompt. The loop variable can be only a single character.
The second command line for usage in a batch file:
for /D %%I in (*docker) do #pushd "%%I" & call mvn.bat -B -f pom.xml clean deploy -Pdocker & popd
In a batch file it is necessary to use command CALL to call another batch file and continue with next command respectively command line in current batch file after execution of the other batch file finished.
Windows command processor cmd.exe continues execution of current batch file on the other batch file on not using command CALL and exits batch file processing once the execution of other batch file finished without further processing command lines in current batch file.
See answer on How to call a batch file that is one level up from the current directory? for details on the existing methods to run a batch file from within a batch file.
The single command line can be also coded with multiple lines:
#echo off
for /D %%I in (*docker) do (
pushd "%%I"
mvn.exe -B -f pom.xml clean deploy -Pdocker
popd
)
And the same multi-line batch file solution in case of mvn is a batch file.
#echo off
for /D %%I in (*docker) do (
pushd "%%I"
call mvn.bat -B -f pom.xml clean deploy -Pdocker
popd
)
Command FOR with option /D searches with wildcard pattern *docker for non hidden directories in current directory of which directory name ends with the string docker.
It is advisable on Windows to reference a file to execute with complete name of file, i.e. file name + file extension. This makes it clear for Windows command processor as well as every reader of the code if the executed file is an executable or a script file which makes a difference as it can be seen here.
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 /?
echo /?
for /?
popd /?
pushd /?
See also:
Single line with multiple commands using Windows batch file
Microsoft's command-line reference
SS64.com - A-Z index of the Windows CMD command line

call command for bat script not working

Essentially I have two .bat scripts that work just fine individually, however I wanted to combine the two scripts into one action as i have it assigned to a hotkey and want all of this with one press of a key, not two but am having problems because the scripts are in different directories.
Below are my two scripts:
BLAH-Script.bat
RE4_UHD_BLAH_Tool.exe -p
DEL /F /S /Q /A "F:\r101\r101_09.AEV"
and the second script:
UDAS_TOOL.bat:
DEL /F /S /Q /A "F:\st1\rl01.udas"
ping -n 1 localhost
DEL /F /S /Q /A "F:\UDAS Tool\r101.udas"
UDAS_Tool.exe -p
Now, both of these work great on their own because each of the scripts calls on an .exe that is in the same DIR as the script that calls them, but trying to get them into one .bat file has not worked using the CALL function because one of the scripts exists in an outside directory.
I have tried to make a new .bat script that would execute both bat scripts in one:
call "F:\pack\BLAH-Script.bat"
ping -n 1 localhost
call "F:\pack\UDAS\UDAS_TOOL.bat"
However this does not work.The files are not packaged by the exe tools as intended.
To try and figure out the problem I added pause command between scripts:
call "F:\pack\BLAH-Script.bat"
pause
call "F:\pack\UDAS_TOOL.bat"
This command allowed me to read the output before the window was closed and yielded the culprit. In the first script I am getting the following error:
F:\UD - r101-->RE4_UHD_BLAH_Tool.exe -p
'RE4_UHD_BLAH_Tool.exe' is not recognized as an internal or external command,
operable program or batch file.
I believe what is happening it that each file works fine if run in its own directory because they are in the same DIR as the .exe they are calling, but the first .bat file will not execute because the third bat file ( the one that is that is calling the first & second) is not in the same DIR as the first.
I have tried:
CD /D "F:\pack"
call "F:\pack\BLAH-Script.bat"
pause
CD /D "F:\pack"
call "F:\pack\UDAS\UDAS_TOOL.bat"
with the same error.
How do I get the directories sorted out ?
In theory, you could simply fully-qualify the executable path, or make sure that the executables are located on the path.
However, some programs assume that the executable is in the current directory, and expect that configuration files, etc. are similarly in that directory, so what I'd do is
in each batch, after the initialisation ceremony (#echo off/setlocal...) add a
pushd "actualexcutablelocation"
and ensure that on exit from the batch,
popd
is executed to restore to the original directory.
pushd switches current directory to the nominated directory until a popd is executed to return to the original.

Use embedded file from .exe

I made an exe that has embedded files in it like a portable 7zip (7za.exe) and I want to call to it in the batch script that I am compiling into an exe but when I do it just gives me "7za.exe" is not recognized as an internal or external command. If I left anything out just ask.
(Sorry if this is an easy fix I am just messing around with some basic code)
This is the code I am working with and exe is in releases tab.
https://github.com/iamtis/mass-extract
Let us look on batch file with some additional lines at top:
#echo off
echo Current working directory is: %CD%
echo Directory of batch file is: %~dp0
pause
echo Files in current working directory:
dir /A-D /B
pause
echo Files in directory of batch file:
dir /A-D /B "%~dp0"
pause
I suppose that the current working directory is not equal the directory of the batch file and the tools are in the directory of the batch file. I suppose the batch file directory is a subdirectory with random name in %TEMP%.
So what you most likely need is:
#echo off
set "ToolPath=%~dp0"
if not exist "%CD%\archive\*" md "%CD%\archive"
"%ToolPath%7za.exe" x "%CD%\*.zip" "%CD%\archive\"
"%ToolPath%7za.exe" x "%CD%\*.7z" "%CD%\archive\"
"%ToolPath%unrar.exe" x "%CD%\*.rar" "%CD%\archive\"
"%ToolPath%7za.exe" a -mx9 archive.7z "%CD%\archive\"
rd /S /Q "%CD%\archive"
set "ToolPath="

batchfile is not running in prebuild event vs2013

I had a batch command that copies multiple files from source to destination
for %I in ( C:\Source\abc.txt C:\Source\cba.txt) do copy %I C:\Destination
if we give the command in buildevent->prebuild event ->commandline of visual studio 2013
Its throwing me the error
error MSB3073: The command "for %I in ( C:\Source\abc.txt C:\Source\cba.txt) do xcopy %I C:\Destination :VCEnd" exited with code 255.
If we run the command in command prompt its successfully copied.
Why this is throwing the error in vs 2013 ?
Thanks
Phani
Issue
From a quick search around, it would seem that error code 255 means that it is failing to find the file.
ILMerge - the command exited with code 255
Copy task, MSB3073, exit code 255
ERROR_EA_LIST_INCONSISTENT
255 (0xFF)
The extended attributes are inconsistent.
Resolution
Try using absolute paths "%~fI" in the command and the flag /Y to prevent copy prompts.
for %I in ("C:\Source\abc.txt" "C:\Source\cba.txt") do xcopy "%~fI" "C:\Destination" /Y
May also be experiencing file or directory prompt which can be resolved with echo d |
for %I in ("C:\Source\abc.txt" "C:\Source\cba.txt") do echo d | xcopy "%~fI" "C:\Destination" /Y

Windows batch file syntax

The following windows 7 batch file script returns the error:
#ECHO OFF
if exist C:\Program Files (x86)\ E1\P45V goto WIN7
ren /s /c "c:\Program Files\ E1\P45V\P45Login.bmp" "c:\Program Files\E1\P45V\P45Login_OLD.bmp"
copy "\\locattion14\temp\E1\P45Login.bmp" "c:\Program Files\ E1\P45V\P45Login.bmp"
goto END
:WIN7
ren /s /c "c:\Program Files (x86)\ E1\P45V\P45Login.bmp" "c:\Program Files (x86)\E1\P45V\P45Login_OLD.bmp"
copy "\\locattion14\temp\E1\P45Login.bmp" "c:\Program Files (x86)\ E1\P45V\P45Login.bmp"
:END
The syntax of the command is incorrect
Using PSTOOLs to push out a change to computers, and will add the list when the syntax error is corrected.
The desired result:
If the pc is an XP machine, rename the P45login.bmp file to same name_OLD.bmp, then copy the file from loaction 14 into the directory noted.
If the PC is a Win 7 machine, skip the first part, go to the second part, and commit the same changes.
close the session.
I have moved quotes, added/subtracted switches, but arrive at the same error.
Surely it is just a simple syntax particularity that I am not catching.
Hoping someone will take a look, see the obvious I am missing, and point me in the right direction,.
Thank you for any help or suggestions.
Your ren syntax is wrong. ren does not support any switches and also rename_to needs to be name only, not full path. See full details here: http://technet.microsoft.com/en-us/library/cc754276%28v=ws.10%29.aspx
Additionally:
- if exists needs quotes around path
- you use both \ E1\ or \E1\ (with or without space). While both could be valid, I would double check if that's not an error.
- if the paths above are actually different, you need to use move (with full paths) instead of ren.

Resources