-1

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?

ebcs
  • 89
  • 8
  • Have you considered writing a script that copies the data from the individual files to a single CSV file? Then you could load the CSV file into Google Sheets or Excel, etc. and easily get your averages. – LesFerch Oct 12 '21 at 12:54
  • Thanks for your suggestion but the files are geospatial raster files in [netCDF](https://en.wikipedia.org/wiki/NetCDF) format containing temperature values for a large area. That is why in this case working with CSV is not possible. – ebcs Oct 12 '21 at 13:08
  • Presumably you already have a program that can calculate the averages for all the files in a single folder. Hence the requirement to copy or move them in one month groups. I was hoping to save you from that pain. It just seems that with the files already named as they are, the calculations could be done without putting them in separate folders. If no one else answers, I'll get back with an answer later. BTW, this is potentially easier to do with PowerShell (depending on your PS skills). – LesFerch Oct 12 '21 at 13:29
  • Thanks for the hint, I will have to search how to do that in PowerShell then. So the GIS programme I would use for the calculations is ArcGIS Pro, and in its ModelBuilder (scripting interface) has the function “iterate workspace”. So if I had the files nicely separated by month and year, I could run this function easily. Maybe it is possible to tell ArcGIS Pro to use only certain files as input (by searching a string of YYYYMM##) by means of an ArcPy script, but that was as well something I am not familiar with and would have to search in forums. – ebcs Oct 12 '21 at 14:09
  • 1
    Try this: https://github.com/LesFerch/SampleCode/blob/main/VBS-misc/MoveTXFiles.vbs – LesFerch Oct 13 '21 at 04:26
  • Thank you so much, @LesFerch, it works for me!!! It saved inconceivably much manual selecting and copying, just fantastic to watch it run. – ebcs Oct 14 '21 at 07:32
  • When I saw your edited script, I first thought there was an error, but then I realized that you wanted to group years under months, whereas I had months under years. Nice work! – LesFerch Oct 14 '21 at 12:21

1 Answers1

0

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
LesFerch
  • 1,540
  • 2
  • 5
  • 21
ebcs
  • 89
  • 8