批处理调用MSXSL处理XML时输出文件名带双引号报错如何解决?
The root issue here is that forfiles's @file variable automatically wraps the filename in double quotes. When you plug this directly into your output path, you end up with nested quotes (like Out\"bk_OIOUBLInvoice_TEST.xml\"), which Windows interprets as invalid syntax.
Here are two straightforward fixes, with the first being the simplest and most reliable:
1. Use @fname and @ext to Build a Quote-Free Filename
Instead of using @file for the output filename, split it into the base name (@fname) and extension (@ext). These variables don't include quotes, so your output path will be clean:
forfiles /p C:\Users\mae\Documents\Testing\MSXSL\In /m *.xml /c "cmd /c C:\Users\mae\Documents\Testing\MSXSL\msxsl.exe @file pre-process_add_filename.xsl -o C:\Users\mae\Documents\Testing\MSXSL\Out\@fname.@ext"
This works because @fname gives you the filename without the extension (e.g., bk_OIOUBLInvoice_TEST) and @ext gives you just the extension (e.g., .xml). Combining them gives you the full, unquoted filename for the output path.
2. Strip Quotes from @file Using Delayed Expansion
If you prefer to stick with @file, you can remove the surrounding quotes using delayed variable expansion in cmd. This requires enabling delayed expansion (/v:on) and escaping the quote characters properly:
forfiles /p C:\Users\mae\Documents\Testing\MSXSL\In /m *.xml /c "cmd /v:on /c set ""fn=@file""& set ""fn=!fn:\""""=""!& C:\Users\mae\Documents\Testing\MSXSL\msxsl.exe @file pre-process_add_filename.xsl -o C:\Users\mae\Documents\Testing\MSXSL\Out\!fn!"
The set ""fn=!fn:\""""=""! part replaces the double quotes in the @file value with nothing, leaving you with the raw filename.
Either approach will resolve the "invalid syntax" error by ensuring your output filename doesn't have unwanted nested quotes.
内容的提问来源于stack exchange,提问作者Quality

