mardi 10 septembre 2019

Conditional concatenation in batch

I am trying to append an underscore to strings when they are not blank. This is for a backup program, where the name of the folder is [prefix][date][time]_[suffix], except the first and last underscores are supposed to only be added when the [prefix] or [suffix] are not empty.

(sorry for the formatting problems, I'm new to posting here. "[prefix][date][time]_[suffix]" is supposed to be separated by underscores at every "][" and not in italics.)

I've attempted to concatenate with "set PRX=%PRX% and _" as I read on some forum, although the and wasn't recognised (it just outputted "backup and _"). I also tried jumping around the files with "goto", but to no avail. I think it's with the concatenation.

@echo off

set /p DRV=Enter drive/directory to back up (e.g. %userprofile% or C:): set /p DRU=Enter directory to save to (e.g. C:\backup or F:): set /p PRX=Enter the prefix for the directory. Directory will be saved as [prefix]_[date]_[time]_[suffix]: set /p SFX=Enter the suffix for the directory. Directory will be saved as [prefix]_[date]_[time]_[suffix]:

if %PRX% NEQ "" (set PRX=%PRX%_)

if %SFX% NEQ "" (set SFX=_%SFX%)

set /p CONT=%CD%, Are you sure you want to continue (Y/N)? if /i "%CONT%" EQU "N" goto :cancel

cls

echo Initialising...

%DRV:~0,1%: cd\

set DAT=%date:~6,4%-%date:~3,2%-%date:~0,2% set TIM=%time:~0,2%-%time:~3,2%-%time:~6,2%

mkdir "%DRU%\%PRX%%DAT%_%TIM%%SFX%"

echo Cloning Files... echo.

xcopy "%DRV%\*" "%DRU%\%PRX%%DAT%_%TIM%%SFX%" /s

I inputted

Enter drive/directory to back up (e.g. %userprofile% or C:): %userprofile%\downloads

Enter directory to save to (e.g. C:\backup or F:): %userprofile%\backup

Enter the prefix for the directory. Directory will be saved as [prefix][date][time]_[suffix]:

(that is empty)

Enter the suffix for the directory. Directory will be saved as [prefix][date][time]_[suffix]: aa

the outputted folder was named "_2019-09-10_17-08-35_aa" as opposed to "2019-09-10_17-08-35_aa", not what I was expecting.

I appreciate any reply, thank you for your time.

Aucun commentaire:

Enregistrer un commentaire