VB Script, copy and remove files based on file name len

Hello experts,

I would like to know how can I set up a vbscript to copy-remove files based on len of a file name.

Ex:

I have a C:\from folder and a C:\to folder I would like to:
1-copy files which have a file name len= 14 (including extension) from C:\from to C:\to
2-delete all the files from C:\from which have a file name len>14 (including extension)

Log requirements:

Generate a log file "log-file.txt" in the same folder in which the script is generated
if  C:\from or C:\to  doesn't exist logouput=C:\from or C:\to  doesn't exist  
List the various files copied from C:\from to C:\to and all the files removed in C:\from
If a file which need to be removed is opened by another user logouput=unable to remove the file as it is opened by another user


Thank you in advance for your feedback.

Regards,
LVL 1
LD16Asked:
Who is Participating?
 
Bill PrewCommented:
Okay, sorry, small typo, this should correct.  Tested here now, works okay.

' Text file I/O constants
Const ForReading = 1
Const ForWriting = 2
Const ForAppending = 8

'Create the file system object for creating folders:
Set objFSO = CreateObject("Scripting.FileSystemObject")

' Define folders and files to work with
strScriptDir = objFSO.GetAbsolutePathName(".")
strLogFile = strScriptDir & "\Log-File.txt" 
strBaseDir = "C:\Users\LD\Desktop\Ongoing\from"
strDestDir = "C:\Users\LD\Desktop\Ongoing\to"
intFilterLength = 14

' Open log file for appending
Set objLog = objFSO.OpenTextFile(strLogFile, ForAppending, True)

' Make sure input folder exists
If Not objFSO.FolderExists(strBaseDir) Then
   objLog.WriteLine Now & " ERROR: Input folder """ & strBaseDir & """ does not exist."
   Wscript.Quit
End If

' Make sure output folder exists
If Not objFSO.FolderExists(strDestDir) Then
   objLog.WriteLine Now & " ERROR: Input folder """ & strDestDir & """ does not exist."
   Wscript.Quit
End If

' Process first level folders in source folder
For Each objFile In objFSO.GetFolder(strBaseDir).Files

   ' Make sure it matches the files we want to copy
   If Len(objFile.Name) = intFilterLength Then
      
      ' Copy file
      objLog.WriteLine Now & " INFO: Copying file """ & objFile.Path & """ to """ & strDestDir & """."
      objFSO.CopyFile objFile.Path, strDestDir & "\" & objFile.Name, True

      On Error Resume Next
      ' Delete file
      objFSO.DeleteFile objFile.Path, True
      If Err.Number <> 0 Then
         objLog.WriteLine Now & " WARNING: Could not delete file """ & objFile.Path & """."
         Err.Clear
      End If
      On Error Goto 0
   End If

Next

' Wrap up
objLog.Close

Open in new window

~bp
0
 
aikimarkCommented:
Does the "file name" include the file extension and the period separator in the file length calculation?
Does the "file name" include the path ("C:\From\") in the calculation of the length calculation?
0
 
LD16Author Commented:
File name include the separator.
File name doesn't include the path of the lenght calculation.
0
Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

 
Bill PrewCommented:
Give this a shot, I think it should do what you described.

' Text file I/O constants
Const ForReading = 1
Const ForWriting = 2
Const ForAppending = 8

'Create the file system object for creating folders:
Set objFSO = CreateObject("Scripting.FileSystemObject")

' Define folders and files to work with
strScriptDir = objFSO.GetAbsolutePathName(".")
strLogFile = strScriptDir & "\Log-File.txt" 
strBaseDir = "c:\from"
strDestDir = "c:\to"
intFilterLength = 14

' Open log file for appending
Set objLog = objFSO.OpenTextFile(strLogFile, ForAppending, True)

' Make sure input folder exists
If Not objFSO.FolderExists(strBaseDir) Then
   objLog.WriteLine Now & " ERROR: Input folder """ & strBaseDir & """ does not exist."
   Wscript.Quit
End If

' Make sure output folder exists
If Not objFSO.FolderExists(strDestDir) Then
   objLog.WriteLine Now & " ERROR: Input folder """ & strDestDir & """ does not exist."
   Wscript.Quit
End If

' Process first level folders in source folder
For Each objFile In objFSO.GetFolder(strBaseDir).Files

   ' Make sure it matches the files we want to copy
   If Len(objFile.Name) = intFilterLength Then
      
      ' Copy file
      objLog.WriteLine Now & " INFO: Copying file """ & objFile.Path & """ to """ & strDestFolder & """."
      objFSO.CopyFile objFile.Path, strDestFolder & "\" & objFile.Name, True

      On Error Resume Next
      ' Delete file
      objFSO.DeleteFile objFile.Path, True
      If Err.Number <> 0 Then
         objLog.WriteLine Now & " WARNING: Could not delete file """ & objFile.Path & """."
         Err.Clear
      On Error Goto 0
   End If

Next

' Wrap up
objLog.Close

Open in new window

~bp
0
 
LD16Author Commented:
Thank you! I will test it tomorrow.
0
 
LD16Author Commented:
Hello Bill,

Strange, I got a Expected end of statement error in line 12.

Thank you again for your help.
0
 
Bill PrewCommented:
Can you post up the exact script you ran (with any changes you made), as well as a capture of the error?  I don't see anything bad about the line 12 I posted.

~bp
0
 
LD16Author Commented:
Ok, I will send you all the info tomorrow.
0
 
LD16Author Commented:
Hello,
I didn't copy the ' char due to this I get an error message in line 12.

Now I have relaunch the code with all the following code:

' Text file I/O constants
Const ForReading = 1
Const ForWriting = 2
Const ForAppending = 8

'Create the file system object for creating folders:
Set objFSO = CreateObject("Scripting.FileSystemObject")

' Define folders and files to work with
strScriptDir = objFSO.GetAbsolutePathName(".")
strLogFile = strScriptDir & "\Log-File.txt" 
strBaseDir = "C:\Users\LD\Desktop\Ongoing\from"
strDestDir = "C:\Users\LD\Desktop\Ongoing\to"
intFilterLength = 14

' Open log file for appending
Set objLog = objFSO.OpenTextFile(strLogFile, ForAppending, True)

' Make sure input folder exists
If Not objFSO.FolderExists(strBaseDir) Then
   objLog.WriteLine Now & " ERROR: Input folder """ & strBaseDir & """ does not exist."
   Wscript.Quit
End If

' Make sure output folder exists
If Not objFSO.FolderExists(strDestDir) Then
   objLog.WriteLine Now & " ERROR: Input folder """ & strDestDir & """ does not exist."
   Wscript.Quit
End If

' Process first level folders in source folder
For Each objFile In objFSO.GetFolder(strBaseDir).Files

   ' Make sure it matches the files we want to copy
   If Len(objFile.Name) = intFilterLength Then
      
      ' Copy file
      objLog.WriteLine Now & " INFO: Copying file """ & objFile.Path & """ to """ & strDestFolder & """."
      objFSO.CopyFile objFile.Path, strDestFolder & "\" & objFile.Name, True

      On Error Resume Next
      ' Delete file
      objFSO.DeleteFile objFile.Path, True
      If Err.Number <> 0 Then
         objLog.WriteLine Now & " WARNING: Could not delete file """ & objFile.Path & """."
         Err.Clear
      On Error Goto 0
   End If

Next

' Wrap up
objLog.Close

Open in new window


And I get the following error message:

Capture.GIF
Thank you again for your help.
0
 
aikimarkCommented:
there is a missing "End If" statement after line 46
0
 
Bill PrewCommented:
So, corrected, that should look like:

' Text file I/O constants
Const ForReading = 1
Const ForWriting = 2
Const ForAppending = 8

'Create the file system object for creating folders:
Set objFSO = CreateObject("Scripting.FileSystemObject")

' Define folders and files to work with
strScriptDir = objFSO.GetAbsolutePathName(".")
strLogFile = strScriptDir & "\Log-File.txt" 
strBaseDir = "C:\Users\LD\Desktop\Ongoing\from"
strDestDir = "C:\Users\LD\Desktop\Ongoing\to"
intFilterLength = 14

' Open log file for appending
Set objLog = objFSO.OpenTextFile(strLogFile, ForAppending, True)

' Make sure input folder exists
If Not objFSO.FolderExists(strBaseDir) Then
   objLog.WriteLine Now & " ERROR: Input folder """ & strBaseDir & """ does not exist."
   Wscript.Quit
End If

' Make sure output folder exists
If Not objFSO.FolderExists(strDestDir) Then
   objLog.WriteLine Now & " ERROR: Input folder """ & strDestDir & """ does not exist."
   Wscript.Quit
End If

' Process first level folders in source folder
For Each objFile In objFSO.GetFolder(strBaseDir).Files

   ' Make sure it matches the files we want to copy
   If Len(objFile.Name) = intFilterLength Then
      
      ' Copy file
      objLog.WriteLine Now & " INFO: Copying file """ & objFile.Path & """ to """ & strDestFolder & """."
      objFSO.CopyFile objFile.Path, strDestFolder & "\" & objFile.Name, True

      On Error Resume Next
      ' Delete file
      objFSO.DeleteFile objFile.Path, True
      If Err.Number <> 0 Then
         objLog.WriteLine Now & " WARNING: Could not delete file """ & objFile.Path & """."
         Err.Clear
      End If
      On Error Goto 0
   End If

Next

' Wrap up
objLog.Close

Open in new window

~bp
0
 
LD16Author Commented:
Hello Bill,

Thank you for this revised version.
I tried your code but I don't have the action done and the log file is empty.

Thank you in advance for your feedback.
0
 
LD16Author Commented:
Perfect! It works!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.