Listing all files in a folder from an Azure pipeline - cmd

I am trying to execute a command for every file inside the build directory by doing something that looks like this:
- task: CmdLine#2
displayName: 'List files'
inputs:
script: |
for %f in ( "$(Build.ArtifactStagingDirectory)\*" ) do #echo %f
I am getting the following error though,
==============================================================================
Generating script.
Script contents:
for %f in ( "C:\Users\tester\my_agent\_work\1\a\*" ) do #echo %f
========================== Starting Command Output ===========================
"C:\windows\system32\cmd.exe" /D /E:ON /V:OFF /S /C "CALL "C:\Users\tester\my_agent\_work\_temp\7a83c3a7-8a9e-4edf-aa36-e4e14d1ee5c7.cmd""
\Users\tester\my_agent\_work\1\a\*" ) do #echo f was unexpected at this time.
##[error]Cmd.exe exited with code '255'.
Finishing: List files
How can I correct my task above so it works as expected?

In a batch script you have to double the %:
Here is the example:
- task: CmdLine#2
displayName: 'List files'
inputs:
script: |
for %%f in ( "$(Build.ArtifactStagingDirectory)\*" ) do #echo %%f

You could try:
- task: CmdLine#2
displayName: 'List files'
inputs:
script: |
dir /a-D /S /B
workingDirectory: '$(Build.ArtifactStagingDirectory)'

Related

Win Task Scheduler generates multiple folders instead a zip file

I want to zip a project folder each month, programmed in a task scheduler executing a .bat file with the following script:
#echo off
set backuptime=%date:~4,2%-%date:~7,2%-%date:~10,4%
"C:\Program Files\7-Zip\7z.exe" a -tzip "D:\backups\projects\%1\%backuptime%.backup.zip" "D:\xampp\htdocs\%1"
echo Done!
%1 is the name of the project as parameter.
When I execute the bat as a Windows command (cmd), works fine generating a file: 17052022.backup.zip, but when is executed via Win task scheduler, the result is this:
Why the day and the month are a new folders?
Solution
Thanks for the suggestion. I have generated the zip with the date name from the O.S. configuration and not from my own user:
#echo off
for /f %%# in ('wmic os get localdatetime^|findstr .') do if "%%#" neq "" set date=%%#
set date=%date:~,4%%date:~4,2%%date:~6,2%
"C:\Program Files\7-Zip\7z.exe" a -tzip "D:\backups\projects\%1\%date%.zip" "D:\xampp\htdocs\%1"
echo Done!

How to run a command in Windows batch script?

I'm trying to feed the file name dynamically to a command through windows batch script. Here's my code,
#echo off
set /p path="Enter the project path: "
for /R "%path%" %%I in (*.java) do (java -classpath MyCheckStyle.jar;checkstyle-5.6-all.jar com.puppycrawl.tools.checkstyle.Main -c custom_check.xml %%I)
pause
Basically, I want to get all the .java files from any given directory and feed those java files to another command which will run for each file. In place of %%I I am trying to provide the java file dynamically. But it says java command not recognized.
If I use java -classpath MyCheckStyle.jar;checkstyle-5.6-all.jar com.puppycrawl.tools.checkstyle.Main -c custom_check.xml path/filename.java alone in a batch file. It works fine.
I have tried the following also,
#echo off
set i=-1
set /p path="Enter the project path: "
for /R "%path%" %%I in (*.java) do (
set /a i=!i!+1
set filenames[!i!]=%%I
)
set lastindex=!i!
for /L %%f in (0,1,!lastindex!) do (
java -classpath MyCheckStyle.jar;checkstyle-5.6-all.jar com.puppycrawl.tools.checkstyle.Main -c custom_check.xml !names[%%f]!
)
pause
But again it says java command not recognized.
What am doing wrong here? Why I can not pass the .java file names dynamically?
Your named your variable with project path "path", and it's bad, because as soon as you do
set /p path="Enter the project path: "
you overwrite the system "path" and then Windows cannot find your "java.exe".
Rename it to project_path:
set /p project_path="Enter the project path: "
for /R "%project_path%" %%I in .......

Execute python via batch based on some condition?

I have a python script which it is downloading some files, before executing the task, I want to know if directory is empty or if the number of files less than 5 then run the python otherwise just say the files already exist, how can I modify the batch code in order to apply this change.
#echo off
.....
:begin
python.exe "%ScriptFolderPath%"\extractDAILYdata.py -u %UserName% -p %Password% -s %BirstSpace% -r %ATIBaseUrl% -sp %DailyLoadPath% -f "%LogPath%" -i %LogLevel% -l %LogFile%
if %errorlevel%==0 goto bcsuccess
:bcerror
echo Task "%TaskName%" failed: %date% >> "%LogPath%\%LogFile%"
exit /B %errorlevel%
:bcsuccess
echo Task "%TaskName%" succeeded: %date% >> "%LogPath%\%LogFile%"

nant delete subfolders exclude files

need help regarding delete task in nant or command which can be used in nant build file.
here is the requirement, i have multiple files and folders in a root folder.
i need to delete only folders but not files..
Any idea how to do this.
Blockquote
EX : ROOT
-a.txt
-b.txt
-Folder1
-Folder2
after deletion, it should be
Blockquote
EX: ROOT-
-a.txt
-b.txt
Thanks in advance.
for /d %%a in ("c:\some\root\*") do echo rmdir /s /q "%%~fa"
For each folder inside the indicated path, matching the indicated mask, remove the folder
Delete operations are echoed to console. If the output is correct, remove the echo command to execute the rmdir commands.
To include it in a build file, and avoid problems with quoting, it is better to create a batch file to contain the command and call this batch file. That way, the batch file will be something like
#echo off
if "%~1"=="" exit /b 1
for /d %%a in ("%~1\*") do echo rmdir /s /q "%%~fa"
With the path to the folder to clean passed as argument
Then, the exec task will be something like
<exec program="cmd.exe" commandline="/c theBatchFile.cmd" workingdir="${project.batchFiles}" output="e:\my.txt">
<arg value="${project.rootFolder}" />
</exec>
The variables will need to be defined pointing to
${project.batchFiles} = where the batch file is located
${project.rootFolder} = the folder that needs to be cleaned
So, the task will call cmd.exe to process the batch file, passing the folder as argument to the batch.
used the above suggustions and found it out :
Code :
<echo file="CleanFolders.bat">for /d %%a in ("${dir}\subdir\*") do rmdir /s /q "%%~fa</echo>
<exec program="CleanFolders.bat"/>

how to make a build failed in hudson using batch command

Im using Hudson for our HTML build main reason for using CI is to validate the html files during each files with cse validator. For validating the HTML I have used the following code
#echo off
PUSHD "F:\Solutions\Documents\Design\html\ValTest"
For %%X in (*.html) do (
"C:\Program Files\HTMLValidator100\cmdlineprocessor" -outputfile output.txt -r1 %%X
set HTMLVAL_ERROR=%ERRORLEVEL%
type output.txt >> result.txt
)
set ERRORLEVEL=%HTMLVAL_ERROR%
POPD
Validation process is working fine but even there is an error in the HTML file hudson is not triggering the build as failed, its always Success.
Please let me know how can I trigger the build failure from batch command.
You should use exit command :
#echo off
PUSHD "F:\Solutions\Documents\Design\html\ValTest" For %%X in (*.html) do ( "C:\Program Files\HTMLValidator100\cmdlineprocessor" -outputfile output.txt -r1 %%X set HTMLVAL_ERROR=%ERRORLEVEL% type output.txt >> result.txt )
POPD
exit %HTMLVAL_ERROR%
which sets the error level of the whole batch.

Resources