简体   繁体   中英

How to Create Conditioned Subfolder through batch file?

We are using Jenkins as our CI tool. At present, I have written a batch file, which takes build value from a notepad file (written by developer), and then copies it to the network drive with the build value as a folder.

The batch file is mentioned below

    for /f "tokens=2" %%i in ('findstr Build "C:\Program Files (x86)\Jenkins\jobs\**\build.txt"') do set Build=%%i
    set Build=%Build:'=%

if not exist "\%Build%\" mkdir "%Build%\"

My motive now is that, if the value in notepad is 6.1 or 6.a, ie anything after decimal,it doesn't create folder, but places it in 6 subfolder. Alternatively, if the value is full, it creates a parent folder.

The text file, where I get the build number is mentioned below.

#define MyAppVersion '4.0.0.0'
#define MyFullAppVersion '4.1.0.0'
#define BuildNumber '81'

I need to create folder on the basis of value entered in "Build Number". If the value is in decimal, it should create within subfolder, else there should be only a main folder.

With the help of below input, I created below batch file for achieving this, but it didn't worked.

for /f "tokens=2" %%i in ('findstr Build "C:\Folder Check\logs\build.txt"') do set Build=%%i
set Build=%Build:'=%

for /F "tokens=2""delims=." %%j in ('findstr Build "C:\Folder Check\logs\build.txt"') do set "sub=%%j"

if exist "\\network\%Version%\%Build%\" mkdir "\\network\%Build%\%sub%%"

if not exist "\\network\%Version%\%Build%\" mkdir "\\network\%Version%\%Build%\"

if exist "\\network\%Version%\%Build%\%sub%%j" XCOPY /y "C:\Program Files (x86)\Notepad++\readme.txt" "\\network\%Version%\%Build%\%sub%\" /E /S

if not exist "\\172.19.0.4\Departement$\Development\Development RCX\RCX_M02_CSP\INTERNAL RELEASES\CI\%Version%\%Build%\%sub%%" XCOPY /y "C:\Program Files (x86)\Notepad++\readme.txt" "\\network\%Version%\%Build%\" /E /S

Any help will be greatly appreciated.

You could use the following code to split off the period . and everything behind:

for /F "delims=." %%j in ("%Build%") do set "Build=%%j"

Take also a look at the quoted set syntax, which is the one I recommend in general, because no special characters could cause problems that way.

Your if exist syntax appears a bit odd to me; I am not sure where the directory %Build% should be located, because you precede a \\ during existence check but you do not during creation. If you want to ensure the directory exists in the current working directory, do not precede the \\ :

if not exist "%Build%\" mkdir "%Build%"

If you want it to be located in the root directory od the current drive, then do precede it by \\ , but this is not what you want, I guess.

However, the appended \\ is needed to check a directory for existence; not providing it would also check for files.

Anyway, to ensure a directory exists, you do not need to check for existence, you can just create it and suppress the error message that appears in case the directory already exists, like this:

mkdir "%Build%" 2> nul

I am not sure if I interpret the ** in your path specification right, but I assume you are going to let me know...

You can use global wild-cards ( * , ? ) only in the last element of a path, so C:\\Program Files (x86)\\Jenkins\\jobs\\**\\build.txt is not valid. In case there is only a single directory in C:\\Program Files (x86)\\Jenkins\\jobs\\ , you can get it like this:

for /D %%D in ("C:\Program Files (x86)\Jenkins\jobs\*") do set "SubDir=%%~D"

If there are multiple directories and you want to get the last modified one, you could use this:

for /F "eol=: delims=" %%D in ('
    dir /B /A:D /O:D "C:\Program Files (x86)\Jenkins\jobs\*"
') do set "SubDir=%%D"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM