VB脚本重命名文件?

发布于 2024-10-20 19:58:12 字数 429 浏览 6 评论 0原文

我编写了一个非常简单的批处理脚本,它将为我执行 XSLT 转换,将一堆 html 文件转换为 xml 文件:

FOR %%i IN (*.htm) DO java -jar saxon.jar -o:"..\Front_Matter\%%i.xml" "%%i" "C:\Documents and Settings\Robert\Desktop\xsl\htm2xml.xsl"

XSLT 工作正常,但只是创建的文件没有所需的文件名,例如例如,如果原始文件名是“Happy Christmas.htm”,我希望输出的xml名为“Happy_Christmas.xml”,所以只有两件事,首先是去掉原始的.htm部分文件名,二是用下划线替换空格。

当前生成的文件名很难看,例如:“Happy Christmas.htm.xml”。

提前致谢!

I've written a very simple batch script that will do an XSLT transform for me, to translate a bunch of html files to xml files:

FOR %%i IN (*.htm) DO java -jar saxon.jar -o:"..\Front_Matter\%%i.xml" "%%i" "C:\Documents and Settings\Robert\Desktop\xsl\htm2xml.xsl"

The XSLT works fine, but just the file gets created doesn't have the desired file name, for example, if the original file name is "Happy Christmas.htm", I want the output xml to be called "Happy_Christmas.xml", so there are only just two things, first is to get rid of the .htm part of the original file name, the second is to replace the space by underscore.

The current resulting file name is ugly, like: "Happy Christmas.htm.xml".

Thanks in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

双手揣兜 2024-10-27 19:58:12
  1. 使用 %%~ni 而不是 %%i 提取不带扩展名的基本文件名。

  2. 使用 %var: =_% 语法将 var 变量值中的空格替换为下划线。

所以,基本上,你需要这样的东西:

@echo off
setlocal enabledelayedexpansion

FOR %%i IN (*.htm) DO (
  SET basename=%%~ni
  SET basename=!basename: =_!
  java -jar saxon.jar -o:"..\Front_Matter\!basename!.xml" "%%i" "C:\Documents and Settings\Robert\Desktop\xsl\htm2xml.xsl"
)
  1. Use %%~ni instead of %%i to extract the base file name without extension.

  2. Use the %var: =_% syntax to replace spaces in the value of the var variable with underscores.

So, basically, you need something like this:

@echo off
setlocal enabledelayedexpansion

FOR %%i IN (*.htm) DO (
  SET basename=%%~ni
  SET basename=!basename: =_!
  java -jar saxon.jar -o:"..\Front_Matter\!basename!.xml" "%%i" "C:\Documents and Settings\Robert\Desktop\xsl\htm2xml.xsl"
)
谎言 2024-10-27 19:58:12
Set fso = CreateObject("Scripting.FileSystemObject")

set oFldr = fso.getfolder("N:\Groups\TestGroup\UPS\")

for each ofile in oFldr.Files
  if lcase(fso.GetExtensionName(ofile.Name)) = "txt" then
    fso.MoveFile ofile.Path, ofile.ParentFolder & "\HRSC100.txt"

    Exit for
  end if
Next
*if you want vbscirpt then above will work*
Set fso = CreateObject("Scripting.FileSystemObject")

set oFldr = fso.getfolder("N:\Groups\TestGroup\UPS\")

for each ofile in oFldr.Files
  if lcase(fso.GetExtensionName(ofile.Name)) = "txt" then
    fso.MoveFile ofile.Path, ofile.ParentFolder & "\HRSC100.txt"

    Exit for
  end if
Next
*if you want vbscirpt then above will work*
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文