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
Related
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.
I want to copy all the files of a folder into some other folders using batch script. Say, I have two folders named folder1 and folder2. these two folders are located in C:\Users\xyz . I want to copy the elements of another folder (say, folder3 which is located in C:\Users\abc\def) into these two folders. I have made the following script but nothing is copied. My sample batch file is as follows:
FOR /L %%A IN (1,1,2) DO (
xcopy /s C:\Users\abc\def\folder3 C:\Users\xyz\folder%%A
)
is there anything wrong in the batch file?
xcopy /s C:\Users\abc\def\folder3\*.* C:\Users\xyz\folder%%A\
where *.* is an appropriate filemask and the final \ in the destination name tells cmd that the destination is a directory.
I suggest using this command line in the batch file:
for /L %%A in(1,1,2) do %SystemRoot%\System32\xcopy.exe "C:\Users\abc\def\folder3" "C:\Users\xyz\folder%%A\" /C /G /H /I /K /R /Q /S /Y >nul
I enclosed both directory paths in double quotes in case of real paths contain 1 or more spaces or other special characters which require double quotes. The last paragraph on last help page output by running in a command prompt window cmd /? outputs on which characters in a directory/file name double quotes are required around the complete directory/file name.
The target path ends with a backslash to make it clear for console application xcopy that the target is a directory and not a file. Together with the redundant /I the target directory is created if not existing already.
For details on the options used on xcopy open a command prompt window and run xcopy /?. This outputs the help for this console application in the command prompt window. On Windows running a command or console application with /? as parameter outputs in general the help for the command/application.
Note: The copying from one user profile directory to another user profile directory requires local administrator privileges. Each user profile directory is by default protected for exclusive usage of the owning user. Therefore I suggest to open a command prompt window and execute in this window:
for /L %A in(1,1,2) do %SystemRoot%\System32\xcopy.exe "C:\Users\abc\def\folder3" "C:\Users\xyz\folder%A\" /C /G /H /I /K /R /S /Y
You can see if that works with just %A as required on command line instead of %%A as required in batch files and without /Q (quiet copying) and without >nul (redirection of success messages to device NUL to suppress them). Or when it does not work, you can see why it does not work as the error message can be viewed on running a command or a batch file from within a command prompt window instead of double clicking on a batch file because the console window keeps open.
Ok, I've installed Dropbox but it didn't corresponded to what I was looking for so I uninstalled it with Revo Pro.
But, when i open the taskmanager there are still processes related to it running in my computer so I decided to make a batch to look out and delete all files that are related to it.
#echo off
cd c:\
:a
set /p a=Phrase that might be realted to it
for /r %%d IN (*.*) DO (
(
findstr /i /m /c:%a% "%%d"
if "%errorlevel%"=="0" del "%%d"
echo %errorlevel%
)
)
pause
The problem is: when I run findstr using loop even when there is no match for my variable "%a%" in an analized file %errorlevel% returns as 0. But when I use findstr alone and there isn't a match %ERRORLEVEL% returns as 1 and 0 for a match.
If I use it, I'll delete all my PC files haha. What's wrong with the code?
Within a parenthesised series of statements, any %var% is replaced by the value of that variable at the time the verb controlling that statement-sequence (or block) is encountered.
Here, the block is the entire sequence of statements controlled by the for. %errorlevel% is replaced by the status of errorlevel at the time the for is encountered, so probably 0.
If you use
findstr /i /m /c:%a% "%%d"
if not errorlevel 1 del "%%d"
echo %errorlevel%
then the run-time value of errorlevel is used (ie. as it changes through the operation of the loop) and the command means "if errorlevel is not (1 or greater than 1) do this..."
The findstr will set errorlevel to 0 on found, 1 on not found and 2 for file not found(IIRC) so NOT (1 or greater than 1) selects 0 only. Note that in certain esoteric circumstances, errorlevel may become negative, but after a findstr I believe 0..2 is the allowed range.
Not sure what's wrong with the code, but you can probably skip it using the && operand.
findstr /i /m /c:%a% "%%d" && del "%%d" echo %errorlevel%
Thanks to Stephan for correcting the example.
Whenever Windows command interpreter encounters ( being interpreted as begin of a command block, it parses the entire command block up to matching ) marking end of the command block and replaces all %variable% by current value of the variable.
This means in this case that before command FOR is the first time executed, everything from ( after DO up to last ) is processed already with replacing all %variable% references by current value of the appropriate variable. Then the already preprocessed block is executed one (on command IF) or more times (on command FOR).
This behavior can be seen by debugging the batch file. For debugging a batch file first #echo off must be removed or commented out with command REM or changed to #echo on. Then a command prompt window must be opened and the batch file is executed from within this command prompt window by typing its name with full path enclosed in double quotes if path or name contains a space character. The Windows command interpreter shows now all command lines and command blocks after preprocessing before executing and of course the standard messages and the error messages output by the commands or by Windows command interpreter itself in case of a syntax error in batch file.
Opening a command prompt window means running cmd.exe with option /K to Keep window open after execution of a command or a batch script. Double clicking on a batch file starts also cmd.exe for processing the batch file, but with parameter /C to Close the window automatically after batch processing terminated independent on cause - successful finished or an error occurred.
The command prompt window opened before running the batch file remains open after batch processing finished successfully or with an error except the batch file contains command EXIT without parameter /B. So experts in batch code writing test batch files always by running them from within a command prompt window instead of double clicking on them.
Delayed variable expansion is needed for variables set or modified and referenced within same command block as explained by help of command SET output on running in a command prompt window set /?.
#echo off
setlocal EnableDelayedExpansion
cd /D C:\
:a
set /P "a=Phrase that might be realted to it: "
for /r %%d in (*) do (
%SystemRoot%\System32\findstr.exe /i /m /c:"%a%" "%%d"
if "!errorlevel!" == "0" del "%%d" >nul
)
endlocal
But for checking the exit code of a previous command there is also if errorlevel syntax as explained by Microsoft in support article Testing for a Specific Error Level in Batch Files.
#echo off
setlocal EnableDelayedExpansion
cd /D C:\
:a
set /P "a=Phrase that might be realted to it: "
for /r %%d in (*) do (
%SystemRoot%\System32\findstr.exe /i /m /c:"%a%" "%%d" >nul
if not errorlevel 1 del "%%d" >nul
)
endlocal
if errorlevel X tests if exit code of previous command or application when it modifies the errorlevel variable at all is greater or equal X. By using if not errorlevel X the check is if last exit code is lower than X which is here a test if exit code is 0.
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.
cd /?
del /?
echo /?
for /?
if /?
set /?
And see also
Microsoft's command-line reference
SS64.com - A-Z index of the Windows CMD command line
Microsoft article about Using command redirection operators
Answer on question Single line with multiple commands using Windows batch file
How to set environment variables with spaces?
Goal: I want to use CMD.EXE to find a single MSI, located in C:\ProgramData - not elsewhere - and then execute it.
My attempt: dir /s /b C:\programdata\*"my program"*.msi | explorer
Problem: Explorer opens but doesn't launch my MSI.
Constraints: I can't write a .BAT. So this must run on the command line.
Although that doesn't surprise me, I apparently don't understand CMD.EXE and piping well enough to do this. Any guidance?
A *.msi file is not an executable. It is a compiled installer script file which needs an interpreter for execution. The interpreter is msiexec.exe.
Searching for a file can be done with command DIR or with command FOR.
The better solution using command FOR:
for /R C:\ProgramData %# in ("my program*.msi") do %SystemRoot%\System32\msiexec.exe /i "%#"
The more complicated solution using the commands DIR and FOR:
for /F "delims=" %# in ('dir /A-D /B /S "C:\ProgramData\my program*.msi" 2^>nul') do %SystemRoot%\System32\msiexec.exe /i "%#"
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.
dir /?
for /?
msiexec /?
Note: %%# instead of %# would be needed if one of the two command lines is used within a batch file.
my Xcopy fails to execute properly after I compile my project.
xcopy "$(SolutionDir)LuaModules\*.*" "$(TargetDir)Modules\" /S /H /Y /C
You have unneeded slash after /S.
$(SolutionDir) gives the directory where the solution is in, but the project ($ProjectDir) gives the folder of your files. I had to use that instead