[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

delete temp files in specific user directory

Posted on 2007-11-26
8
Medium Priority
?
472 Views
Last Modified: 2012-06-27
I want to make a script that delete all temperary files in a specific user share.

The script must delete all files in \fileserver\usershare\%username%\sg\temp

The script must run every saterday and have to be for all users. we all got Windows 2003 servers and the files are on a NAS.

So how can i make a script that does this for me.
0
Comment
Question by:josbos
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
8 Comments
 
LVL 6

Expert Comment

by:MorDrakka
ID: 20348974
Hi,

First create your input file by typing: dir /b >> users.txt in the \fileserevr\usershare path.

Then you can say something like:

FOr /F %i in (users.txt) Do (cd \\fileserver\usershare\%i
del *.tmp)

Something like this, you need to have and enter after the cd command. you can put this in a batch file. just test with it a bit.

Hope this helps.
0
 

Author Comment

by:josbos
ID: 20349371
yeah this could work but i don't want this to do with a tekst document (like users.txt). i want to use the AD to read all usernames and that the script automatic removes the folder (or files in the folder) in the usershare.
0
 
LVL 6

Assisted Solution

by:MorDrakka
MorDrakka earned 800 total points
ID: 20350078
Hi,

Ok to do that I created a vb script.

- create a textfile name it testscript.vbs c/p the content from below in it
- modify the Ad string with the details of your own AD
- Test some first!!


On Error Resume Next
 
Const ADS_SCOPE_SUBTREE = 2
 
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objConnection = CreateObject("ADODB.Connection")
Set objCommand =   CreateObject("ADODB.Command")
objConnection.Provider = "ADsDSOObject"
objConnection.Open "Active Directory Provider"
Set objCommand.ActiveConnection = objConnection
 
objCommand.Properties("Page Size") = 1000
objCommand.Properties("Searchscope") = ADS_SCOPE_SUBTREE 
 
objCommand.CommandText = _
    "SELECT HomeDirectory FROM 'LDAP://ou=users,ou=NL,dc=microsoft,dc=com' WHERE objectCategory='user'"  
Set objRecordSet = objCommand.Execute
 
objRecordSet.MoveFirst
Do Until objRecordSet.EOF
   strHomedir = objRecordSet.Fields("HomeDirectory").Value
   
   SET objFolder = objFSO.GetFolder(strHomedir & "\sg\temp")
   objFSO.DeleteFolder(objFolder.Path), TRUE 
   SET objFolder = nothing 
    
   objRecordSet.MoveNext
Loop

Open in new window

0
NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

 
LVL 6

Expert Comment

by:MorDrakka
ID: 20350088
Hi again,

For testing you can use the script below, this will only echo the homedir path in a cmd prompt.

Ciao

M
Do Until objRecordSet.EOF
   strHomedir = objRecordSet.Fields("HomeDirectory").Value
   wscript.echo strHomedir & "\sg\temp"
   objRecordSet.MoveNext
Loop

Open in new window

0
 

Author Comment

by:josbos
ID: 20350400
ok this will work (i tested it with the script below) but when i read this (correct me if i'm wrong) but this will delete the folder or only the files in it??
0
 
LVL 14

Accepted Solution

by:
canali earned 1200 total points
ID: 20353489
The vbs script delete also the temp you need to recreate it
add after the line 24
' re-create the folder BUT with new permissions!! check it
objFSO.CreateFolderFolder(strHomedir & "\sg\temp")

I have wrote a batch tha do the work without delete the temp

Bye Gastone Canali
@echo off
:::::::::::::::::::::::::::::::::::::::::::::::::::::
::
:: *** Gastone Canali ***
::
:: DELallin.cmd
:: delete all file and folders in
:: EX.: \\fileserver\usershare\ANY_FOLDERS\sg\temp
:: where ANY_FOLDERS are all the dirs 
:: in \\fileserver\usershare\
:: "%PreUsrDir%\ANY_USER_FOLDERS\%PostUsrDir%"
::
:::::::::::::::::::::::::::::::::::::::::::::::::::::
setlocal
pushd %~d0%~p0
set logfile=%temp%\_dellog.log
set log=^>^>%logfile% 2^>^&1
 
:: *** Remove Log File
del /f /q %logfile%
 
:::::::::::::::::::::::::::::::::::::::::::::::::::::
:: \\fileserver\usershare\ANY_FOLDERS\sg\temp
:: modify the pameters PreUsrDir  PostUsrDir
:::::::::::::::::::::::::::::::::::::::::::::::::::::
set PreUsrDir=\\fileserver\usershare
set PostUsrDir=sg\temp
 
:: *** no log
::set log=
:: ***************************************
:: nothing done (SAFE TEST MODE)
set echo=echo
:: Comment the line above to Delete files
::****************************************
 
echo ****%date%**%time%****START %log%
if not exist  %PreUsrDir% goto :_ERR
for /f "delims=\"  %%U in ('dir /b /a:d %PreUsrDir%') do call :_DELallin "%PreUsrDir%\%%U\%PostUsrDir%"
echo ****%date%**%time%**** FINE   %log% 
 
goto :_END
 
:_DELallin
if not exist %1 goto :_NODIR
rem *** I want to delete also hidden or ReadOnly files and dirs
%echo%  attrib -h -s -r %1\*.* /s /d  %log% 
rem *** delete all files 
%echo%  del  /q /f /s %1\*.*         %log%
rem *** delete all dirs 
for /f "delims=\"   %%d in ('dir /b /a:d %1') do if exist %1\"%%d"  %echo%  rmdir %1\"%%d" /S /Q
:_NODIR
goto :_EOF
 
:_ERR
echo off
  echo ERROR
  echo ERROR %log%
goto _END
 
:_END
 
:_EOF

Open in new window

0
 

Author Comment

by:josbos
ID: 20356147
@mordakka & canali: thanks for both the answers. They work like hell and i give you both the points because you both got an answer that i could use or anyone could use. But i going to use the script from canali because the script doesn't delete the temp folder.
0
 

Author Closing Comment

by:josbos
ID: 31410935
this was the script (s) i was looking for. thanks both!!
0

Featured Post

Fill in the form and get your FREE NFR key NOW!

Veeam® is happy to provide a FREE NFR server license to certified engineers, trainers, and bloggers.  It allows for the non‑production use of Veeam Agent for Microsoft Windows. This license is valid for five workstations and two servers.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Compliance and data security require steps be taken to prevent unauthorized users from copying data.  Here's one method to prevent data theft via USB drives (and writable optical media).
Let's recap what we learned from yesterday's Skyport Systems webinar.
This tutorial will walk an individual through the process of configuring their Windows Server 2012 domain controller to synchronize its time with a trusted, external resource. Use Google, Bing, or other preferred search engine to locate trusted NTP …
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Suggested Courses

650 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question