-1

原始问题:

我对 VBScript 很不熟练,但需要一个有效的解决方案来节省我大量手动选择和复制文件的时间:我有一个文件夹,其中包含数千个光栅文件,其中包含某个区域的每日温度值,总共涵盖了 30 年。为了计算每月 30 或 31 个文件的月均值(在地理空间数据程序中),我需要将它们复制到单独的文件夹中,例如 2000 年 1 月 1 日至 31 日的文件(名为 tx_20000101、tx_20000102 等)放入名为 T_01_Jan_2000 的文件夹中,并相应地保存所有其他月份和年份。

所以我需要一个脚本,在所有文件名中搜索不同的文本字符串(YYYYMM),并将匹配的文件移动到给定的文件夹中(对于每个搜索字符串,一个单独的文件夹)。

VBScript 怎么能做到这一点?通过在论坛中找到的示例(主要是:https ://stackoverflow.com/a/29001051/6093207 ),我到目前为止:

Option Explicit
Sub Dateien_verschieben()

Dim i
Dim FSO : Set fso = CreateObject("Scripting.FileSystemObject")
Dim Quelle : Set Quelle = FSO.GetFolder("C:\Users\…\Temperature")

Dim Ziel1 : Set Ziel1 = FSO.GetFolder("C:\Users\…\Temperature\T_01_Jan\T_01_Jan_2000")
Dim Ziel2 : Set Ziel2 = FSO.GetFolder("C:\Users\…\Temperature\T_02_Feb\T_02_Feb_2000")
…
Dim Ziel12 : Set Ziel12 = FSO.GetFolder("C:\Users\…\Temperature\T_12_Dez\T_12_Dez_2000")

Dim Str1, Str2, Str3, Str4, Str5, Str6, Str7, Str8, Str9, Str10, Str11, Str12

Str1 = "200001"
Str2 = "200002"
…
Str12 = "200012"
i = 0

For Each file in Quelle.files
x = fso.getbasename(file)
If instr(lcase(x), Str1) Then
    i = i+1
       If fso.fileexists(Ziel1 & "\" & file.name) Then
        fso.deletefile Ziel1 & "\" & file.name, True
       End If
    fso.movefile Quelle & "\" & file.name, Ziel1
ElseIf instr(lcase(x), Str12) Then 'I have omitted the other ElseIf statements here for reasons of clarity
    i = i+1
        If fso.fileexists(Ziel12 & "\" & file.name) Then
        fso.deletefile Ziel12 & "\" & file.name, True
        End If
    fso.movefile Quelle & "\" & file.name, Ziel12
End If
Next

If i>0 Then
wscript.echo i&" files moved to path " & vbcrlf & Quelle
wscript.quit()
End If

wscript.echo "No matches found"

End Sub

但是,我收到了不同的错误,例如 800A0414 和 800A0046,并且没有按预期运行脚本。欢迎任何有关更正代码或更有效的脚本编写方式的建议。

编辑问题:

拥有一个包含数千个 netCDF 文件的文件夹,其中包含某个区域的每日温度值,总共涵盖了 30 年的时间,如何将它们按月移动到单独的文件夹中?月份文件夹应包含相应年份的子文件夹。

所以文件被命名为tx_20000101.nctx_20000102.nc等等,并且完全在文件夹Temperature中。现在,从一月份开始的所有文件都应该进入一个文件夹名称T_01,其中包含名为T_01_1991T_01_1992等的子文件夹,相应地适用于所有其他月份和年份。

VBScript 如何做到这一点?

4

1 回答 1

0

解决方案(感谢@Les Ferch):

Move = True 'Set to False for Copy 
SrcDir = ".\Temperature" Set oFSO = CreateObject("Scripting.FileSystemObject") 
Set Source = oFSO.GetFolder(SrcDir) 
For Each File in Source.Files   
    FileName = Right(File,11)   
    YYYY = Mid(FileName,1,4)   
    MM = Mid(FileName,5,2)   
    MonthDir = SrcDir & "\T_" & MM & "\"   
    YearDir = MonthDir & "T_" & MM & "_" & YYYY & "\"   
    If Not oFSO.FolderExists(MonthDir) Then oFSO.CreateFolder(MonthDir)   
    If Not oFSO.FolderExists(YearDir) Then oFSO.CreateFolder(YearDir)   
    If Move Then oFSO.MoveFile(File),YearDir Else oFSO.CopyFile(File),YearDir
Next
于 2021-10-18T20:35:31.167 回答