为什么这个脚本会变得疯狂?

发布于 2024-10-31 09:05:16 字数 1542 浏览 1 评论 0原文

噢伙计!

这是一个 VBScript,本来应该将目录中名为

1229_002510 的所有文件更改为 2010-12-29_002510,

但最终发生的情况是脚本从未终止。

在遍历完文件名后,它会继续运行并在前面多次添加 2010- 直到我杀死了脚本。

仅当您在 cmd 中通过 CScript 启动脚本时才会出现该错误

(启动方式:cscript“filename.vbs”)

所以现在我有一个文件夹,其中包含如下文件 2010-20-10-20-10-20-10-20-10-20-10-20-10-20-10-20-10-20-10-20-10-11-26_023335

意识到我是一个 VBScript 菜鸟。

该脚本似乎可以在测试模式下运行(仅打印文件名),但是一旦您让它在实际文件上运行,它就会表现出所描述的行为。

' shell script that changes dates like
' 1219_005530 to 2010-12-19_005530



' create a root filesystemobject:
Dim ofso
Set ofso = CreateObject( "Scripting.FileSystemObject" ) 

' create a folder object USING that root filesystem object
Dim folder

' that's the current directory
Set folder = ofso.GetFolder( "." )

' now, visit each file in the folder
Dim fileo
For Each fileo In folder.Files

  dim originalName
  originalName = CStr( fileo.Name )

  ' cut first 2 chars, prepend 2010-, re-add first 2 chars + "-"
  dim monthNumber
  monthNumber = Mid( originalName, 1, 2 )

  ' don't change the source file!
  If Right( originalName, 3 ) = "vbs" Then
    WSH.echo( "Not changing " & originalName )
  Else
    dim newName
    newName = "2010-" & monthNumber & "-" & Mid( originalName, 3 )

    WSH.echo( originalName )
    WSH.echo( newName & " < CHANGED TO" )


    ' ONLY ENABLE THIS LINE ONCE DEBUGGING COMPLETE  
    'fileo.Name = newName   
  End If

  Next

' PAUSE BEFORE EXIT  

Aw man!

This is a VBScript that was supposed to change all the files in the directory that are named like

1229_002510 to 2010-12-29_002510

What ended up happening is the script never terminated.

After one pass through the file names, it KEPT GOING and prepended 2010- MULTIPLE TIMES until I killed the script.

The bug only appears if you launch the script through CScript at cmd.

(Launch with: cscript "filename.vbs")

So now I have a folderful of files like
2010-20-10-20-10-20-10-20-10-20-10-20-10-20-10-20-10-20-10-20-10-11-26_023335

Realize I'm a VBScript noob.

The script appears to work in test mode (just printing file names) but as soon as you have it work on actual files, it does exhibit the described behavior.

' shell script that changes dates like
' 1219_005530 to 2010-12-19_005530



' create a root filesystemobject:
Dim ofso
Set ofso = CreateObject( "Scripting.FileSystemObject" ) 

' create a folder object USING that root filesystem object
Dim folder

' that's the current directory
Set folder = ofso.GetFolder( "." )

' now, visit each file in the folder
Dim fileo
For Each fileo In folder.Files

  dim originalName
  originalName = CStr( fileo.Name )

  ' cut first 2 chars, prepend 2010-, re-add first 2 chars + "-"
  dim monthNumber
  monthNumber = Mid( originalName, 1, 2 )

  ' don't change the source file!
  If Right( originalName, 3 ) = "vbs" Then
    WSH.echo( "Not changing " & originalName )
  Else
    dim newName
    newName = "2010-" & monthNumber & "-" & Mid( originalName, 3 )

    WSH.echo( originalName )
    WSH.echo( newName & " < CHANGED TO" )


    ' ONLY ENABLE THIS LINE ONCE DEBUGGING COMPLETE  
    'fileo.Name = newName   
  End If

  Next

' PAUSE BEFORE EXIT  

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

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

发布评论

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

评论(2

温馨耳语 2024-11-07 09:05:16

我不知道你写了什么,但我复制/粘贴了你的脚本,它的工作原理就像你想要的那样。 (Windows 7)

编辑:尝试设置脚本循环的最大次数。这样就可以解决问题了。脚本可能会将重命名的文件视为新文件,从而创建无限循环。

I dont know what you dit, but i copy/pasted your script and it works just as you intended. (Windows 7)

Edit: Try to set a max on the times the script may go tru the loop. That will fix it. Might be that the script sees the renamed files as new files, thus creating an infinite loop.

粉红×色少女 2024-11-07 09:05:16

您可以分两步执行此任务。首先扫描目录中的文件并将其名称收集到数组中,然后运行该数组并重命名文件。这个应该比较靠谱。

并考虑切换到 JScript。与它相比,VBS 简直丑陋。

You can perform this task in 2 steps. First scan a directory for files and collect their names into array, and then run through this array and rename files. This should be more reliable.

And consider to switch to JScript. VBS is just ugly compared to it.

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