如何修改Primary.bat生成含指定格式命令的Generator.bat
Fixing Primary.bat to Generate Executable Converter Commands in Generator.bat
Got it, let's tweak your Primary.bat so that the generated Generator.bat contains ready-to-run commands that call converter.java for each text file in your source directory. Here's a revised version with clear explanations:
Modified Primary.bat Code
@echo off SETLOCAL ENABLEDELAYEDEXPANSION :: Configure your paths here (easy to adjust later) set "sourceDir=C:\InputOutput\SourceFiles" set "generatorBat=C:\InputOutput\Generator.bat" set "outputDir=C:\InputOutput\OutputFiles" :: Customize this to your preferred output location :: Clear existing Generator.bat to avoid appending to old content echo @echo off > "%generatorBat%" echo echo Starting file conversion process... >> "%generatorBat%" echo. >> "%generatorBat%" :: Add blank line for readability :: Loop through every text file in the source directory for %%f in ("%sourceDir%\*.txt") do ( :: Get full path of the source file set "sourcePath=%%~f" :: Build output file path (uses source filename with "_output" suffix; adjust as needed) set "outputPath=%outputDir%\%%~nf_output.txt" :: Write the converter command to Generator.bat echo java converter.java "!sourcePath!" "!outputPath!" >> "%generatorBat%" :: Optional: Add a status message for each conversion echo echo Finished converting "%%~nf.txt" >> "%generatorBat%" echo. >> "%generatorBat%" ) :: Add final completion message and pause echo echo All conversions are done! >> "%generatorBat%" echo pause >> "%generatorBat%" echo Success! Generator.bat has been created with all conversion commands. pause ENDLOCAL
Key Fixes & Explanations
- Corrected delayed expansion syntax: Your original code had
SETLOCAL=ENABLEDELAYEDEXPANSION(with an equals sign) which is invalid. The correct syntax isSETLOCAL ENABLEDELAYEDEXPANSION—this lets us use!variable!to access updated values inside the loop. - Loop through files instead of using dir: The
forloop lets us process each file individually, so we can build a full command for every text file, rather than just listing filenames. - Clear Generator.bat first: We overwrite the file instead of appending, so you don't get duplicate commands if you run Primary.bat multiple times.
- Friendly output: Generator.bat now includes status messages and a final
pauseso you can see when conversions finish, instead of closing immediately. - Customizable paths: All directory paths are set at the top, making it easy to adjust later without digging through the code.
Notes to Keep in Mind
- Make sure
converter.class(compiled fromconverter.java) is accessible when running Generator.bat—either place it in the same directory as Generator.bat, or add its location to your system'sCLASSPATH. - If you need to process non-.txt files, change
*.txtin theforloop to your target extension (e.g.,*.mdfor Markdown files) or*.*for all files (just be cautious about non-text files). - Adjust the output filename format (like removing
_outputor changing the extension) by modifying theoutputPathline—%%~nfgrabs the source filename without its extension, so you can mix and match as needed.
内容的提问来源于stack exchange,提问作者Aryabhatta
相关产品推荐
相关产品推荐

