一键重装系统工具 | U盘启动盘制作工具 | 误删文件恢复软件 | 硬盘数据抢救专家 | 电脑蓝屏修复助手 | C盘空间清理神器 | 电脑驱动离线安装工具 | 微信聊天记录恢复工具 | 照片误格式化恢复 | 电脑密码破解清除工具 | 系统崩溃紧急救援盘 | 电脑加速优化大师 | 电脑开不了机怎么重装系统 | 回收站清空了怎么恢复 | 硬盘分区丢失数据恢复 | 电脑卡顿重装系统有用吗 | U盘插入提示格式化数据恢复 | 电脑中毒文件被隐藏恢复 | 忘记电脑开机密码怎么办 | 新硬盘分区对齐工具 | 旧电脑装Win10流畅工具 | SD卡照片删除恢复免费版 | 移动硬盘打不开提示损坏修复 | 电脑无故重启系统修复工具 | 电脑小白一键重装神器 | 程序员电脑环境配置助手 | 设计师电脑字体/素材恢复工具 | 网吧网管系统维护工具箱 | 财务人员电脑发票备份恢复 | 学生党免费电脑系统安装包 | 电脑维修师傅必备工具盘 | 游戏玩家电脑性能优化助手 | 办公白领误删文档恢复软件 | 自媒体视频素材恢复工具 | 网课录制视频损坏修复工具 | 最好的U盘PE系统排名 | 数据恢复软件哪个最强 | 免费电脑助手与收费版区别 | 国产装机工具哪款无广告 | 离线版驱动助手推荐 | 轻量级电脑优化工具对比 | 支持NVMe驱动的PE工具 | 带网络功能的应急启动盘 | 2026最新版万能装机工具 | 支持Win11 24H2的PE工具 | 最新免激活系统重装工具 | 2026数据恢复软件破解版合集 | 纯净无捆绑装机助手V3.0 | 支持苹果M芯片的电脑助手 | 秋季更新版系统维护工具箱 | 电脑系统崩了怎么用U盘把重要资料拷贝出来 | 重装系统前哪些文件夹必须备份 | 固态硬盘误格式化还能恢复数据吗 | 如何制作一个既带PE又能存数据的双分区U盘 | 电脑总是弹窗广告用什么助手彻底拦截 后台管理
📢 欢迎访问系统之家!所有资源均经过安全检测。

How to Write a Batch File on Windows: Guide + Examples

发布时间:2026-09-11 | 浏览:1
📥 下载地址(文章开头)
数据恢复恢复一切数据。
PRO Courses Hot Guides Tech Help Pro Expert Videos About wikiHow Pro Upgrade QUIZZES All Quizzes Hot Community Quizzes Love Quizzes Personality Quizzes Trivia Quizzes Taylor Swift Quizzes EXPLORE Tech Help Pro About Us Random Article Quizzes Request a New Article Community Dashboard Trending Forums Arts and Entertainment Artwork Books Movies Computers and Electronics Computers Phone Skills Technology Hacks Health Men's Health Mental Health Women's Health Relationships Dating Love Relationship Issues Hobbies and Crafts Crafts Drawing Games Education & Communication Communication Skills Personal Development Studying Personal Care and Style Fashion Hair Care Personal Hygiene Quizzes Love Quizzes Personality Quizzes Fun Games Arts and Entertainment Finance and Business Home and Garden Relationship Quizzes Cars & Other Vehicles Food and Entertaining Personal Care and Style Sports and Fitness Computers and Electronics Health Pets and Animals Travel Education & Communication Hobbies and Crafts Philosophy and Religion Work World Family Life Holidays and Traditions Relationships Youth LOG IN Log in Social login does not work in incognito and private browsers. Please log in with your username or email to continue. Facebook Google wikiHow Account No account yet? Create an account Browse Articles Quizzes & Games All Quizzes Hot Community Quizzes Personality Quizzes Dating Simulator Learn Something New Support wikiHow Log in / Sign up Computers and Electronics Operating Systems Last Updated: July 10, 2026 Fact Checked Batch File Basics Saving and Running a Batch File Troubleshooting Sample Batch File This article was co-authored by wikiHow staff writer, Nicole Levine, MFA . Nicole Levine is a Technology Writer and Editor for wikiHow. She has more than 20 years of experience creating technical documentation and leading support teams at major web hosting and software companies. Nicole also holds an MFA in Creative Writing from Portland State University and teaches composition, fiction-writing, and zine-making at various institutions. This article has been fact-checked, ensuring the accuracy of any cited facts and confirming the authority of its sources. This article has been viewed 3,016,094 times. Learn more... This wikiHow guide will teach you how to write and save a basic batch file on a Windows computer. Batch files, or batch scripts, contain Command Prompt commands, arguments, and variables, and are commonly used to automate frequently performed tasks. You shouldn't have to download any fancy editors to create a batch file—the Windows-standard Notepad program is more than sufficient. Creating a Batch File: Fast Facts You can write a batch file in Notepad, a free text editor built into Windows. To save the file, go to File > Save as , select All files (*.*) from the "Save as type" menu, and type a file name that ends with ".bat". To run the script, open Command Prompt, enter the folder that contains the batch file, type the full name of the batch file (e.g., "Backup.bat"), and press Enter . Batch File Basics 1 Open Notepad. Notepad allows you to create code as a text file and then save it when you're done as a batch file. You can open Notepad by opening Start , typing Notepad , and clicking the Notepad app icon at the top of the menu. Notepad is commonly used to convert text files into batch files, but you can write your batch file's text virtually anywhere. Notepad is commonly used to convert text files into batch files, but you can write your batch file's text virtually anywhere. 2 Learn some basic batch commands. Batch files run a series of Command Prompt commands. Some of the more common commands include: [1] X Research source ECHO - Displays text on the screen @ECHO OFF - Hides the text that is normally output START - Start a program, command, or script in a new window REM - Inserts a comment line in the program DEL - Delete a file CD - Change to a directory FOR - Loop a command several times IF - Conditionally perform a command DATE , TIME - Display the date or time FIND - Search for a string of text MKDIR - Create a directory RMDIR - Delete a directory DEL - Deletes a file or files COPY - Copy a file or files XCOPY - Allows you to copy files with extra options TITLE - Edit the title of the window PAUSE - Pauses the script until the user presses a key Advertisement ECHO - Displays text on the screen @ECHO OFF - Hides the text that is normally output START - Start a program, command, or script in a new window REM - Inserts a comment line in the program DEL - Delete a file CD - Change to a directory FOR - Loop a command several times IF - Conditionally perform a command DATE , TIME - Display the date or time FIND - Search for a string of text MKDIR - Create a directory RMDIR - Delete a directory DEL - Deletes a file or files COPY - Copy a file or files XCOPY - Allows you to copy files with extra options TITLE - Edit the title of the window PAUSE - Pauses the script until the user presses a key 3 Write a program to create a directory. One of the easiest ways to learn how to create batch files is to focus on doing basic tasks first. For example, you can use a batch file to quickly create multiple directories: MKDIR c:\example1 MKDIR c:\example2 4 Write the code to make a basic backup program. Batch files are great for running multiple commands, especially if you configure them to be able to run multiple times. With the XCOPY command, you can make a batch file that copies files from select folders to a backup folder, only overwriting files that have been updated since the last copy: @ ECHO OFF XCOPY c:\original c:\backupfolder /m /e /y This copies over files from the folder "original" to the folder "backupfolder". You can replace these with the paths to the folders you want. /m specifies that only files with the "archive" attribute will be copied, /e specifies that all subdirectories in the listed directory will be copied (even if empty), and /y keeps the confirmation message from appearing every time a file is overwritten. [2] X Research source This copies over files from the folder "original" to the folder "backupfolder". You can replace these with the paths to the folders you want. /m specifies that only files with the "archive" attribute will be copied, /e specifies that all subdirectories in the listed directory will be copied (even if empty), and /y keeps the confirmation message from appearing every time a file is overwritten. [2] X Research source 5 Write a more advanced backup program. While simply copying the files from one folder to another is nice, what if you want to do a little sorting at the same time? That's where the FOR/IN/DO command comes in. You can use that command to tell a file where to go depending on the extension: @ ECHO OFF cd c:\source REM This is the location of the files that you want to sort FOR %% f IN ( *.doc *.txt ) DO XCOPY c:\source\ " %% f" c:\text /m /y REM This moves any files with a .doc or REM .txt extension from c:\source to c:\text REM %%f is a variable FOR %% f IN ( *.jpg *.png *.bmp ) DO XCOPY C:\source\ " %% f" c:\images /m /y REM This moves any files with a .jpg, .png, REM or .bmp extension from c:\source to c:\images 6 Display some text. If you want to know what is happening in your batch file but don't want to see all the commands, you could program the batch file to print some text that explains what the batch file does. You can print text with ECHO . For example: @ ECHO OFF MKDIR c:\example1 ECHO Created directory example1 You can change the color of the output with COLOR bf , where b is the background and f is the foreground color, both hexadecimal numbers. The following colors are possible: Number Color Number Color 0 black 8 dark grey 1 dark blue 9 blue 2 dark green a green 3 dark turquoise b turquoise 4 dark red c red 5 dark magenta d magenta 6 dark yellow e yellow 7 light grey f white For example, red text on a dark green background would be displayed with COLOR 2c You need to run the batch file from the command line to see the text. Otherwise, the window will close too fast to actually read the text you printed. You can change the color of the output with COLOR bf , where b is the background and f is the foreground color, both hexadecimal numbers. The following colors are possible: Number Color Number Color 0 black 8 dark grey 1 dark blue 9 blue 2 dark green a green 3 dark turquoise b turquoise 4 dark red c red 5 dark magenta d magenta 6 dark yellow e yellow 7 light grey f white For example, red text on a dark green background would be displayed with COLOR 2c You need to run the batch file from the command line to see the text. Otherwise, the window will close too fast to actually read the text you printed. 7 Experiment with different batch commands. If you want inspiration, you can check out the sample batch text at the end of this article. Saving and Running a Batch File
📥 下载地址(文章中间)
数据恢复恢复一切数据。
1 Write your batch file in Notepad. Once you've completed and proofread your batch file, you can proceed with saving it as an executable file. 2 Click the File menu and select Save As… . This option is in the File drop-down menu. Clicking it prompts the Save As window to open. 3 Enter a name and the ".bat" extension. In the "File name" text box, type in whatever you want to name your program, followed by .bat . For a program named "Backup", for example, you might type in Backup.bat here. For a program named "Backup", for example, you might type in Backup.bat here. 4 Select All Files from the "Save as type" menu. This is super important. If you skip this step, your batch file will not be executable, as it will be given the .txt file extension (e.g., Backup.bat.txt) by default. 5 Select a save location and click Save . It's in the bottom-right corner of the Save As window. The window will close. At any time, you can right-click your batch file and click Edit in the resulting drop-down menu. This will open the batch file as a Notepad document; at this point, you can make any changes and then save the file by pressing Ctrl + S . At any time, you can right-click your batch file and click Edit in the resulting drop-down menu. This will open the batch file as a Notepad document; at this point, you can make any changes and then save the file by pressing Ctrl + S . 6 Open a Command Prompt window. You can do so by pressing the Windows key, typing cmd , and pressing ↵ Enter . If your batch file contains commands that must be run with administrator permissions, run Command Prompt as an administrator. If your batch file contains commands that must be run with administrator permissions, run Command Prompt as an administrator. 7 Enter the directory containing your batch file. When you open a Command Prompt window, it automatically opens to your user folder on the current Windows system. If you saved your batch file to the desktop, type cd Desktop and press ↵ Enter to enter that directory. If you use OneDrive to sync your data to the cloud and have saved your batch file in your Desktop, Documents, or Downloads folder, OneDrive changes your user directory structure. [3] X Research source Instead, use cd OneDrive\Desktop , cd OneDrive\Downloads , or cd OneDrive\Documents . If you use OneDrive to sync your data to the cloud and have saved your batch file in your Desktop, Documents, or Downloads folder, OneDrive changes your user directory structure. [3] X Research source Instead, use cd OneDrive\Desktop , cd OneDrive\Downloads , or cd OneDrive\Documents . 8 Type the name of your batch file and press ↵ Enter to run it. For example, to run a file called Backup.bat in the current directory, just type Backup.bat and press ↵ Enter . If you prefer using PowerShell, run the batch file using the command ./Backup.bat instead. If you prefer using PowerShell, run the batch file using the command ./Backup.bat instead. Troubleshooting 1 Running the batch file opens it up in Notepad instead. This common problem happens when you don't select "All files" before saving the batch file. To fix it: Open the batch file in Notepad. Click File > Save as . Click the "Save as type" menu. Select All files (*.*) . Remove the ".txt" from the end of the batch file's file name so it ends with .bat. Click Save . Open the batch file in Notepad. Click File > Save as . Click the "Save as type" menu. Select All files (*.*) . Remove the ".txt" from the end of the batch file's file name so it ends with .bat. 2 Double-clicking the batch file opens and closes a Command Prompt window. If you want to be able to see the actions a batch file is taking, you'll want to run it from Command Prompt instead of double-clicking it. Just open Command Prompt, navigate to the folder that contains the batch file (e.g., cd C:\Users\Me\Documents , type the name of the batch file, and press Enter . Sample Batch File Question How can I run multiple batch files? Community Answer Open multiple files at once. But if you really wanted to, you can open other batch files within a batch file as such: start "c:\Users\Xx_balzeitmichael_xX\Documents" random.bat. Thanks! We're glad this was helpful. Thank you for your feedback. If wikiHow has helped you, please consider a small contribution to support us in helping more readers like you. We’re committed to providing the world with free how-to resources, and even $1 helps us in our mission. Support wikiHow Yes No Not Helpful 17 Helpful 38 Question How do I open images using a batch file? David Langr Community Answer You can do so by typing the following command: start "C:\Path\picture.jpg". Thanks! We're glad this was helpful. Thank you for your feedback. If wikiHow has helped you, please consider a small contribution to support us in helping more readers like you. We’re committed to providing the world with free how-to resources, and even $1 helps us in our mission. Support wikiHow Yes No Not Helpful 5 Helpful 19 Question How do I create a batch file? Arsalan Kazmi Community Answer Enter the code in Notepad and then go to File > Save As, click File Type, set it to All Files, then in the Name box, enter name.bat, where 'name' is the name of your batch file. Thanks! We're glad this was helpful. Thank you for your feedback. If wikiHow has helped you, please consider a small contribution to support us in helping more readers like you. We’re committed to providing the world with free how-to resources, and even $1 helps us in our mission. Support wikiHow Yes No Not Helpful 12 Helpful 24 You will have to use quotes if you want to open a directory or file with spaces in its name (e.g., start "C:\Documents and Settings\" ). Thanks Helpful 0 Not Helpful 0 You can use third-party editors such as Notepad++ to edit your batch file, but for the most part, you won't need to. Thanks Helpful 0 Not Helpful 0 Tips from our Readers If anyone sent you a .bat file, DO NOT OPEN IT! Instead, right-click it and hit "Edit" so you can inspect the code to see what's inside. You can also make comments by prefacing lines with "::". Depending on the commands you use, batch files can be dangerous. Make sure that none of your code runs the risk of performing an undesirable task (e.g., deleting files or crashing your computer). Thanks Helpful 2 Not Helpful 0 You Might Also Like ↑ https://ss64.com/nt/ ↑ https://learn.microsoft.com/en-us/windows-server/administration/windows-commands/xcopy ↑ https://www.zdnet.com/home-and-office/work-life/is-onedrive-sending-all-your-windows-files-to-the-cloud-heres-why-and-what-you-can-do/ About This Article 1. Learn basic batch commands. 2. Open Notepad. 3. Write your program. 4. Go to File > Save As . 5. Type a name for the file that ends with ".bat" 6. Select All Files from the "Save as type" menu. 7. Click Save . Did this summary help you? Yes No Send fan mail to authors Reader Success Stories Hernan Garcia Aug 31, 2016 "I was trying to create a batch file to automatically run several files in a particular software. However, I had no idea how to proceed. This page helped me in understanding the basic concepts. Now I can start, thank you!" ..." more Is this article up to date? Tasztalos Pista Quizzes & Games You Might Also Like Trending Articles Trending Articles Take the Quiz. Face the Truth. 🔥 Are You More… 🤔 Featured Videos Hot Takes Only 🔥 Your Daily Dose of Fun 🎉 Computers and Electronics Operating Systems Do Not Sell or Share My Info Not Selling Info wikiHow Tech Help Pro: Level up your tech skills and stay ahead of the curve
📥 下载地址(文章结尾)
数据恢复恢复一切数据。