VB.net 2010 视频教程 VB.net 2010 视频教程 python基础视频教程
SQL Server 2008 视频教程 c#入门经典教程 Visual Basic从门到精通视频教程
当前位置:
首页 > 编程开发 > 批处理教程 >
  • 分享几个实用的批处理脚本

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

 

出处:https://www.cnblogs.com/MasterMonkInTemple/p/12144605.html

 


相关教程