2016-11-08 96 views
0

請原諒我對.bat文件的無知,但我不習慣它們。 我想要做的是獲得一個.bat文件來搜索特定的文件名,然後執行該.txt文件的搜索以找到該文件的特定所有者。如果全部匹配,則將該文件移到另一個目錄。.Bat文件找到一個特定的文件名和所有者並移動

這是可能的,我怎麼開始? 我很熟悉使用JS編程,但僅此而已。 以前從未創建批處理文件,但聽到他們所有的時間奇蹟。

*****這是我迄今爲止收集的...並且無法使其工作。您還會注意到,我不知道如何告訴它搜索該文件的特定所有者...即(Jane Doe)和(測試)的文件名,如果全部匹配,則移動到另一個目錄*** **

@echo OFF 
setlocal enableextensions disabledelayedexpansion 

set "source=C:\Users\andrew.moss\Desktop\Test1" 
set "target=C:\Users\andrew.moss\Desktop\Test2" 
set "searchString=Testing" 

set "found=" 
for /f "delims=" %%a in (' 
    findstr /m /i /l /c:"%Testing%" "%C:\Users\andrew.moss\Desktop\Test1%" 2^>nul 
') do (
    if not defined found set "found=1" 
    echo move "%%a" "%C:\Users\andrew.moss\Desktop\Test2%" 
) 

if not defined found (
    echo Failure 
) 

pause 
+0

這不是教程網站,所以你應該分享你的努力。一個好的起點是['dir'](http://ss64.com/nt/dir.html)命令 - 在新的命令提示符窗口中鍵入'help dir'或'dir /?'並閱讀幫助非常仔細... – aschipfl

+0

當我運行我迄今爲止所有我得到的是'失敗' – Drew

+0

你在'%%'中包含了兩個文件路徑,但這些文件應該被刪除...要獲得所有者,你可以解析通過'for/F'循環輸出'dir/Q',但這可能會有點棘手......不知道是否存在依賴'wmic'的方法... – aschipfl

回答

0

我從頭開始提供的腳本,我相信適合您的需求,所以這裏是:

@echo off 
setlocal EnableExtensions DisableDelayedExpansion 

rem // Define constants here: 
set "_SOURCE=."  & rem // (path to directory where to search for files) 
set "_TARGET=.."  & rem // (path to directory where to move found files) 
set "_PATTERN=test.*" & rem // (file name pattern; may contain wild-cards) 
set "$RECURSIVE=#" & rem // (set to non-empty value to search sub-dir.s) 
set "$OWNER=aschipfl" & rem /* (login name of owner; may be preceded with 
         rem  domain and a backslash, like `DOMAIN\`) */ 

rem // Predefine recursive option: 
if defined $RECURSIVE set "$RECURSIVE=/S" 
rem // Split domain off of owner: 
for /F "tokens=1-2 delims=\ eol=\ " %%C in ("%$OWNER%") do (
    if not "%%D"=="" (
     set "$DOMAIN=%%C" 
     set "$OWNER=%%D" 
    ) else set "$DOMAIN=" 
) 

rem /* Loop through all matching files in the given location found by `dir`; 
rem the `/Q` option lets include domain/owner in the output; `findstr` 
rem filters out lines beginning with ` `, hence headers and footers: */ 
pushd "%_SOURCE%" || exit /B 1 
for /F "tokens=4,*" %%E in (' 
    dir /Q /N /-C %$RECURSIVE% /A:-D "%_PATTERN%"^
     ^| findstr /V /B /C:" " 
') do (
    rem // Reset flag: 
    set "FLAG=" 
    rem // Check whether certain domain has been specified: 
    if defined $DOMAIN (
     rem // Domain specified, so domain and owner must match: 
     if /I "%%E"=="%$DOMAIN%\%$OWNER%" (
      rem // Match encountered, hence set flag: 
      set "FLAG=#" 
     ) 
    ) else (
     rem // Domain not specified, so owner must match only: 
     set "OWN=%%E" 
     setlocal EnableDelayedExpansion 
     rem // Dismiss domain of currently iterated file item: 
     set "OWN=!OWN:*\=!" 
     rem // Check whether owner matches: 
     if "!OWN!"=="%$OWNER%" (
      endlocal 
      rem // Match encountered, hence set flag: 
      set "FLAG=#" 
     ) else endlocal 
    ) 
    rem // Check whether flag is set: 
    if defined FLAG (
     rem /* Flag is set, hence do something with current file item; 
     rem here, the file is moved to the given target directory; 
     rem remove the `if exist` query to force overwriting: */ 
     if not exist "%_TARGET%\%%~nxF" (
      > nul move /Y "%%~fF" "%_TARGET%\" 
     ) 
    ) 
    endlocal 
) 
popd 

endlocal 
exit /B 

有很多解釋性發言。核心命令是dir /Q,它能夠返回文件的所有者。請注意,它會返回它們的登錄名,前面帶有域名和一個反斜槓,例如DOMAIN\jdoe。因此,您需要在腳本中以這種方式指定擁有者。您可以省略域前綴,在這種情況下,該域將被忽略。但是,例如,您不能指定顯示用戶名稱,例如Jane Doe,因爲這不會被識別。

請注意,如果給定的登錄名包含空格,腳本將會失敗。

+0

請注意,初始[修訂1](http://stackoverflow.com/revisions/40505600/1)不起作用,因爲它是一個錯誤; [修訂2](http://stackoverflow.com/revisions/40505600/2)可以工作,但不會移動任何文件,只是爲了演示而回應它們; [修訂3](http://stackoverflow.com/revisions/40505600/3)終於移動找到的文件...我建議關閉/刪除[其他問題](http://stackoverflow.com/q/40516634 )與此腳本的初始修訂... – aschipfl

+0

非常感謝您以您的方式提供幫助。我非常感謝。 我關閉了另一個問題。 我試過修訂3,它現在告訴我「系統找不到指定的文件」 – Drew

+0

讓我們[在聊天中繼續討論](http://chat.stackoverflow.com/rooms/127738/discussion-between-drew-和aschipfl)。 – aschipfl

相關問題