問題描述
查找 pdfs/doc/docx/etc 的批處理文件 (Batch file to look for pdfs/doc/docx/etc)
尋求腳本的幫助,以在 c:\users\ 下的所有文件夾中搜索 .pdf、.doc、.docx 等。我正在清理一些帶有文件的計算機,但我不想刪除用戶文件夾大部分只是他們的內容。這適用於用戶不應將任何內容保存到硬盤驅動器的醫師辦公室,我已實施不保存政策,但希望關閉並保存現有數據以備將來參考。
將在 50 多台計算機上的服務器上使用 psexec 運行。如果可能的話,我希望輸出是 \servername\destinationfolder\computername\username\file.pdf 或類似的東西,所以我知道文檔來自哪台計算機和用戶。
我認為它看起來像像這樣使用 robocopy,但它似乎不起作用。<
參考解法
方法 1:
You're using the FOR
command incorrectly, but I think you don't need it at all. The following should cut it as it recurses through every subdirectory beneath c:\users
, collects the specified files (add extensions as you like) and then copies them to the specified destination.
ROBOCOPY c:\users \\servername\destinationfolder *.pdf *.doc *.docx /XJD /S
Edit: As it turned out, OP is using a newer version of Windows than XP and this fact requires the /XJD
switch to be added, which...
...will prevent Directory Junction Points from being parsed. This avoids a nasty recursion issue that can sometimes occur with Windows' "fake" directories such as "Documents and Settings"
Edit for completeness: Motivated by @aschipfl, here's a pointer on what's wrong with the FOR
command ‑ FOR /F
is used in a quite different way. As you wanted to iterate over all the directories of a single level of the filesystem, you could for example do:
FOR /D %%D IN ( c:\users\* ) DO ( ... )
But, when approaching the task like this, you cannot easily create the directory structure like ROBOCOPY
does without writing quite a lot of code, and that's just unnecessary when the solution can be as simple as the one above.