Issues with unmapping a drive letter

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
SIMMS7400
Posts: 546
Joined: 07 Jan 2016 07:47

Issues with unmapping a drive letter

#1 Post by SIMMS7400 » 12 Aug 2017 05:16

Hi Folks -

I'm having an issue trying to un-map a drive letter just prior to my script exiting.

You'll see on lines 235 & 236 I un-map (try to) the drive letters that I set in the beginning. However, only the first one disconnects (%MD%). I'm prompted with the screen that says 'there are still pending searches....". Therefore, I placed the /y switch to prevent that. But, I'm was never prompted for the second drive letter (%MD1%). Does anyone have any idea why?

Thanks!

Code: Select all

:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

SETLOCAL ENABLEDELAYEDEXPANSION

::-- Set Working Directory as Script Path --::

cd /d "%~dp0"

::-- Ensure Process can be executed --::

CALL :CHECK_DAY

::-- Get drive letter validate department share --::

CALL :CHECK_DRIVE

::-- Set Environment File --::
CALL _env.cmd "%DL%"

::-- Set Process Variables --::

SET "PROC_LOGPATH=HC_SUB_Logs\"
SET "PROC_ERRORPATH=HC_SUB_Errors\"

::-- Establish INTRAPATH variables & directories --::

SET "INTRAPATH=%MAINPATH%%LOGPATH%%PROC_LOGPATH%"
SET "ERRORINTRAPATH=%MAINPATH%%ERRORPATH%%PROC_ERRORPATH%"

FOR %%F IN ( "%INTRAPATH%" "%ERRORINTRAPATH%" ) DO IF NOT EXIST %%F MKDIR %%F

::-- Parse %TIME% variable in save-able format --::

FOR /f "tokens=1-2 delims=/:" %%A in ("%TIME%")   DO ( SET TIMESTAMP=%%A%%B)
FOR /f "tokens=* delims= " %%C in ("%TIMESTAMP%") DO ( SET TIMESTAMP=%%C)

::-- Establish Log & Error files --::

SET "LOGFILE=%INTRAPATH%%date:~-4,4%%date:~-10,2%%date:~-7,2%_%TIMESTAMP%_%~n0.log"
SET "ERRORFILE=%ERRORINTRAPATH%%date:~-4,4%%date:~-10,2%%date:~-7,2%_%TIMESTAMP%_%~n0.log"

::-- Begin Script Processing --::

>>"%LOGFILE%" (
ECHO ********************************************************
ECHO %~n0 Starting at %TIME%                                 
ECHO ********************************************************
)
::-- Declare Process Variables --::

SET "CREDS=MPI\sample password"

SET "EXT=xlsx"

SET "HC_EXCEL_SUBPATH=%DL%\Finance & Accounting\FP&A Files\R&D Function & Site Support\Monthly Reporting\Models\Headcount Submission\"
SET "HC_ACT_SUBF=Headcount Actuals Submission Files\Horizon_Import\"
SET "BATCHPATH=%DL%\Finance & Accounting\FP&A Files\R&D Function & Site Support\Monthly Reporting\Models\Automation\Hyperion_Batch\Scripts\Batch"

SET "HORIZON_URL_STAG=https://horizon.tpna.com/sites/rdfinance/TM1/DATA/Ascend_Headcount/Ascend_Headcount_Staging"
SET "HORIZON_URL_IMP=https://horizon.tpna.com/sites/rdfinance/TM1/DATA/Ascend_Headcount"

::-- Set Error Variables --::
SET "NF1="
SET "NF2="
SET "NF3="

::-- Set Count Variables --::
SET /A "CNT1=0"
SET /A "CNT2=0"

>>"%LOGFILE%" (
ECHO ********************************************************
ECHO Map Horizon URL's as drives for transfer process...              
ECHO ********************************************************
ECHO.                                          
)

ECHO Mapping Horizon URL's as drives for transfer process...
PING -n 4 127.0.0.1>nul

SETLOCAL ENABLEDELAYEDEXPANSION

FOR %%D IN ( b d e f g h i j k l m n o p q r s t u v w x y z ) DO (

   IF NOT EXIST %%D:\ (
      SET "MD=%%D"
      NET USE !MD!: "%HORIZON_URL_STAG%" /USER:%CREDS% >nul 2>&1 && (
         ECHO Success : Map Horizon Stage URL >>"%LOGFILE%"
         ECHO      %HORIZON_URL_STAG% >>"%LOGFILE%"
         ECHO.>>"%LOGFILE%"
         CALL :CHCK_MD
         ) || (
         ECHO Failed : Map Horizon Stage URL >>"%LOGFILE%"
         ECHO      %HORIZON_URL_STAG% >>"%LOGFILE%"
         ECHO.>>"%LOGFILE%"
         SET "NF1=1"& GOTO AbnormalExit
         )

         FOR %%E IN ( b d e f g h i j k l m n o p q r s t u v w x y z ) DO (
            
            IF NOT EXIST %%E:\ (
               SET "MD1=%%E"
               NET USE !MD1!: "%HORIZON_URL_IMP%" /USER:%CREDS% >nul 2>&1 && (
                  ECHO Success : Map Horizon Import URL >>"%LOGFILE%"
                  ECHO      %HORIZON_URL_IMP% >>"%LOGFILE%"
                  ECHO.>>"%LOGFILE%"
                  GOTO BREAK
                  ) || (
                  ECHO Failed : Map Horizon Import URL >>"%LOGFILE%"
                  ECHO      %HORIZON_URL_IMP% >>"%LOGFILE%"
                  ECHO.>>"%LOGFILE%"
                  SET "NF1=1"& GOTO AbnormalExit
               )
            )
         )
   )
)

:BREAK
CLS

>>"%LOGFILE%" (
ECHO ********************************************************
ECHO Transfer files from:
ECHO    SharePoint Horizon 'Ascend_Headcount_Staging' directory
ECHO.
ECHO Transfer file to:
ECHO    Finance ^& Accounting \Horizon_Import directory                  
ECHO ********************************************************
ECHO.
ECHO SharePoint Horizon 'Ascend_Headcount_Staging' directory :
ECHO   %HORIZON_URL_STAG%
ECHO.
ECHO Finance ^& Accounting \Horizon_Import directory :
ECHO   "%HC_EXCEL_SUBPATH%%HC_ACT_SUBF%"                                    
)

::-- Create folder in YYYY_MMDD format to place Ascend FTE Submission Files --::
::-- This folder is where the '.xlsx' to '.csv' conversion happens --::

IF NOT EXIST "%HC_EXCEL_SUBPATH%%HC_ACT_SUBF%%date:~-4,4%_%date:~-10,2%%date:~-7,2%" (
   MKDIR "%HC_EXCEL_SUBPATH%%HC_ACT_SUBF%%date:~-4,4%_%date:~-10,2%%date:~-7,2%"
)

PUSHD "%MD%:\"
FOR %%F IN (*) DO SET /A CNT1+=0
FOR %%F IN (*) DO (

   ECHO F | XCOPY /F /Y "%MD%:\%%~F" "%HC_EXCEL_SUBPATH%%HC_ACT_SUBF%%date:~-4,4%_%date:~-10,2%%date:~-7,2%" >nul 2>&1
   DEL /F /Q "%MD%:\%%~F"
      
      ECHO.>>"%LOGFILE%"
      ECHO Transfered          : %%F >>"%LOGFILE%"
      
   CALL :CONVERTXLSX %%F
   PING -n 2 127.0.0.1>nul
)
POPD
CLS

>>"%LOGFILE%" (
ECHO.
ECHO ********************************************************
ECHO Transfer files from:
ECHO    Finance ^& Accounting \Horizon_Import directory   
ECHO.
ECHO Transfer file to:
ECHO    SharePoint Horizon 'Ascend_Headcount' directory                  
ECHO ********************************************************
ECHO.
ECHO Finance ^& Accounting \Horizon_Import directory :
ECHO   "%HC_EXCEL_SUBPATH%%HC_ACT_SUBF%"
ECHO.
ECHO SharePoint Horizon 'Ascend_Headcount' directory :
ECHO   %HORIZON_URL_IMP%                                 
)

PUSHD "%HC_EXCEL_SUBPATH%%HC_ACT_SUBF%%date:~-4,4%_%date:~-10,2%%date:~-7,2%"
FOR %%F IN (*) DO (

    ECHO F | XCOPY /F /Y "%%~F" "%MD1%:\%%~F" >nul 2>&1
   ECHO Transfered  : %%F >>"%LOGFILE%"
PING -n 2 127.0.0.1>nul
)
POPD

PUSHD "%MD1%:\"
FOR %%F IN (*) DO SET /A CNT2+=1
POPD

::-- Unmap Horizon Network Shares --::
NET USE %MD%: /delete /y
NET USE %MD1%: /delete /y

IF "%CNT1%"=="0" IF "%CNT2%"=="1" GOTO JUMP
IF "%CNT1%" NEQ "%CNT2%" (
   :JUMP
   >>"%LOGFILE%" (
   ECHO.
   ECHO WARNING^^! %CNT2% out of %CNT1% files were uploaded to Sharepoint Horizon 'Ascend_Headcount'
   ECHO.
   ECHO WARNING^^! Please contact support to fix the issue
   ECHO.
   )
   
   SET "NF3=T" & GOTO AbnormalExit   
)

>>"%LOGFILE%" (
   ECHO.
   ECHO Success^^! %CNT1% out of %CNT2% files uploaded - No further action necessary^^!
   ECHO.
   ECHO Ascend FTE files imported to TM1 every hour [XX:00]
)

:NormalExit
CLS
>>"%LOGFILE%" (
ECHO.
ECHO ********************************************************
ECHO %~n0 - Completed Successfully                           
ECHO ********************************************************

ECHO ********************************************************
ECHO Normal Exit - %~nx0                                     
ECHO ********************************************************
DATE /t                                                     
TIME /t                                                     
)

CALL :ARCHIVE
CALL :DISPLAY
EXIT 0

:AbnormalExit
>>"%LOGFILE%" (
IF DEFINED NF1 (
   ECHO ********************************************************
   ECHO          ^^! ATTENTION ^^!
   ECHO ********************************************************
   ECHO.
   ECHO A Horizon URL failed to map to the Windows Explorer
   ECHO.
   ECHO Please see above for the URL^(s^) that failed to map
   ECHO.
   ECHO Potentials reasons of failure:
   ECHO.
   ECHO [1] User not connected to Takeda Network
   ECHO [2] User does not have sufficient privileges
   ECHO [3] Folder name changed in document library
   ECHO [4] URL is incorrect
   ECHO.
   ECHO Please contact R^&D Finance technical lead for support
)
IF DEFINED NF2 (
   ECHO ********************************************************
   ECHO          ^^! ATTENTION ^^!
   ECHO ********************************************************
   ECHO.
   ECHO No FTE files detected in:
   ECHO   SharePoint Horizon 'Ascend_Headcount_Staging' directory
   ECHO.
   ECHO Potential causes:
   ECHO.
   ECHO [1] File^(s^) were already processed to TM1
   ECHO [2] File^(s^) were removed
   ECHO [3] File^(s^) are in use and cannot be copied
   ECHO.
   ECHO Please contact R^&D Finance technical lead for support
)
IF DEFINED NF3 (
   ECHO ********************************************************
   ECHO          ^^! ATTENTION ^^!
   ECHO ********************************************************
   ECHO.
   ECHO Discrpancy in file count between source and target
   ECHO.
   ECHO Potential causes:
   ECHO.
   ECHO [1] File^(s^) unable to transfer ^[in use^]
   ECHO [2] Extra file^(s^) in 'Ascend_Headcount_Staging' directory
   ECHO [3] Extra file^(s^) copied to 'Ascend_Headcount' directory
   ECHO [4] Transfer process interuppted
   ECHO.
   ECHO Please contact R^&D Finance technical lead for support
)
ECHO.
ECHO ********************************************************
ECHO %~n0 results in Abnormal Exit                         
ECHO ********************************************************           

ECHO ********************************************************
ECHO Abnormal Exit - %~nx0                                   
ECHO ********************************************************
DATE /t                                                     
TIME /t                                                     
)

CALL :ARCHIVE
CALL :DISPLAY
EXIT 1

::::::::::::::::::::::::::::::::::::::::::::::::::
::-- F U N C T I O N S   B E L O W   H E R E  --::
::::::::::::::::::::::::::::::::::::::::::::::::::

:CHECK_DAY

color CF
FOR %%D IN ( 01 02 03 04 05 ) DO (

   IF %date:~-7,2%==%%D (
      ECHO ********************************************************
      ECHO                    ^! ATTENTION ^!
      ECHO ********************************************************
      ECHO.
      ECHO It is currently Business Day %%D
      ECHO.
      ECHO This process should be executed at EOD Business Day 6
      ECHO.
      ECHO Only run if explicitly approved by R^&D Finance
      ECHO
      ECHO ********************************************************
      ECHO.
      ECHO [1] Proceed
      ECHO [2] Exit
      ECHO.
      CHOICE /N /C 12 /M "Option: "
   
      IF ERRORLEVEL 2 EXIT
   )
)
color 0f
GOTO :EOF

:CHECK_DRIVE
CLS

SET "DL="
FOR /F "tokens=1 delims=" %%D IN ( 'net use ^|FINDSTR "\\departments\departments"' ) DO (
   FOR /F "tokens=2" %%E IN ('ECHO %%D ^|FINDSTR "OK"') DO (
   SET "DL=%%E"
   )
)

IF [%DL%] == [] (
   ECHO ********************************************************
   ECHO                   ^! ATTENTION ^!
   ECHO ********************************************************
   ECHO Unable to determine drive letter for \\departments share
   ECHO.
   ECHO Please ensure %COMPUTERNAME% is connected to the Network
   ECHO ********************************************************
   ECHO.
   ECHO [1] Retest
   ECHO [2] Exit - Contact Support
   ECHO.
   CHOICE /N /C 12 /M "Option: "
   IF ERRORLEVEL 2 EXIT
   CLS
   PING -n 10 127.0.0.1>nul
   GOTO CHECK_DRIVE
)

GOTO :EOF

:CHCK_MD

PUSHD "%MD%:\"
FOR %%F IN (*) DO SET /A CNT1+=1

IF "%CNT1%"=="0" (

   NET USE %MD%: /delete /y >nul 2>&1
   SET "NF2=T" & GOTO AbnormalExit
)

GOTO :EOF

:CONVERTXLSX

SET "HCT_FN=%1"

CSCRIPT //NOLOGO "%BATCHPATH%\XLSX_2_CSV.vbs" "%HC_EXCEL_SUBPATH%%HC_ACT_SUBF%%date:~-4,4%_%date:~-10,2%%date:~-7,2%\!HCT_FN:~0,-5!.%EXT%"^
   "%HC_EXCEL_SUBPATH%%HC_ACT_SUBF%%date:~-4,4%_%date:~-10,2%%date:~-7,2%\!HCT_FN:~0,-5!.csv"

IF EXIST "%HC_EXCEL_SUBPATH%%HC_ACT_SUBF%%date:~-4,4%_%date:~-10,2%%date:~-7,2%\*.%EXT%" (
   DEL /F /Q "%HC_EXCEL_SUBPATH%%HC_ACT_SUBF%%date:~-4,4%_%date:~-10,2%%date:~-7,2%\*.%EXT%"
)

ECHO Converted to '.csv' : !HCT_FN:~0,-5!.csv >>"%LOGFILE%"

GOTO :EOF

:ARCHIVE

::-- Archive Log Files --::

FOR %%F IN ("%INTRAPATH%") DO (
   IF NOT EXIST "%INTRAPATH%%date:~-4,4%_%date:~-10,2%%date:~-7,2%" (
      MKDIR "%INTRAPATH%%date:~-4,4%_%date:~-10,2%%date:~-7,2%"
   )   
MOVE /Y %%F* "%INTRAPATH%%date:~-4,4%_%date:~-10,2%%date:~-7,2%">nul 2>&1
)

::-- Archive Error Files --::
 
FOR %%F IN ("%ERRORINTRAPATH%") DO (
   IF NOT EXIST "%ERRORINTRAPATH%%date:~-4,4%_%date:~-10,2%%date:~-7,2%" (
      MKDIR "%ERRORINTRAPATH%%date:~-4,4%_%date:~-10,2%%date:~-7,2%"
   )   
MOVE /Y %%F* "%ERRORINTRAPATH%%date:~-4,4%_%date:~-10,2%%date:~-7,2%">nul 2>&1
)

GOTO :EOF

:DISPLAY
CLS
IF EXIST "%INTRAPATH%%date:~-4,4%_%date:~-10,2%%date:~-7,2%\%date:~-4,4%%date:~-10,2%%date:~-7,2%_%timestamp%_%~n0.log" (
   TYPE "%INTRAPATH%%date:~-4,4%_%date:~-10,2%%date:~-7,2%\%date:~-4,4%%date:~-10,2%%date:~-7,2%_%timestamp%_%~n0.log"   
)
ECHO.
ECHO [Press Enter to Exit]
set/p=

SIMMS7400
Posts: 546
Joined: 07 Jan 2016 07:47

Re: Issues with unmapping a drive letter

#2 Post by SIMMS7400 » 12 Aug 2017 18:20

Figured it out but I don't know why I had to do it this way.

I needed to breakup the delete drive statements. Essentially, as soon as each drive isn't needed, I disconnect it, rather than trying to disconnect both at once.

Code: Select all

>>"%LOGFILE%" (
ECHO ********************************************************
ECHO Map Horizon URL's as drives for transfer process...              
ECHO ********************************************************
ECHO.                                          
)

ECHO Mapping Horizon URL's as drives for transfer process...
PING -n 4 127.0.0.1>nul

SETLOCAL ENABLEDELAYEDEXPANSION

FOR %%D IN ( b d e f g h i j k l m n o p q r s t u v w x y z ) DO (

   IF NOT EXIST %%D:\ (
      SET "MD=%%D"
      NET USE !MD!: "%HORIZON_URL_STAG%" /USER:%CREDS% >nul 2>&1 && (
         ECHO Success : Map Horizon Stage URL >>"%LOGFILE%"
         ECHO      %HORIZON_URL_STAG% >>"%LOGFILE%"
         ECHO.>>"%LOGFILE%"
          CALL :CHCK_MD
      ) || (
         ECHO Failed : Map Horizon Stage URL >>"%LOGFILE%"
         ECHO      %HORIZON_URL_STAG% >>"%LOGFILE%"
         ECHO.>>"%LOGFILE%"
         SET "NF1=1"& GOTO AbnormalExit
      )

         FOR %%E IN ( b d e f g h i j k l m n o p q r s t u v w x y z ) DO (
            
            IF NOT EXIST %%E:\ (
               SET "MD1=%%E"
               NET USE !MD1!: "%HORIZON_URL_IMP%" /USER:%CREDS% >nul 2>&1 && (
                  ECHO Success : Map Horizon Import URL >>"%LOGFILE%"
                  ECHO      %HORIZON_URL_IMP% >>"%LOGFILE%"
                  ECHO.>>"%LOGFILE%"
                  GOTO BREAK
               ) || (
                  ECHO Failed : Map Horizon Import URL >>"%LOGFILE%"
                  ECHO      %HORIZON_URL_IMP% >>"%LOGFILE%"
                  ECHO.>>"%LOGFILE%"
                  SET "NF1=1"& GOTO AbnormalExit
               )
            )
         )
   )
)

:BREAK
CLS

>>"%LOGFILE%" (
ECHO ********************************************************
ECHO Transfer files from:
ECHO    SharePoint Horizon 'Ascend_Headcount_Staging' directory
ECHO.
ECHO Transfer file to:
ECHO    Finance ^& Accounting \Horizon_Import directory                  
ECHO ********************************************************
ECHO.
ECHO SharePoint Horizon 'Ascend_Headcount_Staging' directory :
ECHO   %HORIZON_URL_STAG%
ECHO.
ECHO Finance ^& Accounting \Horizon_Import directory :
ECHO   "%HC_EXCEL_SUBPATH%%HC_ACT_SUBF%"                                    
)

ECHO Transfering files from:
ECHO    SharePoint Horizon 'Ascend_Headcount_Staging' directory
ECHO.
ECHO Transfering files to:
ECHO    Finance ^& Accounting \Horizon_Import directory
PING -n 4 127.0.0.1>nul
CLS

::-- Create folder in YYYY_MMDD format to place Ascend FTE Submission Files --::
::-- This folder is where the '.xlsx' to '.csv' conversion happens --::

IF NOT EXIST "%HC_EXCEL_SUBPATH%%HC_ACT_SUBF%%date:~-4,4%_%date:~-10,2%%date:~-7,2%" (
   MKDIR "%HC_EXCEL_SUBPATH%%HC_ACT_SUBF%%date:~-4,4%_%date:~-10,2%%date:~-7,2%"
)

PUSHD "%MD%:\"
FOR %%F IN (*) DO SET /A CNT1+=0
FOR %%F IN (*) DO (

   ECHO F | XCOPY /F /Y "%MD%:\%%~F" "%HC_EXCEL_SUBPATH%%HC_ACT_SUBF%%date:~-4,4%_%date:~-10,2%%date:~-7,2%" >nul 2>&1
    DEL /F /Q "%MD%:\%%~F"
      
      ECHO.>>"%LOGFILE%"
      ECHO Transfered          : %%F >>"%LOGFILE%"
      
   CALL :CONVERTXLSX %%F
   PING -n 2 127.0.0.1>nul
)
POPD
CLS

::-- Unmap Horizon Network Share --::
NET USE "%MD%:" /delete /y >nul 2>&1

>>"%LOGFILE%" (
ECHO.
ECHO ********************************************************
ECHO Transfer files from:
ECHO    Finance ^& Accounting \Horizon_Import directory   
ECHO.
ECHO Transfer file to:
ECHO    SharePoint Horizon 'Ascend_Headcount' directory                  
ECHO ********************************************************
ECHO.
ECHO Finance ^& Accounting \Horizon_Import directory :
ECHO   "%HC_EXCEL_SUBPATH%%HC_ACT_SUBF%"
ECHO.
ECHO SharePoint Horizon 'Ascend_Headcount' directory :
ECHO   %HORIZON_URL_IMP%                                 
)

ECHO Transfering files from:
ECHO    SharePoint Horizon 'Ascend_Headcount_Staging' directory
ECHO.
ECHO Transfering files to:
ECHO    Finance ^& Accounting \Horizon_Import directory   
PING -n 4 127.0.0.1>nul
CLS

PUSHD "%HC_EXCEL_SUBPATH%%HC_ACT_SUBF%%date:~-4,4%_%date:~-10,2%%date:~-7,2%"
FOR %%F IN (*) DO (

    ECHO F | XCOPY /F /Y "%%~F" "%MD1%:\%%~F" >nul 2>&1
   ECHO Transfered  : %%F >>"%LOGFILE%"
PING -n 2 127.0.0.1>nul
)
POPD

PUSHD "%MD1%:\"
FOR %%F IN (*) DO SET /A CNT2+=1
POPD

::-- Unmap Horizon Network Share --::
NET USE "%MD1%:" /delete /y >nul 2>&1

IF "%CNT1%"=="0" IF "%CNT2%"=="1" GOTO JUMP
IF "%CNT1%" NEQ "%CNT2%" (
   :JUMP
   >>"%LOGFILE%" (
   ECHO.
   ECHO WARNING^^! %CNT2% out of %CNT1% files were uploaded to Sharepoint Horizon 'Ascend_Headcount'
   ECHO.
   ECHO WARNING^^! Please contact support to fix the issue
   ECHO.
   )
   
   SET "NF3=T" & GOTO AbnormalExit   
)

>>"%LOGFILE%" (
   ECHO.
   ECHO Success^^! %CNT1% out of %CNT2% files uploaded - No further action necessary^^!
   ECHO.
   ECHO Ascend FTE files imported to TM1 every hour [XX:00]
)

:NormalExit

Post Reply