Batch file 批处理条件连接

Batch file 批处理条件连接,batch-file,if-statement,cmd,Batch File,If Statement,Cmd,我试图在字符串不是空的时候给它们加下划线。这适用于备份程序,其中文件夹的名称为[前缀]\u[日期]\u[时间]\u[后缀],但只有当[前缀]或[后缀]不为空时,才应添加第一个和最后一个下划线 当我在某个论坛上读到时,我尝试将与set PRX=%PRX%和连接起来,尽管没有识别出和(它只是输出了“backup and”)。我还试着用“goto”在文件中跳跃,但没有用。我认为这是连接 @echo off set /p DRV=Enter drive/directory to back up (e.

我试图在字符串不是空的时候给它们加下划线。这适用于备份程序,其中文件夹的名称为
[前缀]\u[日期]\u[时间]\u[后缀]
,但只有当
[前缀]
[后缀]
不为空时,才应添加第一个和最后一个下划线

当我在某个论坛上读到时,我尝试将与
set PRX=%PRX%和
连接起来,尽管没有识别出和(它只是输出了“backup and”)。我还试着用“goto”在文件中跳跃,但没有用。我认为这是连接

@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
我输入

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
输出的文件夹名为
“_2019-09-10_17-08-35_aa”
,而不是
“2019-09-10_17-08-35_aa”
,这不是我所期望的


非常感谢您的回复,谢谢您的时间。

字符串比较非常明确

IF "%PRX%" NEQ "" (SET "PRX=%PRX%_")
IF "%SFX%" NEQ "" (SET "SFX=_%SFX%")
无论区域设置如何,都可以轻松获得格式正确的数据和时间值

FOR /F %%A IN ('powershell -NoLogo -NoProfile -Command "Get-Date -Format 'yyyy-MM-dd'"') DO (
    SET "DAT=%%A"
)
FOR /F %%A IN ('powershell -NoLogo -NoProfile -Command "Get-Date -Format 'HH-mm-ss'"') DO (
    SET "TIM=%%A"
)

更改
如果%PRX%NEQ”“(设置PRX=%PRX%\ux)
=>
如果定义了PRX,则设置“PRX=%PRX%”
否则此行在空输入时返回错误。SFX也是如此。当您忽略目录时,为什么要选择输入带有
DRV
的目录?日期-时间子字符串依赖于区域设置/用户设置,最好使用WMIC/PwerShell来获得一致的日期-时间。如果“%PRX%”neq“”…,则引用比较的两部分,如
,如果定义了PRX…
,则使用
。此问题是否已解决?