如何通过五个简单步骤创建批处理(BAT)文件

是否要创建批处理文件?

批处理文件是计算机操作员完成工作的一种方式。他们可以自动执行日常任务,缩短执行某项工作所需的时间,并将复杂的过程转换为任何人都可以操作的事情。

在本文中,您将学习如何编写简单的批处理文件。您将学习批处理文件可以做什么以及如何自己编写批处理文件的基础知识。我还将为您提供进一步的资源,以学习编写批处理(BAT)文件。

如何在Windows中创建批处理文件

在进入详细信息之前,这里有一个快速摘要

  • 打开文本文件,例如记事本或写字板文档。
  • 添加您的命令,从@echo [off]开头,然后以新行开头-标题[批处理脚本的标题],回显[第一行]和暂停。
  • 保存文件扩展名为 .bat 的文件,例如 test。 bat
  • 要运行您的批处理文件,请双击刚刚创建的BAT文件。
  • 要编辑您的批处理文件,请右键单击BAT文件并选择 Edit
  • 如果这太快了,或者您想了解有关命令及其用法的更多信息,请继续阅读!

    步骤1:创建BAT文件

    假设您经常遇到网络问题;您会在命令提示符下不断发现自己,输入 ipconfig 并ping通Google以解决网络问题。一段时间后,您意识到如果只编写一个简单的BAT文件,将其粘贴在USB记忆棒上,然后在进行故障排除的计算机上使用它,效率会更高。

    批处理文件可以简化使用Windows命令提示符重复执行计算机任务。下面是一个批处理文件的示例,该文件负责在命令提示符下显示一些文本。右键单击目录中的空白区域,然后依次选择 New Text Document ,创建一个新的BAT文件。

    双击此< strong>新文本文档以打开默认文本编辑器。将以下代码复制并粘贴到您的文本输入中。

    @echo offtitle This is your first batch script!echo Welcome to batch scripting!pause

    上面的脚本回显了文本“欢迎使用批处理脚本!"。通过转到文件另存为来保存文件,然后根据需要命名文件。以添加的 .bat 扩展名结尾-例如, welcome.bat -然后单击确定。这将完成批处理过程。现在,双击新创建的批处理文件将其激活。

    不要以为所有批处理脚本都可以。批处理脚本参数是命令提示符代码的调整版本,因此仅限于命令提示符可以执行的操作。对于不熟悉该程序的人,命令提示符具有很多功能。

    步骤2:了解批处理脚本的基础知识

    批处理文件使用与命令提示符相同的语言。您要做的只是告诉命令提示符您要通过文件输入的内容,而不是在命令提示符下键入该文件。这样可以节省您的时间和精力。它还允许您放入一些逻辑(例如简单的循环,条件语句等,过程编程可以在概念上进行操作)。

    @echo:在命令提示符下查看您的工作脚本。此参数对于查看您的工作代码很有用。如果批处理文件出现任何问题,您将能够使用echo函数查看与脚本相关的问题。在此参数后添加 off ,可让您在脚本完成后快速关闭脚本。

    标题: HTML中的标记,它将在“命令提示符"窗口中为您的批处理脚本提供标题。</p><p><strong> cls:</strong>清除命令提示符,最好在多余的代码可以使用时使用</p><p><strong> rem:</strong><strong>备注</strong>的简写提供与<strong><!– </strong>相同的功能。 > HTML标记。 Rem语句未输入到您的代码中。而是使用它们来解释和提供有关代码的信息。</p><p><strong> %% a:</strong>文件夹中的每个文件。</p><p><strong>(“ 。\"):</strong>根文件夹。使用命令提示符时,必须先将提示符定向到特定目录,然后再更改文件名,删除文件等。使用批处理文件,您只需要将.bat文件粘贴到您选择的目录中。</p><p><strong>暂停:</strong>允许中断.bat文件的逻辑链。这允许用户在继续执行代码之前先阅读命令行。短语“按任意键继续..."将表示暂停。</p><p><strong>开始“" [网站] </strong>:将使用默认的网络浏览器转到您选择的网站。 </p><p><strong> ipconfig:</strong>:这是一个经典的命令提示符参数,用于发布有关网络信息的信息。此信息包括MAC地址,IP地址和子网掩码。</p><p><strong> ping:</strong> ping IP地址,通过服务器路由发送数据包以评估其位置和延迟(响应至少可以说。</p><p>批处理变量的库很大。幸运的是,这里有一个Wikibook条目,其中包含大量的批处理脚本参数和变量库供您使用。</p><h2>第3步:编写和运行BAT文件</h2><p>我们将创建两个示例可以简化日常在线和离线活动的批处理脚本。</p><p>让我们创建一个立即可用的批处理脚本。如果您想在醒来时打开所有喜欢的新闻网站怎么办?由于批处理脚本使用命令提示符参数,因此我们可以创建一个在单个浏览器窗口中打开每个新闻媒体出口的脚本。</p><p>要重复执行批处理过程:首先,创建一个空的文本文件。 <strong>右键单击</strong>您选择的文件夹中的空白区域,然后依次选择<strong>新建</strong>和<strong>文本文档</strong>。打开文本文件后,输入以下脚本。我们的示例将提供在线提供的主要美国新闻媒体。</p><pre><code class="language-command-line">@echo offstart "" http://www.cnn.comstart "" http://www.abc.comstart "" http://www.msnbc.comstart "" http://www.bbc.comstart "" http://www.huffingtonpost.comstart "" http://www.aljazeera.comstart "" https://news.google.com/</code></pre><p>上面的脚本将一个<strong>开始“" </strong>参数堆叠在另一个之上,以打开多个选项卡。您可以用自己选择的链接替换提供的链接。输入脚本后,转到<strong>文件</strong>,然后依次<strong>另存为</strong>。在“另存为"窗口中,使用扩展名<strong> .bat </strong>保存文件,并将<strong>另存为类型</strong>参数更改为<strong>所有文件(*。*)</strong> 。</p><figure><img class="lazy" data-original="//static.pcpc.me/images/2016/11/save_as_bat.png"></figure><p>保存文件后,您只需<strong>双击BAT文件</strong>即可。随即,您的网页将打开。如果需要,可以将此文件放在桌面上。这样一来,您就可以一次访问所有喜欢的网站。</p><p>您每天都在下载多个文件,却只有数百个文件阻塞了您的“下载"文件夹吗?使用以下脚本创建一个批处理文件,该脚本按文件类型对文件进行排序。将.bat文件放到杂乱无章的文件夹中,然后双击运行。</p><pre><code class="language-command-line">@echo offrem For each file in your folderfor %%a in (".\*") do (rem check if the file has an extension and if it is not our scriptif "%%~xa" NEQ "" if "%%~dpxa" NEQ "%~dpx0" (rem check if extension folder exists, if not it is createdif not exist "%%~xa" mkdir "%%~xa"rem Move the file to directorymove "%%a" "%%~dpa%%~xa\"))</code></pre><p>以下是我的台式机示例,其中包含大量的图像文件。</p><figure><img class="lazy" data-original="//static.pcpc.me/images/2016/11/assorted_files.png"></figure><p>以下是这些相同的文件。</p><figure><img class="lazy" data-original="//static.pcpc.me/images/2016/11/ordered_files.png"></figure><p>就是这么简单。该批处理脚本还可以与任何类型的文件一起使用,无论是文档,视频还是音频文件。即使您的PC不支持文件格式,该脚本也会为您创建一个带有适当标签的文件夹。如果您的目录中已经有JPG或PNG文件夹,该脚本会将您的文件类型移动到相应的位置。</p><h2>使用批处理脚本自动执行简单填充</h2><p>了解批处理脚本必须提供的内容。如果您需要一遍又一遍地简单地做一些事情-无论是订购文件,打开多个网页,重命名文件还是创建重要文档的副本-您都可以使用批处理脚本简化繁琐的任务。</p><p>通过将IF语句合并到批处理脚本中,将事情做得更好。如果您也使用Linux,请查看使用<strong> df </strong>命令的最佳方法。</p> </div> <p> <small>标签: <a href="https://www.pcpc.me/tag/Batch-File/">批处理文件</a> <a href="https://www.pcpc.me/tag/Computer-Automation/">计算机自动化</a> <a href="https://www.pcpc.me/tag/Programming/">编程</a> </small></p> <div class="adslot aligncenter"><ins class="adsbygoogle" style="display:block" data-ad-client="ca-pub-3993128489863289" data-ad-slot="8218521392" data-ad-format="auto" data-full-width-responsive="true"></ins> <script> (adsbygoogle = window.adsbygoogle || []).push({}); </script></div> </div> <aside class="single-post-sidebar" style="display:flex;flex-direction:column;"> <div style="padding:20px;"><ins class="adsbygoogle" style="display:inline-block;width:300px;height:250px" data-ad-client="ca-pub-3993128489863289" data-ad-slot="6247798328"></ins> <script> (adsbygoogle = window.adsbygoogle || []).push({}); </script></div> <div class="article-sidebar"> <div class="flex-block"> <div class="flex-row"> <h3 class="article-sidebar-title">最新相关资讯</h3> <div class="bullet-posts"> <div class="bullet-row"> <div class="bullet-thumbnail"> <a href="https://www.pcpc.me/tech/windows-10-cant-open-jpg-files"> <img class="lazy-load lazy" data-original="https://static.pcpc.me/cover/2021/11/picture-of-laptop-running-windows-10.jpg?q=50&fit=contain&w=480&h=300&dpr=1.5"></a> </div> <div class="bullet-meta"> <h3> <a href="https://www.pcpc.me/tech/windows-10-cant-open-jpg-files">无法在 Windows 10 中打开 JPG 文件?这是修复方法</a></h3> <p>2021-12-20 03:15</p> </div> </div> <div class="bullet-row"> <div class="bullet-thumbnail"> <a href="https://www.pcpc.me/tech/windows-11-recycle-bin-taskbar-hotkey-shortcuts"> <img class="lazy-load lazy" data-original="https://static.pcpc.me/cover/2021/12/recycle-bin-cover-image.jpg?q=50&fit=contain&w=480&h=300&dpr=1.5"></a> </div> <div class="bullet-meta"> <h3> <a href="https://www.pcpc.me/tech/windows-11-recycle-bin-taskbar-hotkey-shortcuts">如何在 Windows 11 中为回收站创建任务栏和热键快捷方式</a></h3> <p>2021-12-20 02:15</p> </div> </div> <div class="bullet-row"> <div class="bullet-thumbnail"> <a href="https://www.pcpc.me/tech/windows-print-screen-fastest-screenshot-tool"> <img class="lazy-load lazy" data-original="https://static.pcpc.me/cover/2021/12/closeup-of-keyboard-print-screen.jpg?q=50&fit=contain&w=480&h=300&dpr=1.5"></a> </div> <div class="bullet-meta"> <h3> <a href="https://www.pcpc.me/tech/windows-print-screen-fastest-screenshot-tool">3 个修饰符使 Print Screen 成为您拥有的最快的屏幕截图工具</a></h3> <p>2021-12-19 05:15</p> </div> </div> <div class="bullet-row"> <div class="bullet-thumbnail"> <a href="https://www.pcpc.me/tech/windows-10-stop-installing-skype"> <img class="lazy-load lazy" data-original="https://static.pcpc.me/cover/2021/11/skype-logo.jpg?q=50&fit=contain&w=480&h=300&dpr=1.5"></a> </div> <div class="bullet-meta"> <h3> <a href="https://www.pcpc.me/tech/windows-10-stop-installing-skype">每次打开时如何阻止 Windows 10 安装 Skype</a></h3> <p>2021-12-19 02:15</p> </div> </div> <div class="bullet-row"> <div class="bullet-thumbnail"> <a href="https://www.pcpc.me/tech/windows-11-best-customization-apps"> <img class="lazy-load lazy" data-original="https://static.pcpc.me/cover/2021/12/how-to-turn-off-animation-effects-windows-11-1.jpg?q=50&fit=contain&w=480&h=300&dpr=1.5"></a> </div> <div class="bullet-meta"> <h3> <a href="https://www.pcpc.me/tech/windows-11-best-customization-apps">自定义 Windows 11 的 6 个最佳应用程序</a></h3> <p>2021-12-18 05:22</p> </div> </div> <div class="bullet-row"> <div class="bullet-thumbnail"> <a href="https://www.pcpc.me/tech/windows-10-cant-turn-on-developer-mode"> <img class="lazy-load lazy" data-original="https://static.pcpc.me/cover/2021/09/Python-developer-mistakes.jpg?q=50&fit=contain&w=480&h=300&dpr=1.5"></a> </div> <div class="bullet-meta"> <h3> <a href="https://www.pcpc.me/tech/windows-10-cant-turn-on-developer-mode">无法在 Windows 10 中打开开发人员模式?尝试这些修复</a></h3> <p>2021-12-18 04:15</p> </div> </div> <div class="bullet-row"> <div class="bullet-thumbnail"> <a href="https://www.pcpc.me/tech/windows-11-insider-preview-build-22523"> <img class="lazy-load lazy" data-original="https://static.pcpc.me/cover/2021/12/man-using-windows-11.jpg?q=50&fit=contain&w=480&h=300&dpr=1.5"></a> </div> <div class="bullet-meta"> <h3> <a href="https://www.pcpc.me/tech/windows-11-insider-preview-build-22523">Windows 11 的 2021 年最终内幕预览版包含哪些内容?</a></h3> <p>2021-12-18 01:05</p> </div> </div> <div class="bullet-row"> <div class="bullet-thumbnail"> <a href="https://www.pcpc.me/tech/windows-11-add-multiple-clocks"> <img class="lazy-load lazy" data-original="https://static.pcpc.me/cover/2021/12/time-management.jpg?q=50&fit=contain&w=480&h=300&dpr=1.5"></a> </div> <div class="bullet-meta"> <h3> <a href="https://www.pcpc.me/tech/windows-11-add-multiple-clocks">如何向 Windows 11 添加多个时钟</a></h3> <p>2021-12-17 03:15</p> </div> </div> <div class="bullet-row"> <div class="bullet-thumbnail"> <a href="https://www.pcpc.me/tech/windows-11-update-22504-best-parts"> <img class="lazy-load lazy" data-original="https://static.pcpc.me/cover/2021/11/windows-11-on-a-laptop.jpg?q=50&fit=contain&w=480&h=300&dpr=1.5"></a> </div> <div class="bullet-meta"> <h3> <a href="https://www.pcpc.me/tech/windows-11-update-22504-best-parts">如何使用 Windows 11 22504 更新的最佳部分 </a></h3> <p>2021-12-17 02:15</p> </div> </div> <div class="bullet-row"> <div class="bullet-thumbnail"> <a href="https://www.pcpc.me/tech/windows-10-we-couldnt-create-a-new-partition-error"> <img class="lazy-load lazy" data-original="https://static.pcpc.me/cover/2021/12/couldnt-create-a-new-partition-featured.jpg?q=50&fit=contain&w=480&h=300&dpr=1.5"></a> </div> <div class="bullet-meta"> <h3> <a href="https://www.pcpc.me/tech/windows-10-we-couldnt-create-a-new-partition-error">如何修复 Windows 10 中的“我们无法创建新分区”错误</a></h3> <p>2021-12-15 03:15</p> </div> </div> </div> </div> </div> </div> </aside> </div> </div> </div> <div class="single-post-transition-bar"> <div class="label-div" style="position:relative;min-height:100px;"> <span class="single-post-transition-label-wrap"> <span class="single-post-transition-label" style="display: inline-block;">分类本周热门</span></span> <svg class="single-post-transition-icon"> <use xlink:href="#icon-down"></use> </svg> </div> <div style="max-width: 1100px; margin: 0 auto;"> <div class="flexbox"> <div class="item"> <a href="https://www.pcpc.me/tech/fix-windows-10-kernel-power-error"> <img height="180" width="320" class="lazy-load lazy" data-original="https://static.pcpc.me/cover/2020/02/windows10-kernel-power-error-382x255.jpg"> <h4>如何通过5个简单步骤修复Windows 10内核电源错误</h4></a> </div> <div class="item"> <a href="https://www.pcpc.me/tech/screen-brightness-windows-10"> <img height="180" width="320" class="lazy-load lazy" data-original="https://static.pcpc.me/cover/2018/03/change-brightness-windows10-382x255.jpg"> <h4>在Windows 10中更改屏幕亮度的5种方法</h4></a> </div> <div class="item"> <a href="https://www.pcpc.me/tech/windows-sound-equalizers"> <img height="180" width="320" class="lazy-load lazy" data-original="https://static.pcpc.me/cover/2018/08/windows-10-sound-equalizers-382x255.jpg"> <h4>用于改善PC音频的5种最佳Windows 10声音均衡器</h4></a> </div> <div class="item"> <a href="https://www.pcpc.me/tech/ways-to-run-a-program-as-administrator-in-windows"> <img height="180" width="320" class="lazy-load lazy" data-original="https://static.pcpc.me/cover/2016/03/windows-10-homescreen-382x255.jpg"> <h4>在Windows中以管理员身份运行任何程序的4种方法</h4></a> </div> <div class="item"> <a href="https://www.pcpc.me/tech/windows-11-check-ram-gpu-cpu-usage"> <img height="180" width="320" class="lazy-load lazy" data-original="https://static.pcpc.me/cover/2021/12/task-manager-feature-imag.jpg?q=50&fit=contain&w=480&h=300&dpr=1.5"> <h4>如何在 Windows 11 中检查 RAM、GPU 和 CPU 使用情况</h4></a> </div> <div class="item"> <a href="https://www.pcpc.me/tech/spice-windows-sound-effects-amazing-resources"> <img height="180" width="320" class="lazy-load lazy" data-original="https://static.pcpc.me/cover/2015/01/windows-system-sounds-382x255.jpg"> <h4>下载Windows音效的5个最佳免费网站</h4></a> </div> <div class="item"> <a href="https://www.pcpc.me/tech/this-is-how-you-can-add-external-subtitles-on-windows-media-player"> <img height="180" width="320" class="lazy-load lazy" data-original="https://static.pcpc.me/cover/2016/10/windows-media-player-10-382x255.jpg"> <h4>这是在Windows Media Player上添加外部字幕的方法</h4></a> </div> <div class="item"> <a href="https://www.pcpc.me/tech/windows-10-stop-installing-skype"> <img height="180" width="320" class="lazy-load lazy" data-original="https://static.pcpc.me/cover/2021/11/skype-logo.jpg?q=50&fit=contain&w=480&h=300&dpr=1.5"> <h4>每次打开时如何阻止 Windows 10 安装 Skype</h4></a> </div> </div> </div> </div> </div> <footer class="footer-wrap"> <div class="footer-container"> <div class="footer-copyright"><img src="/static/images/logo.png" style="height:30px;display:inline;margin-bottom:-8px;"> © 2020 <a href="https://www.pcpc.me">双电网</a>(Www.PcPc.Me). All Rights Reserved. | <a href="/sitemap.xml">网站地图</a></div> </div> </footer> <script> (function(){ var bp = document.createElement('script'); var curProtocol = window.location.protocol.split(':')[0]; if (curProtocol === 'https') { bp.src = 'https://zz.bdstatic.com/linksubmit/push.js'; } else { bp.src = 'http://push.zhanzhang.baidu.com/push.js'; } var s = document.getElementsByTagName("script")[0]; s.parentNode.insertBefore(bp, s); })(); </script> <script> (function(){ var src = "https://jspassport.ssl.qhimg.com/11.0.1.js?d182b3f28525f2db83acfaaf6e696dba"; document.write('<script src="' + src + '" id="sozz"><\/script>'); })(); </script> <div style="display:none;"><script type="text/javascript" src="https://s4.cnzz.com/z_stat.php?id=1278594161&web_id=1278594161"></script></div> <!-- Global site tag (gtag.js) - Google Analytics --> <script async src="https://www.googletagmanager.com/gtag/js?id=G-4DVE4MNDE6"></script> <script> window.dataLayer = window.dataLayer || []; function gtag(){dataLayer.push(arguments);} gtag('js', new Date()); gtag('config', 'G-4DVE4MNDE6'); </script> </body> </html> <script src="/cdn-cgi/scripts/7d0fa10a/cloudflare-static/rocket-loader.min.js" data-cf-settings="ddc0d0a60d42350c745cd22e-|49" defer></script>