none
A script for copying certain files in subfolders to a new location RRS feed

  • Question

  • Hi,

    I have found a script on your forum and developed it to find files from a location that is read from txt files, the one thing I cant do is get it to look for the image files in the subfolders.

    The script will look in C:\CDData\7200000010\180611\ but the subfolders from the folder 180311 are random i.e 0000, 0001, 0010 and it is these folders that contain the images I want to move that are on my list.

    Is there a wildcard way so the subfolders of the date folder (180611) can be looked in to get the images that are listed in the txt file.

    Any Help would be much appreciated, here is the code:

    'Start

     Option Explicit

    'Read Volume name

     Dim InputFile
     Dim InputFile_1
     Dim FSO45, oFile
     Dim FSO46, oFile_46, strData_46
     Dim strData
     Dim fso991, fso946
     Dim fso23
     Dim shl
     Dim path
     Dim exists
     Dim objFSO_1
     
     InputFile = "C:\Transfer_Scripts_gb-bol-pd0102\Loader\CONSUMER\Temp_Files\First_Folder.txt"
     Set FSO45 = CreateObject("Scripting.FileSystemObject")
     Set oFile = FSO45.OpenTextFile(InputFile)
     strData = oFile.ReadAll
     fso991 = strData
     
     InputFile_1 = "C:\Transfer_Scripts_gb-bol-pd0102\Loader\CONSUMER\Temp_Files\Second_Folder.txt"
     Set FSO46 = CreateObject("Scripting.FileSystemObject")
     Set oFile_46 = FSO46.OpenTextFile(InputFile_1)
     strData_46 = oFile_46.ReadAll
     fso946 = strData_46
     
    ' The source path for the copy operation.
     Dim strSourceFolder
     Dim strSourceFolder_1
     Dim strSourceFolder_2
     Dim strSourceFolder_3
     
     ' example of folder desination C:\CDData\7200000010\180611\???
     strSourceFolder = "C:\CDData\" & fso991 & "\" & fso946 & (MISSING PART OF SOURCE FOLDER SUB FOLDERS)
     
    ' The target path for the copy operation.
     Dim strTargetFolder
     strTargetFolder = "C:\Transfer_Scripts_gb-bol-pd0102\Loader\CONSUMER\Output\"
     
    ' The list of files to copy. Should be a text file with one file on each row. No paths - just file name.
     Const strFileList = "C:\Transfer_Scripts_gb-bol-pd0102\Loader\CONSUMER\List\Image_List.txt"

    ' Should files be overwriten if they already exist? TRUE or FALSE.
     Const blnOverwrite = FALSE

     Dim objFSO
     Set objFSO = CreateObject("Scripting.FileSystemObject")

     Const ForReading = 1
     Dim objFileList
     Set objFileList = objFSO.OpenTextFile(strFileList, ForReading, False)

     Dim strFileToCopy, strSourceFilePath, strTargetFilePath
     
     On Error Resume Next
     Do Until objFileList.AtEndOfStream
     
     
    ' Read next line from file list and build filepaths
         strFileToCopy = objFileList.Readline
         strSourceFilePath = objFSO.BuildPath(strSourceFolder, strFileToCopy)
         
         strTargetFilePath = objFSO.BuildPath(strTargetFolder, strFileToCopy)
         
         Wscript.Echo strSourceFilePath

    ' Copy file to specified target folder.
         Err.Clear
         objFSO.CopyFile strSourceFilePath, strTargetFilePath, blnOverwrite
         
         If Err.Number = 0 Then

    ' File copied successfully

         Else

         End If
     Loop

    'End

    • Moved by Bill_Stewart Monday, July 30, 2018 9:25 PM This is not "debug/fix/rewrite my script for me" forum
    Wednesday, June 13, 2018 6:09 PM

All replies

  • You really need to start by learning how to post code in a technical forum.  There is a code posting tool on the edit bar.  Edit you original post and post the code using the tool.  As posted it is pretty much unreadable in most browsers.

    There is no wildcard in VBScript.


    \_(ツ)_/


    • Edited by jrv Wednesday, June 13, 2018 6:16 PM
    Wednesday, June 13, 2018 6:15 PM
  • Hi,

    Sorry about that how is this:

    'Start
    
     Option Explicit
    
    'Read Volume name
    
     Dim InputFile 
      Dim InputFile_1
      Dim FSO45, oFile 
      Dim FSO46, oFile_46, strData_46
      Dim strData
      Dim fso991, fso946
      Dim fso23
      Dim shl
      Dim path
      Dim exists
      Dim objFSO_1
      
      InputFile = "C:\Transfer_Scripts_gb-bol-pd0102\Loader\CONSUMER\Temp_Files\First_Folder.txt" 
      Set FSO45 = CreateObject("Scripting.FileSystemObject") 
      Set oFile = FSO45.OpenTextFile(InputFile) 
      strData = oFile.ReadAll 
      fso991 = strData
      
      InputFile_1 = "C:\Transfer_Scripts_gb-bol-pd0102\Loader\CONSUMER\Temp_Files\Second_Folder.txt" 
      Set FSO46 = CreateObject("Scripting.FileSystemObject") 
      Set oFile_46 = FSO46.OpenTextFile(InputFile_1) 
      strData_46 = oFile_46.ReadAll 
      fso946 = strData_46
      
     ' The source path for the copy operation.
      Dim strSourceFolder
      Dim strSourceFolder_1
      Dim strSourceFolder_2
      Dim strSourceFolder_3
      
      ' example of folder desination C:\CDData\7200000010\180611\???
      strSourceFolder = "C:\CDData\" & fso991 & "\" & fso946 & (MISSING PART OF SOURCE FOLDER SUB FOLDERS)
      
     ' The target path for the copy operation.
      Dim strTargetFolder
      strTargetFolder = "C:\Transfer_Scripts_gb-bol-pd0102\Loader\CONSUMER\Output\"
      
     ' The list of files to copy. Should be a text file with one file on each row. No paths - just file name.
      Const strFileList = "C:\Transfer_Scripts_gb-bol-pd0102\Loader\CONSUMER\List\Image_List.txt"
    
    ' Should files be overwriten if they already exist? TRUE or FALSE.
      Const blnOverwrite = FALSE
    
     Dim objFSO
      Set objFSO = CreateObject("Scripting.FileSystemObject")
    
     Const ForReading = 1
      Dim objFileList
      Set objFileList = objFSO.OpenTextFile(strFileList, ForReading, False)
    
     Dim strFileToCopy, strSourceFilePath, strTargetFilePath
      
      On Error Resume Next
      Do Until objFileList.AtEndOfStream
      
      
     ' Read next line from file list and build filepaths
          strFileToCopy = objFileList.Readline
          strSourceFilePath = objFSO.BuildPath(strSourceFolder, strFileToCopy)
          
          strTargetFilePath = objFSO.BuildPath(strTargetFolder, strFileToCopy)
          
          Wscript.Echo strSourceFilePath
    
    ' Copy file to specified target folder.
          Err.Clear
          objFSO.CopyFile strSourceFilePath, strTargetFilePath, blnOverwrite
          
          If Err.Number = 0 Then
    
    ' File copied successfully
    
         Else
    
    
         End If
      Loop
    
    'End

    Wednesday, June 13, 2018 6:22 PM
  • hi,

    Is there away that you don't have to define the last folder but the script just search through them?

    Wednesday, June 13, 2018 6:23 PM
  • you have to create a test function within the loop to test if the file held is an image file. 

    if last 4 characters = jpeg then copy

    if you have multiple formats you will have to account for each format.

    edit: the file list is kinda moot; you know which files you want you just don't know how to get them? sounds like a fucked up process that is hindering your script.
    • Edited by The Grim Wednesday, June 13, 2018 6:26 PM
    Wednesday, June 13, 2018 6:24 PM
  • hi,

    Is there away that you don't have to define the last folder but the script just search through them?

    You did not use the code posting tool provided.  Colorized code does not display correctly and cannot be copied correctly which is why we have the code posting tool.  It is on the edit bar and will format the code for copying and reading.

    We also do not customize code you have found in the Internet.

    I recommend learning PowerShell as this can be done in PS in a couple of lines.  PS allows wild cards.


    \_(ツ)_/

    Wednesday, June 13, 2018 6:26 PM
  • Hi,

    I did use the tool but it kept saying it was spam and this code is from this site but I have adapted it to my needs and only need help on the last part, I am not asking for answers just hints at were to look.

    Wednesday, June 13, 2018 6:29 PM
  • Hi,

    The image files are only .TIFF files, the system I am using generates the image name's in a txt file then the actual images are generated in the folder structure C:\CDData\[volume number][Volume count]\[YYMMDD]\[image folder name] e.g C:\CDData\7200000001\180613\0000, the last folder goes from 0000 to 9999 but only two [image folder names] mite be created e.g 0010, 0017 that why I cant define the last folder. If there was no third folder I would have this solved but I cant change the process the images are made.

    Wednesday, June 13, 2018 6:36 PM
  • Hi,

    I did use the tool but it kept saying it was spam and this code is from this site but I have adapted it to my needs and only need help on the last part, I am not asking for answers just hints at were to look.

    k; here is a hint. the script assumes a static location. 

    gut the basic copy from source dest; add a root folder to start in and innumerate objects within that folder and check against the txt file.

    Wednesday, June 13, 2018 6:37 PM
  • The tool is on the edit bar.  It is the <> icon. You did not use the tool.


    \_(ツ)_/

    Wednesday, June 13, 2018 6:44 PM
  • The code, as posted, has numerous logic and coding errors.  You will need to fix them.

    This is what correctly posted code lloks like.

    Option Explicit Dim InputFile Dim InputFile_1 Dim FSO45, oFile Dim FSO46, oFile_46, strData_46 Dim strData Dim fso991, fso946 Dim fso23 Dim shl Dim path Dim exists Dim objFSO_1 InputFile = "C:\Transfer_Scripts_gb-bol-pd0102\Loader\CONSUMER\Temp_Files\First_Folder.txt" Set FSO45 = CreateObject("Scripting.FileSystemObject") Set oFile = FSO45.OpenTextFile(InputFile) strData = oFile.ReadAll fso991 = strData InputFile_1 = "C:\Transfer_Scripts_gb-bol-pd0102\Loader\CONSUMER\Temp_Files\Second_Folder.txt" Set FSO46 = CreateObject("Scripting.FileSystemObject") Set oFile_46 = FSO46.OpenTextFile(InputFile_1) strData_46 = oFile_46.ReadAll fso946 = strData_46 ' The source path for the copy operation. Dim strSourceFolder Dim strSourceFolder_1 Dim strSourceFolder_2 Dim strSourceFolder_3 ' example of folder desination C:\CDData\7200000010\180611\??? strSourceFolder = "C:\CDData\" & fso991 & "\" & fso946 & (MISSING PART OF SOURCE FOLDER SUB FOLDERS) ' The target path for the copy operation. Dim strTargetFolder strTargetFolder = "C:\Transfer_Scripts_gb-bol-pd0102\Loader\CONSUMER\Output\" ' The list of files to copy. Should be a text file with one file on each row. No paths - just file name. Const strFileList = "C:\Transfer_Scripts_gb-bol-pd0102\Loader\CONSUMER\List\Image_List.txt" ' Should files be overwriten if they already exist? TRUE or FALSE. Const blnOverwrite = FALSE Dim objFSO Set objFSO = CreateObject("Scripting.FileSystemObject") Const ForReading = 1 Dim objFileList Set objFileList = objFSO.OpenTextFile(strFileList, ForReading, False) Dim strFileToCopy, strSourceFilePath, strTargetFilePath On Error Resume Next Do Until objFileList.AtEndOfStream ' Read next line from file list and build filepaths strFileToCopy = objFileList.Readline strSourceFilePath = objFSO.BuildPath(strSourceFolder, strFileToCopy) strTargetFilePath = objFSO.BuildPath(strTargetFolder, strFileToCopy) Wscript.Echo strSourceFilePath

    ' Copy file to specified target folder. objFSO.CopyFile strSourceFilePath, strTargetFilePath, blnOverwrite If Err.Number = 0 Then ' File copied successfully Else End If Loop

    Notice the code is readable and doesn't have bad formatting.  The code ignores all errors so you will not know why it is failing.

    You will need to use an "if" filter to select the folders you want.


    \_(ツ)_/


    • Edited by jrv Wednesday, June 13, 2018 6:52 PM
    Wednesday, June 13, 2018 6:51 PM
  • 'Start
    
     Option Explicit
    
    'Read Volume name
    
     Dim InputFile 
      Dim InputFile_1
      Dim FSO45, oFile 
      Dim FSO46, oFile_46, strData_46
      Dim strData
      Dim fso991, fso946
      Dim fso23
      Dim shl
      Dim path
      Dim exists
      Dim objFSO_1
      
      InputFile = "C:\Transfer_Scripts_gb-bol-pd0102\Loader\CONSUMER\Temp_Files\First_Folder.txt" 
      Set FSO45 = CreateObject("Scripting.FileSystemObject") 
      Set oFile = FSO45.OpenTextFile(InputFile) 
      strData = oFile.ReadAll 
      fso991 = strData
      
      InputFile_1 = "C:\Transfer_Scripts_gb-bol-pd0102\Loader\CONSUMER\Temp_Files\Second_Folder.txt" 
      Set FSO46 = CreateObject("Scripting.FileSystemObject") 
      Set oFile_46 = FSO46.OpenTextFile(InputFile_1) 
      strData_46 = oFile_46.ReadAll 
      fso946 = strData_46
      
     ' The source path for the copy operation.
      Dim strSourceFolder
      Dim strSourceFolder_1
      Dim strSourceFolder_2
      Dim strSourceFolder_3
      
      ' example of folder desination C:\CDData\7200000010\180611\???
      strSourceFolder = "C:\CDData\" & fso991 & "\" & fso946 & (MISSING PART OF SOURCE FOLDER SUB FOLDERS)
      
     ' The target path for the copy operation.
      Dim strTargetFolder
      strTargetFolder = "C:\Transfer_Scripts_gb-bol-pd0102\Loader\CONSUMER\Output\"
      
     ' The list of files to copy. Should be a text file with one file on each row. No paths - just file name.
      Const strFileList = "C:\Transfer_Scripts_gb-bol-pd0102\Loader\CONSUMER\List\Image_List.txt"
    
    ' Should files be overwriten if they already exist? TRUE or FALSE.
      Const blnOverwrite = FALSE
    
     Dim objFSO
      Set objFSO = CreateObject("Scripting.FileSystemObject")
    
     Const ForReading = 1
      Dim objFileList
      Set objFileList = objFSO.OpenTextFile(strFileList, ForReading, False)
    
     Dim strFileToCopy, strSourceFilePath, strTargetFilePath
      
      On Error Resume Next
      Do Until objFileList.AtEndOfStream
      
      
     ' Read next line from file list and build filepaths
          strFileToCopy = objFileList.Readline
          strSourceFilePath = objFSO.BuildPath(strSourceFolder, strFileToCopy)
          
          strTargetFilePath = objFSO.BuildPath(strTargetFolder, strFileToCopy)
          
          Wscript.Echo strSourceFilePath
    
    ' Copy file to specified target folder.
          Err.Clear
          objFSO.CopyFile strSourceFilePath, strTargetFilePath, blnOverwrite
          
          If Err.Number = 0 Then
    
    ' File copied successfully
    
         Else
    
    
         End If
      Loop
    
    'End
    

    Wednesday, June 13, 2018 6:53 PM
  • Hi,

    Thanks for that I will look into it, I was also looking in to having the full path and filename in the list.txt and then do a read copy paste.

    Wednesday, June 13, 2018 6:55 PM
  • If we remove all unnecessary lines of code and clean up the odd redundant bits this is what you code looks like.

    InputFile = "C:\Transfer_Scripts_gb-bol-pd0102\Loader\CONSUMER\Temp_Files\First_Folder.txt" 
    InputFile_1 = "C:\Transfer_Scripts_gb-bol-pd0102\Loader\CONSUMER\Temp_Files\Second_Folder.txt" 
    ' The list of files to copy. Should be a text file with one file on each row. No paths - just file name.
    Const strFileList = "C:\Transfer_Scripts_gb-bol-pd0102\Loader\CONSUMER\List\Image_List.txt"
    Set fso = CreateObject("Scripting.FileSystemObject")
    Set oFile = fso.OpenTextFile(InputFile) fso991 = oFile.ReadLine() Set oFile_46 = fso.OpenTextFile(InputFile_1) fso946 = oFile_46.ReadLine() ' example of folder desination C:\CDData\7200000010\180611\??? strSourceFolder = "C:\CDData\" & fso991 & "\" & fso946 & (MISSING PART OF SOURCE FOLDER SUB FOLDERS) ' The target path for the copy operation. strTargetFolder = "C:\Transfer_Scripts_gb-bol-pd0102\Loader\CONSUMER\Output\" Set objFileList = fso.OpenTextFile(strFileList) On Error Resume Next Do Until objFileList.AtEndOfStream strFileToCopy = objFileList.Readline() strSourceFilePath = fso.BuildPath(strSourceFolder, strFileToCopy) strTargetFilePath = fso.BuildPath(strTargetFolder, strFileToCopy) Wscript.Echo strSourceFilePath fso.CopyFile strSourceFilePath, strTargetFilePath, True If Err.Number <> 0 Then WScript.Echo Err.Description End If Loop

    I did not fix the logic and design errorx

    What you are trying to do makes no sense.  You cannot add the contents of a text file to a string path.  It won't work as expected.


    \_(ツ)_/




    • Edited by jrv Wednesday, June 13, 2018 7:02 PM
    Wednesday, June 13, 2018 6:58 PM
  • If you do not know VBScript please drop this and learn PowerShell.  In effect you are asking us to use bad code to guess at  what you are trying to do.  Your question is not about script but is about how to modify a script that you don't understand. 

    Do not waste your time trying to get this code fixed.  Learn Power Shell and write your own script.


    \_(ツ)_/

    Wednesday, June 13, 2018 7:04 PM
  • Hi,

    Thank you for cleaning it up I will know how to present an issue next time. I will look in to the 'IF' filter. Your help is much appreciated.

    Wednesday, June 13, 2018 7:07 PM
  • This is how easy it can be in PowerShell:

     dir  C:\CDData\7200000010\180611\* -Directory


    \_(ツ)_/

    Wednesday, June 13, 2018 7:12 PM