分享幾個實用的批處理腳本


1. 批處理刪除遠程共享目錄7天以上的文件和空文件夾

 

net use * /del /yes

NET USE X: \\10.29.48.12\shares\Test password /user:DOMAIN1\account

 

set AutoPath=X:\

%AutoPath:~0,2%

pushd %AutoPath%

cd /d %AutoPath%

 

rem delete reports generated 7 days ago

forfiles /p %AutoPath% /s /m *.* /d -7 /c "cmd /c del /f @path">nul 2>nul

ping 127.0.0.1 -n 15 > nul

 

for /f "tokens=*" %%a in ('dir /b /ad /s^|sort /r') do rd "%%a" 2>nul

ping 127.0.0.1 -n 15 > nul

exit 0

 

2. 批處理命令刪除目錄以及目錄下的子文件夾和文件

 

@echo clean DebugReport

del /q /s /f \\%FILE_SERVER%\shares\DebugReport\*.*

ping 127.0.0.1 -n 30 >nul

 

for /f "delims=" %%a in ('dir /s /b /ad "\\%FILE_SERVER%\shares\DebugReport"') do (rd /q "%%a" 2>nul && echo Deleted "%%a")

ping 127.0.0.1 -n 10 >nul

exit 0

 

3. 拷貝遠程服務器共享文件到本地

net use * /del /yes

NET USE Y: \\10.86.17.243\d$ password /user:MSDOMAIN1\doautotester

 

set sourcePath="Y:\DOAutomationTest\automation_do_scriptlibrary_soapui\Compare Test and Benchmark Environment\Version 1\direct"

set targetPath="C:\Program Files\SmartBear\ReadyAPI-1.9.0\bin\scripts\direct"

 

pushd %sourcePath%

xcopy %sourcePath%\*.groovy %targetPath% /R /Y

net use * /del /yes

pause

 

4. 以當前時間為名創建文件夾,將本地文件夾里的文件拷貝到遠程共享目錄,而且保證本地和Jenkins上運行都成功

@echo off

 

rem connect to szotpc801

net use * /del /yes

NET USE X: \\10.66.234.95\d$ Autotest123 /user:SZDOMAIN1\autotester

 

set AutoPath=%~dp0

%AutoPath:~0,2% 

pushd %AutoPath%

 

cd /d %AutoPath%

 

set sourcePath=%AutoPath%\TestResult\PA

set targetPath=X:\\AutomationReport\PA

 

set tempStr=%date:/=-%-%time::=-%

set directoryName=%tempStr: =-%

echo I will create a directory : %directoryName%

 

if exist %targetPath%\%directoryName% (echo y|cacls %targetPath%\%directoryName% /p everyone:f >nul 2>nul &&rd /s /q %targetPath%\%directoryName%) else echo directory doesn't exist,create it

 

md %targetPath%\%directoryName%

xcopy /d %sourcePath% %targetPath%\%directoryName%

 

rem delete reports generated 7 days ago

forfiles /p %targetPath% /s /m *.* /d -7 /c "cmd /c del /f @path">nul 2>nul

for /f "tokens=*" %%a in ('dir /b /ad /s^|sort /r') do rd "%%a" 2>nul

 

net use * /del /yes

exit 0

 

5. 通過批處理加host

echo. >> %WINDIR%\system32\drivers\etc\hosts & echo xxx.xxx.xxx.xx  test_host >> %WINDIR%\system32\drivers\etc\hosts

 

6. 批處理自動修改區域和語言選項

 

open a cmd window and type reg query "HKCU\Control Panel\International" which will show you the values as you want them.

Then to modify them, use REG ADD "HKCU\Control Panel\International" /t REG_SZ /v LocaleName /d es-Mx /f for each value replacing what is after /v with the appropriate name and what is after /d with the appropriate value.

 

For example:

reg query "HKCU\Control Panel\International REG ADD "HKCU\Control Panel\International" /t REG_SZ /v LocaleName /d en-GB /f REG ADD "HKCU\Control Panel\International" /t REG_SZ /v sCountry /d "United Kingdom" /f

 

7. 通過命令行窗口重啟或關閉遠程電腦

 

在命令行窗口輸入“shutdown -s -t 0”, 立刻關機

在命令行窗口輸入“shutdown -r -t 0”, 立刻重啟

 

8. 遠程執行或停止服務器上的計划任務

 

執行

schtasks /run /tn "IPADForAdvisor_QA_APITest" /s SZPCWIN2K801 /u msdomain1\jzhang6 /p jzhang6'spassword

在機器SZPCWIN2K801上建一個計划任務,就可以在別的機器上通過這個命令來啟動這個計划任務

 

停止

schtasks /end /tn "IPADForAdvisor_QA_APITest" /s SZPCWIN2K801 /u msdomain1\jzhang6 /p jzhang6'spassword

 

9. 查找進程並殺掉

tasklist|findstr /i NRobotRemoteConsole.exe && taskkill /f /im NRobotRemoteConsole.exe exit 0

 

10. VBScript自動刪除2小時以前生成的文件

將下面的代碼保存為deleteTempFiles.vbs,雙擊即可運行(縮進效果見附圖)

 

dim folder, file, mFSO, subfolder

 

Set mFSO = CreateObject("Scripting.FileSystemObject")

set folder=mFSO.GetFolder("C:\Users\msautotestuser\AppData\Local\Temp")

'Delete files

dim df

For Each file In folder.files

'df=DateDiff("h",file.DateCreated,Now)    'Create Date

df=DateDiff("h",file.DateLastModified,Now)   'Modify Date

If (df>2) Then    '2 hours ago

'MsgBox folder.path & "\" & file.Name & vbTab & file.DateCreated

'MsgBox folder.path & "\" & file.Name & vbTab & file.DateLastModified

On Error Resume Next

file.Delete()

End If

Next

'Delete folders

set subfolder = Folder.subFolders

For Each file In subfolder

'df=DateDiff("h",file.DateCreated,Now)    'Create Date

df=DateDiff("h",file.DateLastModified,Now)    'Modify Date

If (df>2) Then    '2 hours ago

On Error Resume Next

mFSO.deleteFolder(folder.path & "\" & file.Name)

end if

next

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM