for-loopvbscriptmovefile

VBScript: Move files to different folders depending on string in file name (For Each loop)


Original question:

I am quite unskillful with VBScript but would need an efficient solution for saving me lots of time selecting and copying files manually: I have a folder with thousands raster files containing values of daily temperature for a certain area, altogether covering a period of 30 years. In order to calculate monthly means out of 30 or 31 files per month (within a programme for geospatial data), I need to copy them into separate folders, e. g. the files from 2000 January 1 to 31 (named tx_20000101, tx_20000102 and so forth) into a folder named T_01_Jan_2000 and accordingly for all other months and years.

So I need a script, that searches for different text strings (YYYYMM) within all file names and moves the matched files into the given folder (for each search string a separate folder).

How could that be accomplished with VBScript? With examples found in forums (mainly this: https://stackoverflow.com/a/29001051/6093207), I have come so far:

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

However, I get different errors like 800A0414 and 800A0046, and did not get the script running yet as intended. Any suggestions for correcting the code or for more efficent ways of scripting are welcome.

Edited question:

Having a folder with several thousands netCDF-files containing values of daily temperature for a certain area, altogether covering a period of 30 years, how is it possible to move them into separate folders monthwise? The month folders should contain subfolders for the respective year.

So the files are named tx_20000101.nc, tx_20000102.nc and so forth and are altogether in the folder Temperature. Now all files from January should come into a folder name T_01, which contains subfolders named T_01_1991, T_01_1992 and so on, accordingly for all other months and years.

How can this be accomplished by VBScript?


Solution

  • The solution (thanks to @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