【问题标题】:Create file for every script instance为每个脚本实例创建文件
【发布时间】:2016-01-28 09:58:19
【问题描述】:

我创建了下面的脚本,以使用 linux 工具 DU 获取 C:\Users 的文件夹大小。该脚本通过 GPO 分发给所有用户。

xcopy \\0.0.0.0\netlogon\gnu-core-utils\du.exe C:\profile-size-script\ /i /s /Y
xcopy \\0.0.0.0\netlogon\gnu-core-utils\libintl3.dll C:\profile-size-script\ /i /s /Y
xcopy \\0.0.0.0\netlogon\gnu-core-utils\libiconv2.dll C:\profile-size-script\ /i /s /Y
C:
cd C:\profile-size-script
du -hs C:\users\* > \\0.0.0.0\userfolder\%username%.txt

行解释如下:

  1. 将名为 du.exe 的文件复制到位置 C:\profile-size-script
  2. 将名为 libintl3.dll 的文件复制到同一位置。
  3. 复制名为 libiconv2.dll 的文件
  4. 更改为驱动器 C:以防 cmd 从不同的驱动器号开始。
  5. 更改为相同的文件夹大小
  6. 使用 DU 获取文件夹大小并将输出回显到 \0.0.0.0\userfolder\%username%.txt。

问题是这个脚本不是为每个用户创建一个新文件,而是附加第一个创建的文件 %username%.txt

【问题讨论】:

    标签: windows batch-file command-prompt


    【解决方案1】:

    我认为你正在寻找这样的事情:

    xcopy \\0.0.0.0\netlogon\gnu-core-utils\du.exe C:\profile-size-script\ /i /s /Y
    xcopy \\0.0.0.0\netlogon\gnu-core-utils\libintl3.dll C:\profile-size-script\ /i /s /Y
    xcopy \\0.0.0.0\netlogon\gnu-core-utils\libiconv2.dll C:\profile-size-script\ /i /s /Y
    cd /d C:\profile-size-script
    for /d %%u in (c:\users\*) do (du -hs C:\users\* > \\0.0.0.0\userfolder\%u%.txt)
    

    FOR /D 循环读取每个文件夹并为它找到的每个文件夹 (%%u) 执行您的 du.exe 命令。查看FOR /? 了解更多信息和其他可能性。

    附加说明:CD /D 命令允许您同时更改驱动器号和目录。或者,如果您运行命令而不使用显式路径更改目录,则可以完全跳过 C:CD 行,如下所示:

    xcopy...
    for /d %%u in (c:\users\*) do (
        C:\profile-size-script\du.exe -hs C:\users\* > \\0.0.0.0\userfolder\%u%.txt)
    

    【讨论】:

    • 非常感谢您的回复韦斯。我确实使用了CD /D,但是我使用我的解决方案解决了我的问题。
    【解决方案2】:

    我通过创建一个新文件解决了这个问题,因为建议的答案是为 C:\users* 中的每个文件夹创建一个 txt 文件。

    xcopy \\0.0.0.0\netlogon\gnu-core-utils\du.exe C:\profile-size-script\ /i /s /Y
    xcopy \\0.0.0.0\netlogon\gnu-core-utils\libintl3.dll C:\profile-size-script\ /i /s /Y
    xcopy \\0.0.0.0\netlogon\gnu-core-utils\libiconv2.dll C:\profile-size-script\ /i /s /Y
    cd /d C:\profile-size-script
    copy NUL \\0.0.0.0\userfolder\%username%.txt
    du -hs C:\users\* > \\0.0.0.0\userfolder\%username%.txt
    

    每次运行脚本时,它都会复制一个 NUL 文件并根据登录的用户对其进行重命名,这对我来说很有效。但是,我确实将 C: 替换为 cd /d(这会更改驱动器和目录)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-05-20
      • 1970-01-01
      • 2014-06-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多