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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
Bill PrewIT / Software Engineering ConsultantCommented:
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
Become a Certified Penetration Testing Engineer

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

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 PrewIT / Software Engineering ConsultantCommented:
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 PrewIT / Software Engineering ConsultantCommented:
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
Bill PrewIT / Software Engineering ConsultantCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
LD16Author Commented:
Perfect! It works!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
VB Script

From novice to tech pro — start learning today.