Script - Incorporating a 2nd script into script 1 - Renaming and Moving File

Good Morning,

You have provided this code before - process to find a particular file and append the filename

Option Explicit

' Define files and folders to work with (NO TRAILING BACKSLASH)
Const cBaseDir = "D:\Audits\[[DATE]]"
Const cControlFile = "D:\WorkYardInspections\Output\WYIN_Data.txt"
Const cDelim = ","

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

' Declare global variables
Dim objFSO, strBaseDir, strControlFile, strDate
Dim arrControl, strControl, arrFields, i
Dim strOldName, strNewName, strOldPath, strNewPath

' Create file system object
Set objFSO = WScript.CreateObject("Scripting.FileSystemObject")

' Get date in YYYYMMDD format
strDate = Year(Now) & Right("0" & Month(Now), 2) & Right("0" & Day(Now), 2)

' Resolve file paths
strBaseDir = objFSO.GetAbsolutePathname(Replace(cBaseDir, "[[DATE]]", strDate, 1, -1, vbTextCompare))
strControlFile = objFSO.GetAbsolutePathname(cControlFile)

' Make sure base folder exists
If Not objFSO.FolderExists(strBaseDir) Then
    Wscript.Echo Now & " ERROR: Base folder does not exist - " & strBaseDir
    Wscript.Quit
End If

' Make sure control file exists
If Not objFSO.FileExists(strControlFile) Then
    Wscript.Echo Now & " ERROR: List file does not exist - " & strControlFile
    Wscript.Quit
End If

' Load csv file into array
With objFSO.OpenTextFile(strControlFile, ForReading)
    arrControl = Split(.ReadAll, vbCrLf)
    .Close
End With

' Loop over each control record and process as needed (skip header row)
For i = 1 To UBound(arrControl)

    ' Split line at delimiter, gather fields
    arrFields = CSVParse(arrControl(i))
    If UBound(arrFields) = 1 Then
        strOldName = arrFields(0) & ".pdf"
        strNewName = arrFields(0) & "_" & CleanFilename(arrFields(1)) & ".pdf"
    
        ' Build full paths for old and new file names
        strOldPath = strBaseDir & "\" & strOldName
        strNewPath = strBaseDir & "\" & strNewName
    
        ' If old file exists, and new name is not used, rename.  Otherwise display warning.
        If objFSO.FileExists(strOldPath) Then
            If Not objFSO.FileExists(strNewPath) Then
                objFSO.MoveFile strOldPath, strNewPath
            Else
                Wscript.Echo Now & " WARNING: New file already exists - " & strNewName
            End If
        Else
            Wscript.Echo Now & " WARNING: Old file does not exist - " & strOldName
        End If
        End If
Next

' Function to parse comma delimited line and return array of field values.
Function CSVParse(ByVal strLine)
    Dim arrFields
    Dim blnIgnore
    Dim intFieldCount
    Dim intCursor
    Dim intStart
    Dim strChar
    Dim strValue

    Const QUOTE = """"
    Const QUOTE2 = """"""

    ' Check for empty string and return empty array.
    If (Len(Trim(strLine)) = 0) then
        CSVParse = Array()
        Exit Function
    End If

    ' Initialize.
    blnIgnore = False
    intFieldCount = 0
    intStart = 1
    arrFields = Array()

    ' Add "," to delimit the last field.
    strLine = strLine & ","

    ' Walk the string.
    For intCursor = 1 To Len(strLine)
        ' Get a character.
        strChar = Mid(strLine, intCursor, 1)
        Select Case strChar
            Case QUOTE
                ' Toggle the ignore flag.
                blnIgnore = Not blnIgnore
            Case ","
                If Not blnIgnore Then
                    ' Add element to the array.
                    ReDim Preserve arrFields(intFieldCount)
                    ' Makes sure the "field" has a non-zero length.
                    If (intCursor - intStart > 0) Then
                        ' Extract the field value.
                        strValue = Mid(strLine, intStart, _
                            intCursor - intStart)
                        ' If it's a quoted string, use Mid to
                        ' remove outer quotes and replace inner
                        ' doubled quotes with single.
                        If (Left(strValue, 1) = QUOTE) Then
                            arrFields(intFieldCount) = _
                                Replace(Mid(strValue, 2, _
                                Len(strValue) - 2), QUOTE2, QUOTE)
                        Else
                            arrFields(intFieldCount) = strValue
                        End If
                    Else
                        ' An empty field is an empty array element.
                        arrFields(intFieldCount) = Empty
                    End If
                    ' increment for next field.
                    intFieldCount = intFieldCount + 1
                    intStart = intCursor + 1
                End If
        End Select
    Next
    ' Return the array.
    CSVParse = arrFields
End Function

Function CleanFilename(strName)
    Dim arrIllegal, strIllegal, i
    
    arrIllegal = Array("<",">",":","""","/","\","|","?","*"," ")
    CleanFilename = Trim(strName)
    For Each strIllegal In arrIllegal
        CleanFilename = Replace(CleanFilename, strIllegal, "_")
    Next
    For i = 0 To 31
        CleanFilename = Replace(CleanFilename, Chr(i), "_")
    Next
End Function

Open in new window


Once this process has been completed I want to be able to move the renamed file to another location.

I have been using this script to do the move

@echo off

REM Get current date in YYYYMMDD
set Stamp=
for /f "tokens=* skip=1" %%A in ('wmic os get LocalDateTime') do (
    if not defined Stamp set Stamp=%%A
)
set Stamp=%Stamp:~0,8%

D:
CD \
net use T: \\TestDrive\Data Password /user:admin
copy /Y "D:\Audits\%Stamp%\WYIN*.pdf" "T:\inbox\"
net use t: /del
quit

Open in new window


I want to incorporate the 2nd script -moving the file into the 1st script - so it renames and then moves it
NICK COLLINSAsked:
Who is Participating?
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.

Bill PrewIT / Software Engineering ConsultantCommented:
I think this should do what you want.

Option Explicit

' Define files and folders to work with (NO TRAILING BACKSLASH)
Const cBaseDir = "D:\Audits\[[DATE]]"
Const cDestDir = "T:\Inbox"
Const cControlFile = "D:\WorkYardInspections\Output\WYIN_Data.txt"
Const cDelim = ","

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

' Declare global variables
Dim objFSO, strBaseDir, strControlFile, strDate
Dim arrControl, strControl, arrFields, i
Dim strOldName, strNewName, strOldPath, strNewPath, strDestDir

' Create file system object
Set objFSO = WScript.CreateObject("Scripting.FileSystemObject")

' Get date in YYYYMMDD format
strDate = Year(Now) & Right("0" & Month(Now), 2) & Right("0" & Day(Now), 2)

' Resolve file paths
strBaseDir = objFSO.GetAbsolutePathname(Replace(cBaseDir, "[[DATE]]", strDate, 1, -1, vbTextCompare))
strControlFile = objFSO.GetAbsolutePathname(cControlFile)
strDestDir = objFSO.GetAbsolutePathname(cDestDir)

' Make sure base folder exists
If Not objFSO.FolderExists(strBaseDir) Then
    Wscript.Echo Now & " ERROR: Base folder does not exist - " & strBaseDir
    Wscript.Quit
End If

' Make sure dest folder exists
If Not objFSO.FolderExists(strDestDir) Then
    Wscript.Echo Now & " ERROR: Destination folder does not exist - " & strDestDir
    Wscript.Quit
End If

' Make sure control file exists
If Not objFSO.FileExists(strControlFile) Then
    Wscript.Echo Now & " ERROR: List file does not exist - " & strControlFile
    Wscript.Quit
End If

' Load csv file into array
With objFSO.OpenTextFile(strControlFile, ForReading)
    arrControl = Split(.ReadAll, vbCrLf)
    .Close
End With

' Loop over each control record and process as needed (skip header row)
For i = 1 To UBound(arrControl)

    ' Split line at delimiter, gather fields
    arrFields = CSVParse(arrControl(i))
    If UBound(arrFields) = 1 Then
        strOldName = arrFields(0) & ".pdf"
        strNewName = arrFields(0) & "_" & CleanFilename(arrFields(1)) & ".pdf"
    
        ' Build full paths for old and new file names
        strOldPath = strBaseDir & "\" & strOldName
        strNewPath = strBaseDir & "\" & strNewName
    
        ' If old file exists, and new name is not used, rename.  Otherwise display warning.
        If objFSO.FileExists(strOldPath) Then
            If Not objFSO.FileExists(strNewPath) Then
                objFSO.MoveFile strOldPath, strNewPath
                objFSO.CopyFile strOldPath, strDestDir & "\" & strNewName
            Else
                Wscript.Echo Now & " WARNING: New file already exists - " & strNewName
            End If
        Else
            Wscript.Echo Now & " WARNING: Old file does not exist - " & strOldName
        End If
        End If
Next

' Function to parse comma delimited line and return array of field values.
Function CSVParse(ByVal strLine)
    Dim arrFields
    Dim blnIgnore
    Dim intFieldCount
    Dim intCursor
    Dim intStart
    Dim strChar
    Dim strValue

    Const QUOTE = """"
    Const QUOTE2 = """"""

    ' Check for empty string and return empty array.
    If (Len(Trim(strLine)) = 0) then
        CSVParse = Array()
        Exit Function
    End If

    ' Initialize.
    blnIgnore = False
    intFieldCount = 0
    intStart = 1
    arrFields = Array()

    ' Add "," to delimit the last field.
    strLine = strLine & ","

    ' Walk the string.
    For intCursor = 1 To Len(strLine)
        ' Get a character.
        strChar = Mid(strLine, intCursor, 1)
        Select Case strChar
            Case QUOTE
                ' Toggle the ignore flag.
                blnIgnore = Not blnIgnore
            Case ","
                If Not blnIgnore Then
                    ' Add element to the array.
                    ReDim Preserve arrFields(intFieldCount)
                    ' Makes sure the "field" has a non-zero length.
                    If (intCursor - intStart > 0) Then
                        ' Extract the field value.
                        strValue = Mid(strLine, intStart, _
                            intCursor - intStart)
                        ' If it's a quoted string, use Mid to
                        ' remove outer quotes and replace inner
                        ' doubled quotes with single.
                        If (Left(strValue, 1) = QUOTE) Then
                            arrFields(intFieldCount) = _
                                Replace(Mid(strValue, 2, _
                                Len(strValue) - 2), QUOTE2, QUOTE)
                        Else
                            arrFields(intFieldCount) = strValue
                        End If
                    Else
                        ' An empty field is an empty array element.
                        arrFields(intFieldCount) = Empty
                    End If
                    ' increment for next field.
                    intFieldCount = intFieldCount + 1
                    intStart = intCursor + 1
                End If
        End Select
    Next
    ' Return the array.
    CSVParse = arrFields
End Function

Function CleanFilename(strName)
    Dim arrIllegal, strIllegal, i
    
    arrIllegal = Array("<",">",":","""","/","\","|","?","*"," ")
    CleanFilename = Trim(strName)
    For Each strIllegal In arrIllegal
        CleanFilename = Replace(CleanFilename, strIllegal, "_")
    Next
    For i = 0 To 31
        CleanFilename = Replace(CleanFilename, Chr(i), "_")
    Next
End Function

Open in new window


»bp
NICK COLLINSAuthor Commented:
Thank you very much..

In the last script there was a process to map the 'T' drive using a username and password to map the drive..

as the T drive is temp just for moving
Bill PrewIT / Software Engineering ConsultantCommented:
I think you should be able to use the actual network path there, as long as it doesn't require a new password to be entered, without a need to map, try this change:

Const cDestDir = "\\TestDrive\Data"

Open in new window

However if the password has to be entered then I believe you will have to map the drive before running the VBS script.

You could execute the DOS command to do the mapping from VBS, but it seems like it would be easier just to do the mapping in a BAT script that then runs the VBS to me.


»bp

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
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
Bat

From novice to tech pro — start learning today.