- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个包含“unix 风格”行尾的文本文件:每行末尾有一个 0x0A。
我正在编写一个脚本来修改该文件,以向其中添加新内容。这是在 Windows 上运行的 JScript。代码如下所示:
var fso = new ActiveXObject("Scripting.FileSystemObject"),
fs = fso.openTextFile(src),
origContent = fs.readAll();
fs.Close();
fso.MoveFile(src, dest);
// write updated content to a new file.
fs = fso.CreateTextFile(src);
fs.WriteLine("# key: " + keyvaluee);
fs.WriteLine("# group: " + groupvalue);
fs.WriteLine(origContent);
fs.Close();
修改后的文件内容是这样的:
00000000: 2320 6b65 793a 2075 6e64 6566 696e 6564 # key: undefined
00000010: 0d0a 2320 6772 6f75 703a 2069 6d61 700d ..# group: imap.
00000020: 0a23 636f 6e74 7269 6275 746f 7220 3a20 .#contributor :
00000030: 416e 6472 6577 2047 776f 7a64 7a69 6577 Andrew Gwozdziew
00000040: 7963 7a20 3c77 6562 4061 7067 776f 7a2e ycz <web@apgwoz.
00000050: 636f 6d3e 0a23 6e61 6d65 203a 2069 6d61 com>.#name : ima
00000060: 705f 6765 745f 7175 6f74 6172 6f6f 7428 p_get_quotaroot(
00000070: 2e2e 2e2c 202e 2e2e 290a 2320 2d2d 0a69 ..., ...).# --.i
00000080: 6d61 705f 6765 745f 7175 6f74 6172 6f6f map_get_quotaroo
00000090: 7428 247b 696d 6170 5f73 7472 6561 6d7d t(${imap_stream}
000000a0: 2c20 247b 7175 6f74 615f 726f 6f74 7d29 , ${quota_root})
000000b0: 2430 0d0a $0..
如您所见,使用 JScript TextStream 添加的行以 0D 0A
结尾,而现有内容的每一行仅以 0A
结尾。查看位置 0x10 处的序列并与位置 0x54 处的字节进行比较。这是一个文本文件,我只是以十六进制显示内容。
如何使修改后的文件中的行尾保持一致?无论哪种方式对我来说都很好,我只是不喜欢在同一个文件中使用这两种方式。
我可以将内存中的 origContent
字符串转换为使用“DOS 风格”行尾吗?
最佳答案
我只是用十六进制编码替换字符串。这对我有用。
var fso = new ActiveXObject("Scripting.FileSystemObject"),
fs = fso.openTextFile(src),
origContent = fs.readAll(),
re1 = new RegExp("\x0A", 'g');
fs.Close();
fso.MoveFile(src, dest);
// write updated content to a new file.
fs = fso.CreateTextFile(src);
fs.WriteLine("# key: " + keyvaluee);
fs.WriteLine("# group: " + groupvalue);
origContent = origContent.replace(re1, "\x0D\x0A");
fs.WriteLine(origContent);
fs.Close();
结果是 origContent
中的所有行尾都被转换为 CR/LF 而不仅仅是 LF。仅当 原始文件中没有 CRLF 对 时才有效 - 仅当它最初具有所有 unix 行结尾时。
关于javascript - 如何使用 Scripting.FileSystemObject 和 TextStream 将 LF 转换为 CR/LF?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9858193/
如果这已在其他地方得到回答,我们深表歉意。我很难理解其他论坛帖子上糟糕的书面英语,我真的很想了解发生了什么。 这段代码很好用。 dim FileSys, source, destination, WS
我刚刚经历过这样的时刻之一,你走出杂草,检查你的代码并问自己“我为什么要那样做?” 我有一个脚本,它使用 FileSystemObject 创建一个包含多个子文件夹和文件的文件夹。一切正常,但我想我一
在本节描述的示例代码,提供真实的例子来示范在 FileSystemObject 对象模式中可用的许多功能。该代码显示了如何一起使用对象模式的所有功能,以及如何在您自己的代码中有效
在 VBScript 中,我想获取按创建日期排序的文件夹中的文件列表。我看到为了做到这一点,我需要使用记录集(对我来说似乎有点矫枉过正)或自己对集合进行排序(我想我可以避免它并且我希望我的代码更短)。
下面是用于打开文件并将所需数据粘贴到这些文件中并在完成工作后关闭这些文件的代码。当我运行这段代码时,没有文件在打开和 Run-time error-13 : Type mismatch 正在抛出这行代
不知道为什么,但下面的代码已经开始抛出一个未知错误。当宏运行时,Excel 停止响应。 为什么会出现这个错误? 具有相同功能的替代路线是什么? 此代码位于 Windows 7 计算机上的 Excel
我创建了一个 Do While 循环来循环使用 .jpg 的文件扩展,以便使用这样的代码发现第一个 fn = Dir(sPath & "*.jpg") Do While fn <> "" sF
我正在尝试编写一个宏来自动将 csv 文件导入我的工作簿。但是我遇到了解析分隔符的问题,特别是文件系统对象的 readline 方法在调用时没有读取完整的行 例子 csv中的一行: 1,2,jac
我有几个使用 FileSystemObject 的过程。我觉得挺方便的。 问题:将 FileSystemObject 的现有实例作为参数从“主”过程传递到这些其他过程,而不是让每个过程创建自己的 Fi
我编写了一个小程序来检查驱动器是否可用等。当我在另一台计算机上安装它时,Avira Antivirus 弹出木马警告。我将有问题的代码行缩小为: 20 Set fso = CreateO
有没有我可以使用的函数将不可靠的文件名转换为好的文件名? 我正在处理大量照片,偶尔,我的脚本会停止,因为上传者在文件名中加入了 curl 符号 (~)。我现在还想知道是否还有其他不能出现在文件名中的错
我有 Excel-2007。我正在使用文件系统对象 VBA 代码列出目录中的文件。我还在 Excel 中设置了对 Microsoft Scriptlet 库的引用。我得到: 编译器错误:用户定义类型未
如果一个文件夹中只有一个文件,我是否可以在不知道知道其名称或遍历文件夹中的文件的情况下将其取出? (代码是 VBS,但它可以是任何东西,FSO 是这里有趣的部分。) 这对我不起作用: dim fso
我想使用以下代码创建一个目录和一个子目录: Public fso As Scripting.FileSystemObject Set fso = New Scripting.FileSystemObj
是的,我仍在使用经典 ASP。 我将要编写一个脚本,每 5 分钟检查一次服务器上的目录,检查我的办公室是否有新上传的照片,并将照片传输到另一个位置。我使用 ASP 和 FileSystemObject
所以在我的程序中,我使用 COM Auotmation(Silverlight 4 中的 AutomationFactory)创建一个 FileSystemObject,我向其写入一个字符串 (the
我已经在网络驱动器上创建了一个文件作为脚本的一部分,并且我试图将其隐藏,这样如果再次运行脚本,它应该能够看到该文件并根据其中包含的信息进行操作,但是我在做这件事时遇到了麻烦。到目前为止我所拥有的是:
我需要访问文件信息,例如文件大小。 我可以创建 ActiveXObject: ActiveXObject fileSystem = new ActiveXObject("Scripting.FileS
FileSystemObject 对象 提供对计算机文件系统的访问。 说明 以下代码举例说明如何使用 FileSystemObject 对象返回一个 TextStream 对象,此对象可以被读
在源文件夹中有 3 个文件: a.csv b.csv a.csv_backup 我希望 *.csv 只复制 a.csv 和 b.csv,但它复制了 a.csv_backup 也是。 代码: Dim o
我是一名优秀的程序员,十分优秀!