分享几个实用的批处理脚本

1. 批处理删除远程共享目录7天以上的文件和空文件夹

net use * /del /yes

NET USE X: \10.29.48.12sharesTest password /user:DOMAIN1account

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%sharesDebugReport*.*

ping 127.0.0.1 -n 30 >nul

for /f "delims=" %%a in ('dir /s /b /ad "\%FILE_SERVER%sharesDebugReport"') 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.243d$ password /user:MSDOMAIN1doautotester

set sourcePath="Y:DOAutomationTestautomation_do_scriptlibrary_soapuiCompare Test and Benchmark EnvironmentVersion 1direct"

set targetPath="C:Program FilesSmartBearReadyAPI-1.9.0inscriptsdirect"

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.95d$ Autotest123 /user:SZDOMAIN1autotester

set AutoPath=%~dp0

%AutoPath:~0,2% 

pushd %AutoPath%

cd /d %AutoPath%

set sourcePath=%AutoPath%TestResultPA

set targetPath=X:\AutomationReportPA

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%system32driversetchosts & echo xxx.xxx.xxx.xx  test_host >> %WINDIR%system32driversetchosts

6. 批处理自动修改区域和语言选项

open a cmd window and type reg query "HKCUControl PanelInternational" which will show you the values as you want them.

Then to modify them, use REG ADD "HKCUControl PanelInternational" /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 "HKCUControl PanelInternational REG ADD "HKCUControl PanelInternational" /t REG_SZ /v LocaleName /d en-GB /f REG ADD "HKCUControl PanelInternational" /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 msdomain1jzhang6 /p jzhang6'spassword

在机器SZPCWIN2K801上建一个计划任务,就可以在别的机器上通过这个命令来启动这个计划任务

停止

schtasks /end /tn "IPADForAdvisor_QA_APITest" /s SZPCWIN2K801 /u msdomain1jzhang6 /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:UsersmsautotestuserAppDataLocalTemp")

'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