AD用户文件夹清理批处理脚本

发布于 2024-11-27 23:02:07 字数 383 浏览 4 评论 0原文

我有一个批处理文件,我一直在尝试创建该文件,该文件将读取两个 csv 文件,对它们进行比较并根据差异执行操作。第一个文件是广告用户列表,第二个文件是我们 nas 上的广告用户文件夹列表。

我想要一个批处理文件来比较它们,如果有广告用户没有文件夹,请创建该文件夹。如果存在没有用户的文件夹,请删除该文件夹。但我也希望能够在让它执行操作之前看到比较结果,以确保一切正确。

IE

  • 广告用户文件

    <前><代码>汤姆 山姆 乔治 莎莉
  • 文件夹文件

    <前><代码>汤姆 乔治 莎莉 账单

Sam 没有文件夹,所以创建它,Bill 有一个文件夹,但没有用户,所以删除它。

I have a batch file I have been trying to create that would read two csv files compare them and do an action based on the differences. First file is a list of ad users and second file is a list of ad user folders on our nas.

I would like a batch file that would compare them and if there is a ad user that does not have a folder, create the folder. If there is a folder that does not have a user delete the folder. But I also would like to be able to see the results of the comparison before I let it perform the actions to make sure all is correct.

I.E.

  • ad user file

    Tom
    Sam
    George
    Sally
    
  • folder file

    Tom
    George
    Sally
    Bill
    

There is no folder for Sam so create it, and there is a folder for Bill but no user so delete it.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

残龙傲雪 2024-12-04 23:02:07

如果不能立即解决问题,这样的事情至少可以作为一个好的起点:

@ECHO OFF
SET "adusers=adusers.txt"
SET "folders=folders.txt"

COPY NUL "%adusers%.tmp" >NUL
COPY "%folders%" "%folders%.tmp" >NUL

FOR /F "usebackq delims=" %%U IN ("%adusers%") DO CALL :process "%%U"

ECHO Users without folders:
CALL :type "%adusers%.tmp"
ECHO.
ECHO Folders without users:
CALL :type "%folders%.tmp"
ECHO.
PAUSE

FOR /F "usebackq delims=" %%U IN ("%adusers%.tmp") DO CALL :addfolder "%%U"
FOR /F "usebackq delims=" %%U IN ("%folders%.tmp") DO CALL :delfolder "%%U"

DEL "%adusers%.tmp"
DEL "%folders%.tmp"
GOTO :EOF

:process
FIND %1 NUL || (ECHO %~1>>"%adusers%.tmp" & GOTO :EOF)
FIND /V %1 "%folders%.tmp.$$"
DEL "%folders%.tmp"
RENAME "%folders%.tmp.$$" "%folders%.tmp"
GOTO :EOF

:type
IF %~z1 == 0 (ECHO ^(none^)) ELSE (type %1)
GOTO :EOF

:addfolder
script to create the folder
GOTO :EOF

:delfolder
script to delete the folder
GOTO :EOF

Something like this might at least serve as a good starting point, if not immediately solve the problem:

@ECHO OFF
SET "adusers=adusers.txt"
SET "folders=folders.txt"

COPY NUL "%adusers%.tmp" >NUL
COPY "%folders%" "%folders%.tmp" >NUL

FOR /F "usebackq delims=" %%U IN ("%adusers%") DO CALL :process "%%U"

ECHO Users without folders:
CALL :type "%adusers%.tmp"
ECHO.
ECHO Folders without users:
CALL :type "%folders%.tmp"
ECHO.
PAUSE

FOR /F "usebackq delims=" %%U IN ("%adusers%.tmp") DO CALL :addfolder "%%U"
FOR /F "usebackq delims=" %%U IN ("%folders%.tmp") DO CALL :delfolder "%%U"

DEL "%adusers%.tmp"
DEL "%folders%.tmp"
GOTO :EOF

:process
FIND %1 NUL || (ECHO %~1>>"%adusers%.tmp" & GOTO :EOF)
FIND /V %1 "%folders%.tmp.$$"
DEL "%folders%.tmp"
RENAME "%folders%.tmp.$$" "%folders%.tmp"
GOTO :EOF

:type
IF %~z1 == 0 (ECHO ^(none^)) ELSE (type %1)
GOTO :EOF

:addfolder
script to create the folder
GOTO :EOF

:delfolder
script to delete the folder
GOTO :EOF
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文