Delete temporary files on startup - scripting

I have a program which when it runs it fills Temp folder with lots of .tmp files. This is causing C drive to fill up. I have been asked to investigate if it's possible to write a script in dos to delete temporary files on startup. I also wish to delay the program starting until all files are deleted. This would need to happen every time on start-up. It would be great if this could be installed via a flash drive.
I would be grateful on any pointers on how this could be done

The little batch I am using to delete my temporary files:
#echo off
rd %temp% /s /q
md %temp%
cls
echo Temporary Files have been deleted!
echo.
pause
%temp% is a path which always results in your current temporary folder. However note that there are more temporary file locations like C:\Windows\temp.
If you just want to delete TMP files, go with del C:\<MyPath>\*.tmp.

There are probably more sophisticated ways, but the good old fashioned del c:\Temp\*.* should be a good start.
There's a list of all the options, here: http://www.computerhope.com/delhlp.htm
You will probably want /F (delete read only), /S (sub-directories) and /Q (quiet)

I assume, the following row in c:\autoexec.bat file may help:
del c:\path\to\temp\files\*.tmp

Cheers for replies. This is what I'm using
c:
cd \
cd "c:\Documents and Settings\user\Local Settings\Temp\"
del *.tmp /f/s/q
echo All tmp files deleted.
pause
This seems to do what I want it to do. Now I need it to do this everytime PC starts up. Is there a way to install this via flash drive? ie write a batch file with all commands, put on flash drive. double click .bat file, now installed and will run on startup? (Have a number of PCs which need same thing)

Related

Check for new files at program start

I need to write a .dll for a program that works with some files.
I need to code that every time someone starts the program, it should check in a folder for new files and if there are new ones, it should copy them automatically in the programs folder and restart the program. If there are none the program should just start normally.
My problem is that I dont know how to make the program identify new files since the last time the program was started/closed, because I dont want to monitor the folder the whole time the program is running, but only at the start of the program.
I thought about comparing the files in the 2 folders, but with houndreds of files it would be bad perfomance wise, wouldnt it ?
Besides that if someone knows how to automatically copy files and could give me an idea for it too, that would be great.
Iam very new to vb.net and only have limited experience with coding, so forgive me if I ask some easy questions.
Every idea and answer is much appreciated. Thank you!
UPDATE: Thank you very much guys for your ideas, gonna test some of these :)
You could try this approach.
I use this approach in backing-up my files into our server.
This will check for new files and newer-dated version of an existing file (if any) then overwrites it.
I used the XCopy command in my batch file
Creating batch files is pretty much basic.
Open a Notepad
Paste this command: XCopy "C:\YourSourceFolder\*.*"
"D:\YourDestinationFolder" /D /S /Y
Save the file having an extension of .bat
Explanation regarding XCopy:
*.* specifies that all the files inside your directory will be
copied.
/D means it copies only those files whose source time is newer
than the destination time.
/S will copy directories and sub directories except empty ones.
/Y overwrites existing files without prompting
Source: MS-DOS XCopy Command
Now, running it through VB.Net takes only a one line of code. You could put it in a Button_Click,Timer_tick or any events you prefer.
System.Diagnostics.Process.Start("C:\YourBatFile.bat")
Of course, the directory still varies on where did you saved your .bat file.
Source: Run a batch file in VB.Net
In your case: You could use a timer that executes this batch file in an interval of your preference.
You can create a windows service and use a filewatcher https://msdn.microsoft.com/en-us/library/system.io.filesystemwatcher(v=vs.110).aspx

Move or Copy Files within subfolders to another folder

I've been searching around for quite some time but nothing I've found seems to work for me.
I would really like to move the files to another folder on the same drive if possible just in-case the drive happens to be low on space. but none of the move scripts i found seemed to work for me.
i've tried using move, move-items, copy, xcopy, and even robocopy.
robocopy is the only one to work for me so far even tho its creating a new file, it also creates a subfolder that the file is in. in the new location.
so this how my work looks so far
D:\DVDBD-Projects\Project1\
Movie1\
Movie2\
Movie3\
there is a .mkv file in each movie folder and I would like to move the .mkv in each folder under projects to a new location without creating the folder it was originally in.
So the .mkv files will be moved to D:\DVDBD-Projects\Complete without any folders just mkv files.
This is my code
robocopy "D:\DVDBD-Projects\Project1" "D:\DVDBD-Projects\Complete" *.mkv /s
this will copy the files to the new location but also create the movie1 folder and so on. if i dont use the /s switch it wont look inside the subfolders and copy the files.
So i was hoping you guys could help me out, is there a way that i can copy or move just the .mkv files to the complete folder.
i plan on adding this code to script that encodes the movies from my camera so it saves me alot of point and clicking and cutting and pasting.
*note - looks like i can use this code
pushd "D:\DVDBD-Projects\Project1"
for /r %%a in (*.mkv) do (
MOVE "%%a" "D:\DVDBD-Projects\Complete\%%~nxa"
)
popd
this will take all files from subfolders and move them to the destination folder
i could probably also install xxcopy which extends xcopy which would allow me to copy the files from sub directories without creating new folders in the destination
for /f "delims=|" %%s in ('dir /ad/b') do robocopy "\\source\%%s\" "destination\" *.mkv /s

Move folder structure from one computer to another computer when these computers aren't networked?

I have two computers that aren't networked. I need to replicate the folder structure of one drive that exists on one computer and put it on the other computer. Both Windows 7 machines. I don't need the files, just folders/directories. The drive letters are the same on both computers (Y). The computers are miles apart physically, but I do have access to the computer I am trying to get the folder structure of via LogMeIn.
I am thinking I need to save the folder structure to a file using some process. Move that file to my computer (via email or LogMeIn File Manager) and run some process to put in on my computer.
Is there a better solution? If not, is there code out there to do this via VBA, Cmd window, .bat script, VB.NET, or VBS? I know I can write it in VBA, but I'd rather not recreate the wheel if I don't have to.
I don't have a 'process'/program that does this. LogMeIn File Manager doesn't do this either (I asked). There are lots of paths on this drive that I need so creating them by hand would take a long time. I searched and found a lot of solutions that work with computers that are networked, but these computers are not networked, hence why I think I need to save it to a file. Again, I don't want all the files on the drive (its huge and most of the files are unnecessary), just folders.
thanks.
Create a directory listing of the source computer and redirect the output to a text file:
dir /ad /b /s >> dirlist.txt
The switches to dir are (more info at MS TechNet):
/ad List only files with attribute of directory
/b Bare format (no heading information or summary)
/s Displays files in specified directory and all subdirectories
Transfer dirlist.txt from the source computer to the destination computer.
On the destination computer, use a for /F with that text file from the command prompt to create the directory structure:
for /F "delims=" %i in (dirlist.txt) do md "%i"
The switches to for are documented at MS TechNet
You have many options:
Windows command xcopy source destination /T /E. The /T option creates the directory structure, and the /E option includes empty directories.
Bundle the empty directory structure into an installer (perhaps as a zip file).
If the structure isn't relatively small and not likely to change, you could just put a bunch of md commands into a batch file.
Combination - xcopy the structure locally, zip it, transfer it, unzip it.

xcopy "Access denied" with /y /o /s /c /h

I'm currently refining and updating my offline virus database updater for Malwarebytes. It's simply all the Malwarebytes data from C:\ProgramData\Malwarebytes (virus database, build number, etc) inside an SFX .exe archive set to run a batch file after successful extraction.
The Problem
My existing updater works by copying each file with individual xcopy commands, but this time I want it to simply copy the whole folder. But when trying to xcopy to C:\ProgramData, I get "Access denied" for everything.
This occurs even though I'm using the /y and /o switches and running the .exe as Administrator (from the context menu) to achieve elevated permissions. I know that I'm hitting the right directory and that the update does work with a manual copy and paste of the folder.
Is it just that it's not possible to command-copy a folder to ProgramData in Windows 7?
Extra Information
Inside the SFX .exe archive, I have a folder called "Malwarebytes" and a batch file. This is the only relevant segment of the batch file (I have independently confirmed that the rest of it works):
xcopy "Malwarebytes" "C:\ProgramData\Malwarebytes"\ /y /s /c /h /o
Odd.. It just started working.
The only idea I have for this is that when I was trying this, I was trying it with an activated version of Malwarebytes, which means that it was running a service in the background, whereas this time I was not using an activated Malwarebytes'.
This could have possibly been denying access to the files because they were in use.
I don't know. Thanks anyway, guys.
I have encountered this problem without pin pointing the problem However I used Robocopy instead. So simply replace xcopy with Robocopy with appropriate switches and it should work ...

xcopy with unc path adds virtual network drives

When i copy files to a some network path with xcopy command I get new virtual drives on my pc .
The problem is that i do that within a batch script that runs in scheduler so i get there a lot of drives to same location.
How I can copy files to a network with out getting a new drives on the pc or just remove them
?
Thanks.
Wait a minute. You're using pushd. pushd, as the documentation states:
If Command Extensions are enabled the
PUSHD command accepts network paths in
addition to the normal drive letter
and path. If a network path is
specified, PUSHD will create a
temporary drive letter that points to
that specified network resource and
then change the current drive and
directory, using the newly defined
drive letter. Temporary drive letters
are allocated from Z: on down, using
the first unused drive letter found.
While that's perfectly fine to use, you should never call pushd without an appropriate popd afterwards.
So instead of just
pushd \\somepath\etc
xcopy "%mycopyposition%*.exe"
you add another line
popd
which will de-allocate the temporary drive letter again.
It sounds like the command you are using to call xcopy. Can we see that ?
the net command is your fiend (as good a fiend you can get on Windows). To remove a network drive you might want to look at:
net use /?