【问题标题】:Passing a Variable to Subfolder将变量传递给子文件夹
【发布时间】:2016-07-20 12:41:56
【问题描述】:

我有点卡在这里。 运行一个包含几个步骤的批处理文件。 我需要在一个文件夹中运行一个批处理文件,遍历每个文件夹,然后 3 个文件夹向下分配一个变量。因此,将第三个文件夹复制到每个文件夹中 所以我的文件夹结构

例如:2135698563325\Folder1\Folder2\Folder3\c007

eg2: 21356486543248\Folder1\Folder2\Folder3\c111

REM get c007 as a variable to be able to set a folder name
set variable = Folder0\Folder1\Folder2\Folder3\%Variable%

REM Step 2:Check that the c007 folder doens't already exist
if %Variable%==\\hippo\Folder4\ ((echo "Error: Duplicate Folder"):eof) Else mkdir \\hippo\Folder4\%Variable%

REM Step 3:Copy a default File Structure from Template Dir
xCopy /s \\hippo\production\Folder4\Temaplate \\hippo\production\Folder4\%Variable%
Rem Step 4: Copy the contents of c007 in to Folder6
xCopy /s %Variable% \\hippo\production\Folder4\Variable\Folder5\Folder6\

这样更有意义吗?

【问题讨论】:

  • 在你的所有代码中都应该是 %variable% 而不是 variable。
  • 好的,所以我可以更改它,但仍然不确定如何遍历每个文件夹并将第三个文件夹向下分配一个变量?
  • 您能否编辑您的帖子,并举例说明您获得了什么以及您需要什么?我在这里有点迷路了。例如,您的脚本的第二行不能真正按原样工作。
  • 嗨,Jean,我无法启动脚本,因为我不知道该怎么做。
  • 我需要我的脚本从根文件夹中获取子文件夹的名称。例如: 2135698563325\Folder1\Folder2\Folder3\c007 其中 c007 将更改然后在 \\hippo\production\Folder4\c007 然后创建该文件夹,如果它不存在,但从模板目录创建文件夹结构

标签: batch-file command-line command


【解决方案1】:

这是关于如何获取c007 和c111 的批注脚本。将您的代码放入子程序ProcessFolder。文件夹名称c007 和c111 通过%~1 引用的第一个参数传递给子例程。

@echo off
setlocal EnableExtensions

rem Get current directory path always without backslash at end. Environment
rem variable CD holds the current directory path usually without backslash
rem at end. But current directory string is with backslash at end if the
rem current directory is the root directory of a drive.
if "%CD:~-1%" == "\" (
    set "CurrentFolder=%CD:~0,-1%"
) else (
    set "CurrentFolder=%CD%"
)

rem Process the folder names 4 levels below current directory.
for /D %%A in ("%CurrentFolder%\*") do (
    for /D %%B in ("%%A\*") do (
        for /D %%C in ("%%B\*") do (
            for /D %%D in ("%%C\*") do (
                for /D %%E in ("%%D\*") do (
                    call :ProcessFolder "%%~nxE"
                )
            )
        )
    )
)

endlocal
rem Avoid a fall through to the subroutine by exiting batch processing here.
goto :EOF

rem Subroutine to process the folder with name found above.

:ProcessFolder
echo Folder variable is: %~1

rem Exit subroutine and continue in the most inner FOR loop above.
goto :EOF

要了解所使用的命令及其工作原理,请打开命令提示符窗口,在其中执行以下命令,并仔细阅读每个命令显示的所有帮助页面。

  • call /?
  • echo /?
  • endlocal /?
  • for /?
  • goto /?
  • if /?
  • rem /?
  • set /?
  • setlocal /?

【讨论】:

    猜你喜欢
    • 2014-09-30
    • 1970-01-01
    • 1970-01-01
    • 2017-05-30
    • 2018-08-07
    • 1970-01-01
    • 1970-01-01
    • 2011-01-11
    • 2019-05-30
    相关资源
    最近更新 更多