At work we handle people every day who wants help regarding signing on to their Online Bank from home. Some times we need the user to delete files and guiding them to do this, can be tiresome and bothersome a lot of the time, wasting upwards to 20 minutes because the user is inexperienced in computers.
We have been talking about a solution (if possible) where we let the user download a batch file that we tell them to run, which can then delete the required files for us. I ask this question here, because none of us here at work, have experience with batch files.
Personally I've played around with it from time to time but I can't really come up with a solution that fits our needs and when I search around on the net, I can't find a solution that fits either. The script would have to locate the folder automatically (if possible).
Is this possible?
Would you be able to help me accomplish this?
Thanks in advance!
Maybe this could help you, I commented it so you understand what it is doing ;)
:: Program to remove Folders which fullfill the criterias
:: You won't see the commands in the console
#echo off
:: Go to drive C (i think you'll need application data or similar and this is
:: on c, but if the bat is started in an other partition, it will search the
:: one he starts in
C:
:: Go to the path in which you'll search, if you want C:\ , remove this
CD "C:\yoursearchstartpath"
:: Okay, this is the loop, it gets all folders ( /A -D is for folders)
:: which have delMe in their name and then asks if you want to delete it
:: showing the path to make sure you don't delete sth accidentially
for /f "delims=" %%a IN ('dir /S /B /A -D *delMe*') do RD /S "%%a"
:: That the batch file is not closed automatically but shows that it's finished
set /p id="Finished, press Space to quit " %=%
A simple one:
rd /s "%USERPROFILE%\.oces2"
If they follow a pattern (e.g. start with .oces):
#echo off
cd /d "%USERPROFILE%"
for /d %%i in (.oces*) do rd /s "%%i"
pause
Check out this link
http://support.microsoft.com/kb/65994
I created a sample batch file A.BAT. It checks if the C:\Folder\F1 exists, and removed the folder F1 if available
IF EXIST D:\FOLDER\F1 GOTO A
EXIT
:A
D:
DEL D:\FOLDER\F1\*.*
RMDIR F1
Hope it helps
Related
I am trying to make simple security program for my company.
We usually make a lot of doc or ppt(x) files and
for some reason, we need to make them disable as soon as possible.
So we usually deleted the all of files but It took so long.
So I thought I can do that by overwritting the files.
If I have a empty doc or ppt files then overwite all of doc, ppt files in working drive each, then it will be faster and much safer than just deleting.
So I tried to use xcopy
Assuming empty.doc is just empty doc file and
xcopy /s /y c:\users\mycom\empty.doc c:\*.doc
But it said cannot perform cyclic copy
I need you guys help
and I am glad to hear suggestion.
Thanks.
This is an old, old batch file that I employed for such an endeavour:
:: dispose.bat
#echo off
:: Check parameter(s), stripping quotes
set yp1=%1
set ydf=
:insistp1
if defined yp1 set yp1=%yp1:"=%
if not defined yp1 for %%i in (echo goto) do %%i error - filename required
if not exist "%yp1%" for %%i in (echo goto) do %%i error - file not found
for %%i in ("%yp1%") do (set yfr=%%i&call :zapit %%~di) 2>nul
:error
:: Clean up variables used
if defined ydf echo Warning! dispose failed!!
for %%i in (yp1 yfr yrn ydf) do set %%i=
goto :eof
:zapit
set yfr=%yfr:"=%
IF /i %1 == u: DEL "%yfr%" &goto :eof
if not exist %1\delete\. md %1\delete
(set yrn=)
:rndloop
set yrn=%yrn%%random%
if exist %1\delete\%yrn% goto rndloop
if not exist "%yfr%" set ydf=Y&goto :eof
move "%yfr%" %1\delete\%yrn%>nul
goto :eof
:: dispose.bat ends
Noting that u: is a RAMDRIVE on my system, hence mere deletion is all that is required.
The purpose is not to actually delete the files, but to move them to a directory named ?:\delete and provide them with a random name in that directory.
Since the file is simply MOVEd it is quite fast, which addresses your time consideration.
An issue for me is the idea of copying a file over all of the files you target. If the file that you copy is shorter than the other files, some data wilstill be available to be recovered. Regardless, it will alwats be slower than simply deleting the files (which you say you are currently doing.)
This scheme simply accumulates the files-to-be-deleted in a known directory on the same drive (so they will simply be moved, not copied.)
Once they are in your \delete directory, you can let a utility like ccleaner or recuva loose on that single directory in the background and it will overwrite the files a specified number of times.
Here's a simpler method. Be careful.
At the command line:
for /r c:\ %A in (*.doc? *.ppt?) do echo. > %A
In a batch file:
for /r c:\ %%A in (*.doc? *.ppt?) do echo. > %%A
EDIT:
To replace with a file, see the example below. Replace the example's d:\path\file.ext with your intended file. Note that the previous option will work much faster with a similar result.
At the command line:
for /r c:\ %A in (*.doc? *.ppt?) do copy d:\path\file.ext > %A
In a batch file:
for /r c:\ %%A in (*.doc? *.ppt?) do copy d:\path\file.ext > %%A
Either way, as noted in Magoo's answer, larger files will still have recoverable data on the drive. You stated in a comment:
But if I overwrite the original files, then they cannot guess what it
was unless they got bak files
This isn't accurate. Forensic tools can retrieve the partial data that wasn't overwritten with new content.
This is a really beginners question. I have never work with scripts before and today I decided to start learning because of some unknown reason upgrading to Windows 10 duplicated many of my files. There is a pattern for music files, duplicates end in -1,-2,-3... and so on. I created a script with the following and it did work to delete duplicated files.
del E:\folder_name\*-1.mp3
del E:\folder_name\*-2.mp3
del E:\folder_name\*-3.mp3
However, there are too many folders and specifying one by one will take me forever. I found this script to recursively loop through subdirectories.
For /R E:\music_sample\ %%G IN (*-1.mp3) do Echo del "%%G"
Which produces the following output
but it does not actually deletes the files. Can you help me understand what I am doing wrong? Thanks!
Remove the term "Echo" from your script:
For /R E:\music_sample\ %%G IN (*-1.mp3) do del "%%G"
Taking a moment to learn something new is always great!
Sometimes there is already a tool for the job.
Agent Ransack allows you to search by patterns/regular expressions for files. This or something similar might save you some time in the future.
del /s "%userprofile%\music\*-1.mp3" "%userprofile%\music\*-2.mp3 "%userprofile%\music\*-3.mp3"
type
set u
and
del /?
for help.
Command prompt has it's own regular expressions. It's different to dos even if the tokens are the same. EG in dos * is only valid at the end of a filename or extension but in command prompt it is valid anywhere.
First post, and I have searched and tried for this but cant seem to find something like this.
Okay, so I have spent some time on this and have hit a wall. I understand and can code this if I am moving on a single drive (with move) but as soon as I try to implement xcopy so that it will work cross drives I begin to struggle.
This is what I have so far. This will look in the directory name for a left parenthesis and if it has it, it will try to move the directory to the other folder. This works correctly.
#echo off
SETLOCAL EnableDelayedExpansion
for /d %%f in ("%C:\Testing\Folder1%\*") do (
SET "folder=%%f"
if /I NOT "!folder:(=!"=="!folder!" move "%%f" C:\Testing\Folder2
)
Example directory names are as follows
Vacation (2004) - Move
Florida Vacation - Dont Move
New York (2014) - Move
Exmaple Folder - Dont Move
Now when I found out move does not work for different drives (D: (source) and E: (destination)) I found out I needed to use Xcopy. When I try to use xcopy instead of move it will never move any files or folders.
I am thinking I might have to mkdir the folder in the new location first and then move everything and delete the old one, but then I would need to substring the directory name from the full path. I probably can do it, but want to verify with you guys before I spend another half day googling how to do it. I have slammed my head into the wall enough for the past few days that I figured some experts could help out.
Attempt with xcopy
#echo off
SETLOCAL EnableDelayedExpansion
for /d %%f in ("%D:\Folder1%\*") do (
SET "folder=%%f"
if /I NOT "!folder:(=!"=="!folder!" xcopy "%%f" E:\Folder2
)
I appreciate any and all help you can give.
Jay
I use the following, perhaps it'll work for you:
#echo off
setlocal enabledelayedexpansion
for /d %%i in (C:\TEST\*(*) do (
xcopy "%%i" "D:\%%~pni" /s /i
)
pause
Substitute paths for your relevant directories. Good luck!
If that title wasn't confusing enough.. hopefully what I am trying to do is a lot simpler to understand.
Windows 7 just in case it needs to be said.
I have multiple directories within the folder I am working in;
C:\WorkingDir\1
C:\WorkingDir\2
C:\WorkingDir\3
and so on
Within each of these folders (1,2,3,etc) is a single sub-directory and no other files or folders;
C:\WorkingDir\1\5E04AB
C:\WorkingDir\2\4F07FC
C:\WorkingDir\3\9DA04F
I need to move each of those single subdirectories from within the parent folder to a new folder;
C:\NewFolder\5E04AB
C:\NewFolder\4F07FC
C:\NewFolder\9DA04F
That's it! I thought it might be simple, but I can't wrap my head around the variables or a better resource explaining how to use them. I don't use batch files much if at all, so I am very sorry for this cry for help. Hopefully someone more knowledgeable has a simple explanation that can help me out :-)
I found this: http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/percent.mspx?mfr=true
But can someone link me to a resource where I can learn more about batch variables and parameters for future reference?
Thank you thank you thank you
Update:
#endoro
Thanks for your response. There must have been a user error the first time I tried to run your code. It is working properly and all is well! Thank you so much!
Update 2
After running the code User1 contributed, It will create my NewFolder directory, but will not copy anything to it. It remains empty. Here is some of the repetitive output in DOS when I run it:
C:\WorkingDir>(
set fldr2=C:\WorkingDir\1\5E04AB
move "C:\WorkingDir\\" "C:\NewFolder\"
)
The system cannot find the file specified.
C:\WorkingDir>(
set fldr2=C:\WorkingDir\2\4F07FC
move "C:\WorkingDir\\" "C:\NewFolder\"
)
The system cannot find the file specified.
C:\WorkingDir>(
set fldr2=C:\WorkingDir\3\9DA04F
move "C:\WorkingDir\\" "C:\NewFolder\"
)
The system cannot find the file specified.`
Please look at the output and remove the echo, if it is OK:
#echo off &setlocal
set "workingdir=WorkingDir"
md "C:\NewFolder" 2>nul
pushd "%workingdir%"
cd
for /d %%i in (*) do for /d %%j in ("%%~i\*") do echo move "%%~fj" "C:\NewFolder\%%~nxj"
popd
I haven't been able to test this but:
#echo off
setlocal EnableDelayedExpansion
md "c:\NewFolder\"
cd "C:\WorkingDir\"
for /D /r %%a in ("*") do (
set fld1=%%a
cd "%fld1%"
for /D /r %%b in ("*") do (
set fld2=%%b
move "c:\WorkingDir\%fld1%\%fld2%" "C:\NewFolder\%fld2%"
)
)
And a good resource for batch:
http://ss64.com/nt/
I'm a bit of a newbie with batch files, but am looking for a script to delete .txt log files that are older than 30 days... (FWIW, I am on XP SP3...)
I tried to follow the example found here, (including downloading the forfiles.exe from here) and ended up with this:
forfiles -p"D:\SQL Database Back Ups\Update_FmlaManagement_Log" -s -m *.txt -d-30 -c "cmd /c del #FILE"
But I get this error:
Error path not found (3rd argument) line 545
I did a lot of googling, but can't seem to figure out what this error is talking about. Somebody suggested using forfiles.exe in order to get the #variables to work, but I tried that (along with the full forfiles path), and nothing seems to be working...
Can someone at least point me in the correct direction as to what this error message is telling me?
Just to see whether I could, I cobbled a little sumn sumn together to delete files older than 30 days old without using forfiles. Make sure the pushd line is set to go to the appropriate directory. I got the 2592000 by subtracting 01/01/2013 from 01/31/2013.
#echo off
setlocal enabledelayedexpansion
pushd "D:\SQL Database Back Ups\Update_FmlaManagement_Log"
set today=%date:~-10%
call :toEpoch %today%
set /a thirtyDaysAgo=%epoch% - 2592000
for /f "tokens=1,5*" %%I in ('dir /o:d *.txt ^| findstr "txt$"') do (
call :toEpoch %%I
if "%%K"=="" (set file=%%J) else (set file=%%J %%K)
if /i %thirtyDaysAgo% GTR !epoch! del /q "!file!"
)
popd
goto :EOF
:toEpoch date
echo wscript.echo datediff("s", "01/01/1970 00:00:00", "%1")>epoch.vbs
for /f %%x in ('cscript /nologo epoch.vbs') do set epoch=%%x
del /q epoch.vbs
goto :EOF
(VBscript borrowed from Cameron Oakenleaf.)
Admittedly, this won't work obsessively accurately. For instance, if a log file was last modified at noon 30 days ago and you're running this at 8:00 am, the file will probably be deleted even though it still has another 4 hours to go to reach 30 days old. And it's probably not nearly as efficient as forfiles.exe.
*shrug* It was a road less traveled and I traveled it.
EDIT: HerbCSO has another, probably better implementation that makes mine look like a Rube Goldberg-ian solution.
#file is only the File name. It does not include the path. If your batch file is not being executed within your path it will not see the file to delete it because you are in a different working directory.
I can't believe that the error comes from your sample line.
My forfiles (Win7) throws an error for the missing space between -p and the path.
And the text of the error message isn't normal for batch commands.
I don't know any batch command that tells you the line where an error occours, nor which parameter is wrong.
So I believe you got the error message from a completly different source.
Or you have a second forfiles command that do some totally other thing.
Or do you named one of your tests forfiles.bat?
Well, I'm not positive, but it seems the issue might have been a bad forfiles.exe from Microsoft's FTP server. I ended up downloading a second forfiles.exe (for Windows Server 2003) from this guy's site (direct download).
After replacing the original exe in C:\WINDOWS\system32 and getting rid of the copy in my downloads folder, I changed the syntax a little from the dashes to forward slashes as follows:
forfiles /p "D:\SQL Database Back Ups\Update_FmlaManagement_Log" /s /m *.txt /d -30 /c "cmd /c del #FILE"
and this worked perfectly first try!
So I guess the answer is, don't download forfile.exe from Microsoft's FTP server? :P
EDIT:
I would be very interested to know if other people have successfully used Microsoft's .exe in the link above? Otherwise maybe it's just something with my particular setup...