如何使用一套功能批处理脚本接受空间 [英] How to use the set function in a batch script to accept spaces

查看:88
本文介绍了如何使用一套功能批处理脚本接受空间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想更多地了解批处理脚本,我用Google搜索这个问题,并没有真正理解我读的东西。

所以我有以下批处理脚本来创建多个文件夹。批处理脚本创建具有文件夹名称当前日期的文件夹。这也提示输入文件夹名用户。我不能得到正确的唯一的事情是我可以在文件夹名称空间。如果我把该文件夹例如我的文档的什么都不会发生(批次将无法正常工作)。

那么,怎样才能使我这批接受在文件名中空间?

  ::日期设定
        @ECHO关闭
        SETLOCAL ENABLEEXTENSIONS
        如果%DATE%ALSSA(设置toks = 1-3)其他(集toks = 2-4)
        FOR / F令牌= 2-4 delims =( - )%%一个在('回声:^ |日期')做(
          FOR / F令牌=%toks%delims = .- /%% i的('日期/ T')做(
            集'%% A'= %%我
            集'%% B'= %%Ĵ
            集'%% C'= %% K))
        如果%'YY'%LSS 100集'YY'= 20%'YY'%
        设置如今=%'YY'% - %毫米% - %DD%
    ENDLOCAL&安培; SET v_year =%'YY'%放大器; SET v_month =%毫米%放大器; SET v_day =%DD%
:: ECHO今天是新年:[%V_Year%]月:[%V_Month%]天:[%V_Day%]
:: ------------------------------------------------ ------------------------------------------------
:: AskForFolderName
设置/ p NewFolder =在这里输入文件夹名称:
如果[%NewFolder%] == []转到AskForFolderName
如果存在%NewFolder%(
   回声文件夹已经存在
   回声。
   转到AskForFolderName

:: ------------------------------------------------ ------------------------------------------------
MD%V_Year%%V_Month%%V_Day% - 。%NewFolder%\\ IMAGES \\ RAW图像
MD%V_Year%%V_Month%%V_Day% - 。%NewFolder%\\ IMAGES \\ EXPORTS \\ JPG
MD%V_Year%%V_Month%%V_Day% - 。%NewFolder%\\ IMAGES \\ EXPORTS \\ PNG
MD%V_Year%%V_Month%%V_Day% - 。%NewFolder%\\ IMAGES \\ EXPORTS \\ PSD
MD%V_Year%%V_Month%%V_Day% - 。%NewFolder%\\ VIDEO \\原始视频
MD%V_Year%%V_Month%%V_Day% - 。%NewFolder%\\ VIDEO \\ EDITS
MD%V_Year%%V_Month%%V_Day% - 。%NewFolder%\\ VIDEO \\出口


解决方案

 如果[%NewFolder%] == []转到AskForFolderName

让我们来拆解这条线,假设%newfolder%我的文档

 如果[我的文档] == []转到AskForFolderName

如果语法如果参数1比较参数2命令

参数1 [我的,比较文件] ,等待...什么!语法错误!

处理空间的批处理方式包围在字符串中qoutes:

 如果%NewFolder%==转到AskForFolderName

参数1 我的文档,比较 == 参数2 ,命令转到... - 语法确定

(有趣的细节:在非常下一行(是否存在... )使用正确引用的字符串)

I am trying to learn more about batch scripts and I have googled this question without really understand what I am reading.

So i have the following batch script to create multiple folders. The batch script creates the folders with the current date in the folder name. It also prompts the user for the folder name. The only thing I cant get right is how I can have spaces in the folder name. If I call the folder e.g My documents nothing will happen (the batch will not work).

So how can I enable this batch to accept spaces in the file name?

:: DATE SETTINGS
        @ECHO off
        SETLOCAL ENABLEEXTENSIONS
        if "%date%A" LSS "A" (set toks=1-3) else (set toks=2-4)
        for /f "tokens=2-4 delims=(-)" %%a in ('echo:^|date') do (
          for /f "tokens=%toks% delims=.-/ " %%i in ('date/t') do (
            set '%%a'=%%i
            set '%%b'=%%j
            set '%%c'=%%k))
        if %'yy'% LSS 100 set 'yy'=20%'yy'%
        set Today=%'yy'%-%'mm'%-%'dd'% 
    ENDLOCAL & SET v_year=%'yy'%& SET v_month=%'mm'%& SET v_day=%'dd'%
:: ECHO Today is Year: [%V_Year%] Month: [%V_Month%] Day: [%V_Day%]
::------------------------------------------------------------------------------------------------
::AskForFolderName
set /p NewFolder=Enter folder name here:
If [%NewFolder%]==[] Goto AskForFolderName
If Exist "%NewFolder%" (
   Echo Folder already exists
   Echo.
   Goto AskForFolderName
)
::------------------------------------------------------------------------------------------------
MD "%V_Year%.%V_Month%.%V_Day% - %NewFolder%\IMAGES\RAW IMAGES"
MD "%V_Year%.%V_Month%.%V_Day% - %NewFolder%\IMAGES\EXPORTS\JPG"
MD "%V_Year%.%V_Month%.%V_Day% - %NewFolder%\IMAGES\EXPORTS\PNG"
MD "%V_Year%.%V_Month%.%V_Day% - %NewFolder%\IMAGES\EXPORTS\PSD"
MD "%V_Year%.%V_Month%.%V_Day% - %NewFolder%\VIDEO\RAW VIDEO"
MD "%V_Year%.%V_Month%.%V_Day% - %NewFolder%\VIDEO\EDITS"
MD "%V_Year%.%V_Month%.%V_Day% - %NewFolder%\VIDEO\EXPORTS"

解决方案

If [%NewFolder%]==[] Goto AskForFolderName

let's disassemble this line, assuming %newfolder% is My Documents:

If [My Documents]==[] Goto AskForFolderName

if syntax is if argument1 comparison argument2 command

argument1 is [My, comparison is Documents], wait... What!? Syntax Error!

The batch-way of handling spaces is enclosing the string in qoutes:

If "%NewFolder%"=="" Goto AskForFolderName

argument1 is "My Documents", comparison is ==, argument2 is "", command is Goto ... - syntax ok.

(Interesting detail: in the very next line (if exist ...) you use the string quoted correctly)

这篇关于如何使用一套功能批处理脚本接受空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆