Trigger bat file in Jenkins - batch string + loop to delete multiple file directories

Background: Delete the file directory of the specified machine (node) through Jenkins job

Step1: Create Jenkins job - select Freestyle project (free style)

Step2: Select node node

Used to specify the machine to be deleted. If your jenkins does not have this option, download the Node and Label parameter plugin.

Step3: Add String parameters

This parameter is used to specify the directory to be deleted. Select the String type.

The parameter name can be arbitrary, just keep it consistent with the subsequent cmd command. What I write here is path

value writes the path of the file to be deleted. If you want to delete multiple files, separate them with a - sign (it does not have to be a - sign, you can also set * or &, etc., but it must be consistent with the delimiter written in the script) )

Step4: Select build - Execute Windows batch command

Write the following command

@echo off
cd /d e:/script
delfolder.bat %path%

The e:/script above refers to the directory where you store the bat file, and delfolder.bat is the script that actually executes the deletion of the file.

At this point, the setup of the jenkins job ends.

Optional: You can set a scheduled cleaning at zero o'clock every day

Step5: Create bat file

Mine is to create delfolder.bat in the e:/script directory, the content is:

setlocal enabledelayedexpansion
set str=%path%
set remain=%str%
echo %remain%
:loop
for /F "tokens=1* delims=-" %%a in ("%remain%") do (
    echo ***The file to be deleted is %%a***
    del /f /s /q %%a\*.*
    rd /s /q %%a
    set remain=%%b
)
if defined remain goto :loop

Note that the path variable in the script is consistent with the variable name set by Jenkins.

delims=- refers to using - as the delimiter, which is also consistent with the delimiter in the path variable in the Jenkins job.

Guess you like

Origin blog.csdn.net/fenger_c/article/details/115482189
Recommended