如何使我的用户输入作为一个变量来使文件夹 [英] How to make my user input as a variable to make a folder

查看:127
本文介绍了如何使我的用户输入作为一个变量来使文件夹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

(三)ToTheMaker

我在这里发现了这个code和我要去使用它,但唯一的问题是我希望它有一个用户的输入,将创建文件夹

例如:输入文件夹的数量:

其中用户意愿输入将被用作将创建的文件夹的可变的值。我怎么做?

  @ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSIONSET GROUPSIZE = 10
SET N = 1
SET NF = 0FOR %%在f IN(* .TXT)DO(
    IF!N!== 1(
        SET / A + NF = 1
        MD Cake_!NF!
    )    移动/ Y%% FCake_!NF!    IF!N!==!GROUPSIZE! (
        SET N = 1
    )ELSE(
        SET / A N + = 1
    )

ENDLOCAL
暂停


解决方案

在命令提示符窗口中执行任何帮助设置设置/ <? / code>结果打印几个帮助页面到控制台窗口的命令设置应仔细阅读。帮助也说明设置/ P 用于提示用户输入一个值或字符串。

 关闭@echo
集FOLDERCOUNT = 1
集/ PFOLDERCOUNT =输入文件夹的数量(默认:%FOLDERCOUNT%):
对/ L %% N的(1,1,%FOLDERCOUNT%)做MD文件夹%% N
设置FOLDERCOUNT =

这个小批量code定义环境变量 FOLDERCOUNT 值为1的缺省值是用来当用户点击刚刚键<大骨节病>返回或<大骨节病> ENTER 的提示。

该用户下一步的文件夹创建的数量要求。用户输入的字符串被分配到环境变量 FOLDERCOUNT 。用户希望输入一个正数,而不是不同的东西。

循环创建一个名为文件夹并追加了当前数字是自动1在每个循环运行增量在当前目录下的文件夹开始值为1。

最后一行删除环境变量 FOLDERCOUNT 没有必要了。

有关理解使用的命令以及它们如何工作,打开命令提示符窗口中,执行有下面的命令,并阅读完全针对每个命令显示的所有帮助页面非常谨慎。


  • 回声/?

  • 为/?

  • MD /?

  • 设置/?

编辑:最后一批code与移动的文本文件,也是整个任务

 关闭@echo
需要在循环变量FolderIndex REM延迟扩展。
REM命令SETLOCAL还创建了一个新的环境变量
REM表复制现有的所有变量的新表。
SETLOCAL EnableDelayedExpansionREM询问批用户的子文件夹的数量来创建和创建它们。
集FOLDERCOUNT = 1
集/ PFOLDERCOUNT =输入文件夹的数量(默认:%FOLDERCOUNT%):
对/ L %% N的(1,1,%FOLDERCOUNT%)做MD文件夹%% NREM从当前文件夹移动所有* .txt文件到创建子文件夹。
集FolderIndex = 0
为%% f由于(* .txt)中做(
    集/ A FolderIndex + = 1
    移动/ Y%%〜F,文件夹!FolderIndex!\\ %%〜NXF
    如果!FolderIndex! ==%FOLDERCOUNT%设定为FolderIndex = 0
)REM恢复导致破坏previous环境
REM与FolderIndex和FOLDERCOUNT当前的环境下表。
ENDLOCAL

(c)ToTheMaker

I found this code here and I'm going to use it but the only problems is I want it to have a user input that will create the folders

For example: "Enter number of folders:"

The value which the user will input will be used as a variable that will create the folders. How am I going to do that?

@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION

SET groupsize=10
SET n=1
SET nf=0

FOR %%f IN (*.txt) DO (
    IF !n!==1 (
        SET /A nf+=1
        MD Cake_!nf!
    )

    MOVE /Y "%%f" Cake_!nf!

    IF !n!==!groupsize! (
        SET n=1
    ) ELSE (
        SET /A n+=1
    )
)
ENDLOCAL
PAUSE

解决方案

Executing in a command prompt window either help set or set /? results in printing several help pages into the console window for command SET which should be read carefully. The help explains also set /P for prompting user for a value or string.

@echo off
set "FolderCount=1"
set /P "FolderCount=Enter number of folders (default: %FolderCount%): "
for /L %%N in (1,1,%FolderCount%) do md "Folder%%N"
set "FolderCount="

This little batch code defines the environment variable FolderCount with value 1 as default value which is used when the user hits just key RETURN or ENTER on prompt.

The user is asked next for the number of folders to create. The string entered by the user is assigned to environment variable FolderCount. The user hopefully enters a positive number and not something different.

The FOR loop creates the folders in current directory with name Folder and the current number appended which is automatically incremented by 1 on each loop run starting with value 1.

The last line deletes the environment variable FolderCount not needed anymore.

For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.

  • echo /?
  • for /?
  • md /?
  • set /?

Edit: The final batch code for the entire task with moving the text files, too.

@echo off
rem Delayed expansion required for variable FolderIndex in FOR loop.
rem Command setlocal additionally creates a new environment variable
rem table with copying all existing variables to the new table.
setlocal EnableDelayedExpansion

rem Ask the batch user for number of subfolders to create and create them.
set "FolderCount=1"
set /P "FolderCount=Enter number of folders (default: %FolderCount%): "
for /L %%N in (1,1,%FolderCount%) do md "Folder%%N"

rem Move all *.txt files from current folder into the created subfolders.
set "FolderIndex=0"
for %%F in (*.txt) do (
    set /A FolderIndex+=1
    move /Y "%%~F" "Folder!FolderIndex!\%%~nxF"
    if !FolderIndex! == %FolderCount% set "FolderIndex=0"
)

rem Restore previous environment which results in destroying
rem current environment table with FolderIndex and FolderCount.
endlocal

这篇关于如何使我的用户输入作为一个变量来使文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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