SCRIPT TO MOVE FILES TO SUBDIR

Closed
BennyLod - Apr 6, 2010 at 10:17 AM
Richard.Williams Posts 25 Registration date Saturday November 7, 2009 Status Member Last seen July 18, 2012 - Apr 20, 2010 at 10:58 AM
Hello,

I'm looking to write a windows batch or vbs file that will look in a fixed directory, check for filename attributes, and move files in subdirectories by the attribute listed in the filename.

Example:

Directory C:\data contains
- various .zip files with codename that identify the subdirectory where we have to move the file into (Example filename: W0154_FF_BN.zip......W0157_FR_BN)
- 2 subdir (named FF and FR)

I need to move files that contain "FF" attribute in filename to directory C:\data\FF.....and files that contain "FR" attribute in filename to directory C:\data\FR

Please, can anybody help me?

Thanks a lot in advance!

Best regards.

Ben

2 replies

rizvisa1 Posts 4479 Registration date Thursday January 28, 2010 Status Contributor Last seen May 5, 2022 768
Apr 8, 2010 at 10:48 PM
Dim objFSO
Dim objFolder
Dim objFile
Dim fileName
Dim rootPath

    rootPath = "C:\Data\"
    
    'Set FSO object
    Set objFSO = CreateObject("Scripting.FileSystemObject")

    'Get the folder object associated with the directory
    Set objFolder = objFSO.GetFolder(rootPath)

    'Loop through the Files collection
    For Each objFile In objFolder.Files
        fileName = objFile.Name
    
        If (InStr(1, fileName, "_FF_") > 0) Then
            objFSO.MoveFile rootPath & fileName, rootPath & "FF\"
        ElseIf (InStr(1, fileName, "_FR_") > 0) Then
            objFSO.MoveFile rootPath & fileName, rootPath & "FR\"
        End If
    Next

    Set objFile = Nothing
    Set objFolder = Nothing
    Set objFSO = Nothing


0
Richard.Williams Posts 25 Registration date Saturday November 7, 2009 Status Member Last seen July 18, 2012 14
Apr 20, 2010 at 10:58 AM
Biterscripting script below. It collects a list of .zip files in C:/data directory. It gets the FF or FR by extracting the text between the first and second underscore (_) in the file name. Then moves the file to that subdirectory.


# Script FFFR.txt
var str list, path, file, subdir
cd "C:/data"
lf -n "*.zip" > $list
while ($list <> "")
do
    lex "1" $list > $path
    stex -p "^/^l[" $path > $file
    stex -p "^_^[" $file > $subdir ; stex "[^_^" $subdir > null
    # $subdir now has FF or FR.
    system move ("\""+$path+"\"") ("\""+$subdir+"\"")
done



Copy and paste the script in file C:/Scripts/FFFR.txt, start biterscripting, enter this command.



script "C:/Scripts/FFFR.txt"



If need to do this daily, schedule this command in task scheduler.


"C:/biterscripting/biterscripting.exe"  "C:/Scripts/FFFR.txt"



Don't have biterscripting ? Get it. It's free - good for this stuff. There are several sites to download from. Just google up the word 'biterscripting'.
-1