Deleting some cache folder on a remote server

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
naraen87
Posts: 17
Joined: 21 Dec 2017 06:41

Deleting some cache folder on a remote server

#1 Post by naraen87 » 29 Jan 2018 09:42

Below is my script for deleting the cache folder on remote server

Code: Select all

REM *********************Delete the Cache and Log Folders************************************************************************
set "servername=10.47.36.90"
set "psexec=E:\Jenkins_Software\PSTools\PsExec.exe"
set "Adminlogpathtofolder=\\%servername%\C$\narayana\Enviromnet_Setup\WIServerSetup\UAT\EDrive\Oracle\Middleware\user_projects\domains\Skandia\servers\AdminServer\logs\"

%psexec% \\%servername% pushd "%Adminlogpathtofolder%" && (rd /s /q "%Adminlogpathtofolder%" 2>nul & popd)
if %errorlevel%==0 goto cachedeletesuccess
if NOT %errorlevel%==0 goto cachedeleteunsuccess
:cachedeletesuccess
set "cache_delete_status=success"
echo "Cache Deletion Successfully"
goto exit
:cachedeleteunsuccess
set "cache_delete_status=failure"
echo "Unable to delete the cache"
goto :EOF

:exit

Below is the error I got when I run my above batch script

Code: Select all

C:\Users\SVCT-WI-Jenkins\Desktop>REM *********************Delete the Cache and Log Folders************************************************************
************

C:\Users\SVCT-WI-Jenkins\Desktop>set "servername=10.47.36.90"

C:\Users\SVCT-WI-Jenkins\Desktop>set "psexec=E:\Jenkins_Software\PSTools\PsExec.exe"

C:\Users\SVCT-WI-Jenkins\Desktop>set "Adminlogpathtofolder=\\10.47.36.90\C$\narayana\Enviromnet_Setup\WIServerSetup\UAT\EDrive\Oracle\Middleware\user_
projects\domains\Skandia\servers\AdminServer\logs\"

C:\Users\SVCT-WI-Jenkins\Desktop>E:\Jenkins_Software\PSTools\PsExec.exe \\10.47.36.90 pushd "\\10.47.36.90\C$\narayana\Enviromnet_Setup\WIServerSetup\
UAT\EDrive\Oracle\Middleware\user_projects\domains\Skandia\servers\AdminServer\logs\"   && (rd /s /q "\\10.47.36.90\C$\narayana\Enviromnet_Setup\WISer
verSetup\UAT\EDrive\Oracle\Middleware\user_projects\domains\Skandia\servers\AdminServer\logs\"   2>nul  & popd)

PsExec v2.2 - Execute processes remotely
Copyright (C) 2001-2016 Mark Russinovich
Sysinternals - www.sysinternals.com


PsExec could not start pushd on 10.47.36.90:
The system cannot find the file specified.

C:\Users\SVCT-WI-Jenkins\Desktop>if 2 == 0 goto cachedeletesuccess

C:\Users\SVCT-WI-Jenkins\Desktop>if NOT 2 == 0 goto cachedeleteunsuccess

C:\Users\SVCT-WI-Jenkins\Desktop>set "cache_delete_status=failure"

C:\Users\SVCT-WI-Jenkins\Desktop>echo "Unable to delete the cache"
"Unable to delete the cache"

C:\Users\SVCT-WI-Jenkins\Desktop>goto :EOF
If I'm not using the PsExec its taking nearly 30 to 40 mins of time for deleting the folder.

Can anyone help me on this... :cry:

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Deleting some cache folder on a remote server

#2 Post by aGerman » 29 Jan 2018 10:38

I never used PsExec but I'm wondering why you try to use the network path once you are connected to the server.
Just try

Code: Select all

%psexec% \\%servername% rd /s /q C:\narayana\Enviromnet_Setup\WIServerSetup\UAT\EDrive\Oracle\Middleware\user_projects\domains\Skandia\servers\AdminServer\logs
Steffen

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: Deleting some cache folder on a remote server

#3 Post by Squashman » 29 Jan 2018 11:00

PUSHD and RD are all internal shell commands that you are trying to run on the remote server. I would assume you would need to launch cmd.exe first.

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Deleting some cache folder on a remote server

#4 Post by aGerman » 29 Jan 2018 11:16

Good catch Squashman!

Code: Select all

%psexec% \\%servername% cmd /c "rd /s /q C:\narayana\Enviromnet_Setup\WIServerSetup\UAT\EDrive\Oracle\Middleware\user_projects\domains\Skandia\servers\AdminServer\logs"

naraen87
Posts: 17
Joined: 21 Dec 2017 06:41

Re: Deleting some cache folder on a remote server

#5 Post by naraen87 » 30 Jan 2018 05:55

Thank you very much for your timely help @aGerman @Squashman

Could you please help me to solve this too I tried it but it doen't work

Code: Select all

REM *********************Delete the Cache and Log Folders************************************************************************
set "servername=10.47.36.90"
set "psexec=E:\Jenkins_Software\PSTools\PsExec.exe"
set "Adminlogpathtofolder=\\%servername%\C$\narayana\Enviromnet_Setup\WIServerSetup\UAT\EDrive\Oracle\Middleware\user_projects\domains\Skandia\servers\AdminServer\logs\"
%psexec% \\%servername% pushd "%Adminpathtofolder%"
for /f "delims=" %%i in ('dir /ad /b^|findstr /vlix "stage security logs"') do rd /s /q "%%i"
popd

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Deleting some cache folder on a remote server

#6 Post by aGerman » 30 Jan 2018 14:16

Again I can only guess

Code: Select all

set "servername=10.47.36.90"
set "psexec=E:\Jenkins_Software\PSTools\PsExec.exe"
set "Adminlogpathtofolder=C:\narayana\Enviromnet_Setup\WIServerSetup\UAT\EDrive\Oracle\Middleware\user_projects\domains\Skandia\servers\AdminServer\logs"
%psexec% \\%servername% cmd /c "cd /d %Adminlogpathtofolder% &rd /s /q stage &rd /s /q security &rd /s /q logs"
Steffen

naraen87
Posts: 17
Joined: 21 Dec 2017 06:41

Re: Deleting some cache folder on a remote server

#7 Post by naraen87 » 31 Jan 2018 04:06

Sorry @aGerman

I guess I wrongly explained my requirement in the above one I don't want to delete the following folders logs,security and stage
except that I want to delete all the other folders.
But the solution you provide is to delete the logs, security and stage.

Sorry for the trouble.
Could you please provide me a solution for delete all the other things expect the above said three folders.
Currently I'm running the following batch file for deleting the folders and files expect that 3 folders.

Code: Select all

pushd "%Adminpathtofolder%"
for /f "delims=" %%i in ('dir /ad /b^|findstr /vlix "stage security logs"') do rd /s /q "%%i"
popd
if %errorlevel%==0 goto cachedeletesuccess
if NOT %errorlevel%==0 goto cachedeleteunsuccess
:cachedeletesuccess
set "cache_delete_status=success"
echo "Cache Deletion Successfully"
goto exit
:cachedeleteunsuccess
set "cache_delete_status=failure"
echo "Unable to delete the cache"
goto :EOF

:exit
Time taken for deleting these is more than 30mins because I'm running this batch file in my build server not in the server which I used , that is why I have taken psexec for running the commands in remote server.

I think I explained clearly if not please correct me.

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: Deleting some cache folder on a remote server

#8 Post by Squashman » 31 Jan 2018 08:13

Would be a lot easier to create a batch file with the commands you want to run on the remote server and then execute that batch file on the remote server.

naraen87
Posts: 17
Joined: 21 Dec 2017 06:41

Re: Deleting some cache folder on a remote server

#9 Post by naraen87 » 31 Jan 2018 09:08

Hi,

While I running the below ORF_delete.bat

Code: Select all

REM *********************Delete the Cache and Log Folders************************************************************************
set "servername=10.66.216.141"
set "psexec=E:\Jenkins_Software\PSTools\PsExec.exe"
set "Adminlogpathtofolder=\\vfs-or2-wi\bancs\BaNCSFS\ORF2Test\New folder\"

if exist "%Adminlogpathtofolder%" goto deleteadminlog
if not exist "%Adminlogpathtofolder%" goto noadminlog

:deleteadminlog
REM %psexec% \\%servername% pushd "%Adminlogpathtofolder%" && (rd /s /q "%Adminlogpathtofolder%" 2>nul & popd)
REM %psexec% \\%servername% cmd /c "rd /s /q %Adminlogpathtofolder%"
REM pushd "%Adminlogpathtofolder%" && (rd /s /q "%Adminlogpathtofolder%" 2>nul & popd)
[b]pushd "%Adminlogpathtofolder%"
rd /s /q "%Adminlogpathtofolder%"[/b]
echo %errorlevel%
popd
if %errorlevel%==0 goto cachedeletesuccess
if NOT %errorlevel%==0 goto cachedeleteunsuccess
:cachedeletesuccess
set "cache_delete_status=success"
echo "Admin Cache Deletion Successfully"
goto exit
:cachedeleteunsuccess
set "cache_delete_status=failure"
echo "Unable to delete the cache"
goto :EOF

:noadminlog
echo "There is no log folder on the Admin Server"
goto exit
:exit
The output I got

Code: Select all

C:\Users\SVCT-WI-Jenkins\Desktop>ORF_delete.bat

C:\Users\SVCT-WI-Jenkins\Desktop>REM *********************Delete the Cache and Log Folders************************************************************
************

C:\Users\SVCT-WI-Jenkins\Desktop>set "servername=10.66.216.141"

C:\Users\SVCT-WI-Jenkins\Desktop>set "psexec=E:\Jenkins_Software\PSTools\PsExec.exe"

C:\Users\SVCT-WI-Jenkins\Desktop>set "Adminlogpathtofolder=\\vfs-or2-wi\bancs\BaNCSFS\ORF2Test\New folder\"

C:\Users\SVCT-WI-Jenkins\Desktop>if exist "\\vfs-or2-wi\bancs\BaNCSFS\ORF2Test\New folder\" goto deleteadminlog

C:\Users\SVCT-WI-Jenkins\Desktop>REM E:\Jenkins_Software\PSTools\PsExec.exe \\10.66.216.141 pushd "\\vfs-or2-wi\bancs\BaNCSFS\ORF2Test\New folder\" &&
 (rd /s /q "\\vfs-or2-wi\bancs\BaNCSFS\ORF2Test\New folder\" 2>nul & popd)

C:\Users\SVCT-WI-Jenkins\Desktop>REM E:\Jenkins_Software\PSTools\PsExec.exe \\10.66.216.141 cmd /c "rd /s /q \\vfs-or2-wi\bancs\BaNCSFS\ORF2Test\New f
older\"

C:\Users\SVCT-WI-Jenkins\Desktop>REM pushd "\\vfs-or2-wi\bancs\BaNCSFS\ORF2Test\New folder\" && (rd /s /q "\\vfs-or2-wi\bancs\BaNCSFS\ORF2Test\New fol
der\" 2>nul & popd)

C:\Users\SVCT-WI-Jenkins\Desktop>pushd "\\vfs-or2-wi\bancs\BaNCSFS\ORF2Test\New folder\"

[u][b]Z:\BaNCSFS\ORF2Test\New folder>[b]rd /s /q "\\vfs-or2-wi\bancs\BaNCSFS\ORF2Test\New folder\"
\\vfs-or2-wi\bancs\BaNCSFS\ORF2Test\New folder\New Text Document.txt - Access is denied.
The process cannot access the file because it is being used by another process.[/b][/b][/u]

Z:\BaNCSFS\ORF2Test\New folder>echo 0
0

Z:\BaNCSFS\ORF2Test\New folder>popd

C:\Users\SVCT-WI-Jenkins\Desktop>if 0 == 0 goto cachedeletesuccess

C:\Users\SVCT-WI-Jenkins\Desktop>set "cache_delete_status=success"

C:\Users\SVCT-WI-Jenkins\Desktop>echo "Admin Cache Deletion Successfully"
"Admin Cache Deletion Successfully"

C:\Users\SVCT-WI-Jenkins\Desktop>goto exit
In here the errorlevel doesn't handle the error. it shows success if the command got failed also.

Could you please help me to fix this.
I don't have the modify access for that folder so that the error is coming. So the same thing I've to fail the job. But in my case the file is not deleted but the job is getting succeed.
How do I handle the error in this case, could you please advise.

Post Reply