Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: net use /delete in script - do not work  (Read 18070 times)

0 Members and 1 Guest are viewing this topic.

silberzin

    Topic Starter


    Rookie

    net use /delete in script - do not work
    « on: July 19, 2010, 01:44:07 AM »
    Hi All

    can anybody help.

    I have script:
    Code: [Select]
    echo Y|net use p: /delete

    START /wait NET USE p: \\server1\MDPRD\distributions\Inbound\70\70_Customer_flat\Archive
    call e:\batchfile.bat
    echo Y|net use p: /delete

    echo Y|net use p: /delete
    START /wait NET USE p: \\server1\MDPRD\distributions\P31\BW__RACUGRP\Archive
    call e:\batchfile.bat
    echo Y|net use p: /delete

    START /wait NET USE p: \\server1\MDPRD\distributions\Archive
    call e:\batchfile.bat
    echo Y|net use p: /delete

    however after calling e:\batchfile.bat
    echo Y|net use p: /delete
    do not work

    can anobydy advice how to solve that error?

    Code: [Select]
    E:\>echo Y  | net use y: /delete
    y: was deleted successfully.

    E:\>START /wait NET USE y: \\server1\MDPRD\distributions\Inbound\70\70_Customer_flat\Archive


    E:\>call e:\batchfile.bat
    The device is being accessed by an active process. <---- here net use delete  is failing

    More help is available by typing NET HELPMSG 2404.

    Press any key to continue . .

    Please advice

    Thank you very much.

    BR
    Michal

    Sidewinder



      Guru

      Thanked: 139
    • Experience: Familiar
    • OS: Windows 10
    Re: net use /delete in script - do not work
    « Reply #1 on: July 19, 2010, 06:19:46 AM »
    Quote
    however after calling e:\batchfile.bat
    echo Y|net use p: /delete
    do not work

    It would help if you posted the contents of batchfile.bat. It appears some process has an open handle to the P drive.

    Any reason why you are using start with the net use command? There is no reason to run it in a separate window and running it inline produces the same result as the /wait switch would.

     8)
    The true sign of intelligence is not knowledge but imagination.

    -- Albert Einstein

    silberzin

      Topic Starter


      Rookie

      Re: net use /delete in script - do not work
      « Reply #2 on: July 20, 2010, 12:00:21 AM »
      hi Sidewinder

      Thank you for replay, here is full batchfile.bat:


      Code: [Select]
      @echo off
      CD /d p:\

      REM grab todays date into variables
      set MM=%Date:~3,2%
      set DD=%Date:~0,2%
      set YYYY=%Date:~6,4%

      REM Add the date variables to the log name
      REM putting the date in this order will sort the files sensibly
      Set LogFile="e:\NewFiles_%YYYY%-%MM%-%DD%.Log"

      REM create the vb script
      >c:\evaluate.vbs echo Wscript.echo eval(WScript.Arguments(0))

      REM extract todays date less 7 days
      set OldestDate=
      for /f "delims=" %%A in ( ' cscript //nologo c:\evaluate.vbs "Date -7" ' ) do set OldestDate=%%A

      REM tidy
      del c:\evaluate.vbs

      REM break up the date into constituent parts
      REM Note - the date is parsing YYYY-MM-DD format dates,

      set MM=
      set DD=
      set YYYY=

      set MM=%OldestDate:~5,2%
      set DD=%OldestDate:~8,2%
      set YYYY=%OldestDate:~0,4%

      REM reconstruct US format with '-' separators
      set OldestDate=%MM%-%DD%-%YYYY%

      REM add the new entry to the log
      >> %LogFile% Echo %DATE% %TIME%
      >> %LogFile% Echo Since : %OldestDate%
      net use p:|find "Remote name">> %LogFile%

      REM list the files that would be copied if we really were doing a copy
      REM force it to believe that the target NonExistentDirectory is a directory
      REM and echo the name & time for each

      for /f "delims=" %%A in ( ' xcopy /l /i /d:%OldestDate% *.*  NonExistentDirectory ' ) do call :ProcessEach "%%A"

      REM tidy up the log
      >> %LogFile% Echo ========================================

      goto :EOF

      :ProcessEach

      >> %LogFile% echo %~f1                       %~t1

      goto :EOF



      Thank you for help :)

      BR
      Michal

      Sidewinder



        Guru

        Thanked: 139
      • Experience: Familiar
      • OS: Windows 10
      Re: net use /delete in script - do not work
      « Reply #3 on: July 20, 2010, 05:03:22 AM »
      The second line in batchfile.bat logs you on to the P drive. There is no other CD instruction in the file (that I could find), so when batchfile.bat ends, you are still logged into the P drive.

      Batchfile.bat needs to be changed to log off the P drive before the calling file attempts to delete the mapping (net use p: /delete)

       8)
      The true sign of intelligence is not knowledge but imagination.

      -- Albert Einstein

      gpl



        Apprentice
      • Thanked: 27
        Re: net use /delete in script - do not work
        « Reply #4 on: July 20, 2010, 06:17:37 AM »
        Michal
        I knew it would be something easy !
        Sorry I couldnt finish this off without Sidewinder's help (cheers SW)
        Graham

        Try this
        Code: [Select]
        @echo off
        CD /d p:\

        REM grab todays date into variables
        set MM=%Date:~3,2%
        set DD=%Date:~0,2%
        set YYYY=%Date:~6,4%

        REM Add the date variables to the log name
        REM putting the date in this order will sort the files sensibly
        Set LogFile="e:\NewFiles_%YYYY%-%MM%-%DD%.Log"

        REM create the vb script
        >c:\evaluate.vbs echo Wscript.echo eval(WScript.Arguments(0))

        REM extract todays date less 7 days
        set OldestDate=
        for /f "delims=" %%A in ( ' cscript //nologo c:\evaluate.vbs "Date -7" ' ) do set OldestDate=%%A

        REM tidy
        del c:\evaluate.vbs

        REM break up the date into constituent parts
        REM Note - the date is parsing YYYY-MM-DD format dates,

        set MM=
        set DD=
        set YYYY=

        set MM=%OldestDate:~5,2%
        set DD=%OldestDate:~8,2%
        set YYYY=%OldestDate:~0,4%

        REM reconstruct US format with '-' separators
        set OldestDate=%MM%-%DD%-%YYYY%

        REM add the new entry to the log
        >> %LogFile% Echo %DATE% %TIME%
        >> %LogFile% Echo Since : %OldestDate%
        net use p:|find "Remote name">> %LogFile%

        REM list the files that would be copied if we really were doing a copy
        REM force it to believe that the target NonExistentDirectory is a directory
        REM and echo the name & time for each

        for /f "delims=" %%A in ( ' xcopy /l /i /d:%OldestDate% *.*  NonExistentDirectory ' ) do call :ProcessEach "%%A"

        REM tidy up the log
        >> %LogFile% Echo ========================================

        REM ----------------------------
        REM Release mapped drive
        REM ----------------------------

        E:

        goto :EOF

        :ProcessEach

        >> %LogFile% echo %~f1                       %~t1

        goto :EOF

        silberzin

          Topic Starter


          Rookie

          Re: net use /delete in script - do not work
          « Reply #5 on: July 22, 2010, 06:00:43 AM »
          Hi :)

          Now it looks to work with deleting p: drive when script is finished

          It seems to be working as expected :)

          Thank you very very very much :)

          BR
          Michal

          silberzin

            Topic Starter


            Rookie

            Re: net use /delete in script - do not work
            « Reply #6 on: August 16, 2010, 05:01:26 AM »
            Hi

            I encouriage strange problem. Mentioned above script works fine on one computer, but on all others is failing.

            I have that error message:

            C:\MDM_Monitoring>echo Y  | net use p: /delete
            p: was deleted successfully.


            C:\MDM_Monitoring>START /wait NET USE p: \\server1\P_Customer\Inbound\70\70_Customer_flat\Archive

            C:\MDM_Monitoring>call e:\bathfile.bat
            The system cannot find the drive specified.
            The device is being accessed by an active process.

            More help is available by typing NET HELPMSG 2404.

            Press any key to continue . . .


            Can you please help, I have no clue why that error occour. Special that it works on once computer only. On all other computers I have mentioned above error.

            Thank you in advance.

            BR
            Michal

            Sidewinder



              Guru

              Thanked: 139
            • Experience: Familiar
            • OS: Windows 10
            Re: net use /delete in script - do not work
            « Reply #7 on: August 16, 2010, 02:02:35 PM »
            Quote
            REM ----------------------------
            REM Release mapped drive
            REM ----------------------------

            E:

            I see remarks for deleting the mapped drive but no actual instructions. It may be time to rethink your design. Batchfile should contain all the instructions it needs to run whether from the command line or as a called file. By this I mean batchfile should map the drive, access the drive and release the drive; it should not rely on some caller batch file to supply these instructions.

            Be aware that in a caller/calling sequence, that if any module fails to complete properly, your session status may be corrupt.

            Why are you using start /wait with the net use command? Why are you piping Y to the net use command?

            Quote
            Special that it works on once computer only. On all other computers I have mentioned above error.

            Let us know.  8)

            Are all the computers running the same OS. Are they on the same network?

            The true sign of intelligence is not knowledge but imagination.

            -- Albert Einstein

            silberzin

              Topic Starter


              Rookie

              Re: net use /delete in script - do not work
              « Reply #8 on: August 17, 2010, 12:16:00 AM »
              Hi All

              Sorry I messed up, all works fine.

              note. Always connect to disks mapped to your computer :)

              BR
              Silberzin