I've came across several posts and even code online about .batch files to delete folders and files. However, I am still pretty confused by the myriad amount of information that's on the Internet. Is there a specific way of writing a script that automatically deletes folders that are older than 30 days (the script has to be smart enough to run itself and not be done manually)
I appreciate your help and I am willing to learn here. Thanks!
P.S. I've attached an example of the kind of folders that I would like to delete. As you can see these folders date back to 2008
This will remove the entire directory tree, so use it with coution and remove the echo, if the output is OK:
forfiles /d -30 /c "cmd /c if #isdir==TRUE echo rd /s /q #fname"
Scripts can not run them selves automatically, but the os provides facilities to do that using scheduled tasks. So you need to create yourself a batch file to delete what you need and then add a recurring scheduled task to do the work.
There is no built-in batch command to work only on files with a certain age. But you can download the forfiles tool from Microsoft. It allows to execute commands on files of a certain age only (on the command line).
Related
I know I can use the below code to move all files from a source folder to a destination folder but how would I do this within a specific period of time. For example, I want to copy files from a folder into another folder every day at 2:00am. How can I do this with a script?
echo off
set X=<days>
set "source=C:\<Source Folder Path>"
set "destination=D:\<Destination Folder Path>"
robocopy "%source%" "%destination%" /mov /minage:%X%
exit /b
In case you want to do something periodically in Windows, using a script, I would give you following advise:
Write a script which does what you need to do.
Configure task scheduler to run the frequency and time table of your choice: adding this to the script itself is not a good idea (imagine you restart your computer and you have forgotten to re-launch your script, then it won't be executed again).
I just started working in a school and the way we have the computers set up they automatically download a students user from the network when a student logs into any computer. We're working on making a script to run this command for cleaning them :
for /D %f in (*20**) do rmdir %f /s /q
In command prompt it shows every user with 20 in the name and deletes them from the computer along with everything in these folders.
We can't just change the file name to .bat instead of storing it in a note pad to copy to a command prompt window.
The batch file we currently have is
#echo off
pause
for /d %%f in (*20**) do rmdir C:\Users\%%f /s /q
pause
Which shows each pause to try to help me debug it but doesn't delete the folder. What am I doing wrong here?
I wouldn't do this. As I assume it's something like:
Regular%20User
%20 is basically a space.
To address your issue of wanting to clean them, as I assume it is the temp profile that is created when they login(from a network profile), rather than making the script clean anything with 20 in it, make a script that removes users locally that are of a certain Group Policy group. That way your admin accounts stay locally, and if your script is done right, your student's profiles are autocleaned after a set delay. (I would recommend 30 days, as you can use the temp profile as evidence if a student is caught on a computer he/she shouldn't be on :)
I want to use SCCM 2012 to distribute some files (not application, no executable there).
I know there is a way to create a batch file with xcopy command, and create a package to deploy that batch file to the clients. However, if I use xcopy I cannot leverage the BITS functionality and other benefits from SCCM.
Is this possible?
Hi I can see this is an old post, but I have used SCCM to copy files in the past.
A company I worked for in the past refused to use GP for any file copy (don't get me started) so I had to implement GP files over SCCM.
Here are some examples I used.
xcopy User.bmp "%ProgramData%\Microsoft\User Account Pictures" /c /y /r
REG ADD HKLM\Software\FILE_GP /v Copy_User_image /t REG_sz /d 1.0 /f
timeout 5
if not exist "%windir%\system32\oobe\Info\backgrounds" md "%windir%\system32\oobe\Info\backgrounds"
xcopy backgrounddefault.jpg "%windir%\system32\oobe\Info\backgrounds" /c /y /r
REG ADD HKLM\Software\File_GP /v Backgrounddefault /t REG_sz /d 1.0 /f
timeout 5
I had the script add the Registry key so I could use Application deployment and have the detection method look for the key as well as the file. So if a user deleted the file it would return.
The main reason for me to have the Reg Key was if you wanted to push an updated version of the file I could set the new copy script to push 1.1 to the reg key and have the detection look for that version, then I could retire the 1.0 version.
I found also for short scripts of 1 file of small size without the timeout line it would report failed immediately, if there were larger files I didn't need the timeout. The initial fail does clear after some time without the timeout line.
It should work if you chose your settings right.
In your batch make sure that all the paths and references are relative and never absolute.
Then when you create the deployment there is a point called "Distribution Points" where you can select the deployment options for fast as well as unreliable networks. There you would have to choose "Download content from distribution point and run locally" in both cases. Then your batch would basically copy the files from the CCM cache to the hard drive.
This method should work for packages, for applications I think it would be more difficult because you cannot directly specify deployment options for fast networks there.
Packages are downloaded to the clients with BITS by default. The XCOPY command would copy from CCMCACHE to final destination on the machine.
Not sure if you have tried it or not but if you have deployed any .exe file for application its ask for dependence file in that you mention your required file which need to copy and create a script to copy data from ccmcache to copy the data to destination folder. for this you can get the use of BITS and to check if the file are copied or not you can use a proper detection method.
I used a PowerShell script for this purpose and it works like charm.
Create a new script and change this one liner to your mods:
Copy-Item -Path "\\[NETWORK PATH]" -Destination "[DESTINATION PATH]" -Recurse
Other way around—if need to make a required package—could be to create a new application deployment and run this through PowerShell (ExecutionPolicy ByPass) and manually install from the client side; if you don't want user interaction, make it a required deploy.
So I have a batch file that copies files from one folder to a bunch of different folders.
The purpose of this batch file is to be like a simple installer(Without registry edits) to make it easier for others to use a set of files I am distributing. Because this is being widely distributed, it will need to work on windows operatings systems from XP on. The files need to be moved to the program files of a program (This works), their documents folder(Also works), and the program folder in the all users start menu(Does not work).
So far I have been unsuccessful with xcopy and robocopy as they both tell me "access denied". Also running the batch file with "Run as administrator". If you can think of a more effective way to do what I'm trying to do besides a batch file I am open to ideas.
xcopy /q /y ".\add_ui.bat" "%PROGRAMDATA%\Microsoft\Windows\Start Menu\Programs\My Program\Add.bat"
I'm sure there's some simple trick that I'm missing.
Thanks in advance!
You must have local admin rights to write to that folder and your batch file will need to be ran with elevated permissions (Run As Adminstrator).
I'm trying to run a basic forfiles command, but no matter what mask I use it always simply prints the whole content of the directory. Not filtered for *.bat no "is a batch file", just a simple list off all files in C:\
I'm working on a winXPproffesional. What could be the cause?
forfiles /p c:\ /s /m *.bat /c "cmd /c echo #file is a batch file"
Somehow this version uses a different syntax:
FORFILES -pc:\ -s -m*.BAT -c"CMD /C Echo #file is a batch file"
works!
The command should work.
Maybe you have a FORFILES.BAT, FORFILES.CMD or bogus copy of FORFILES.EXE in either your current directory or another directory that happens to be in your PATH.
Try running this command from the command line to verify that the correct FORFILES.EXE is executing.
for %F in (forfiles.exe forfiles.bat forfiles.cmd) do #echo %~$path:F
If that is not the problem, then it looks to me like you have a bugged version of FORFILES.
Try using one option at a time to see which, if any of the options work
As a matter of fact, there's no forfiles in Windows XP but there is one in Windows 2003 Server, with the syntax as in your question.
Windows 2000 did have the tool, but only in the form of a resource kit, which had to be downloaded separately. For some reason, the corresponding Technet article doesn't list forfiles as part of the kit, although, as can be derived from this and this article, it must have been there initially. Maybe the guys at Microsoft decided to stop distributing the older version to avoid possible confusion with the newer one.
And yes, it seems like your forfiles copy is the older version, which does have a slightly different syntax, as explained in this post.