Hello, My name is Zach and I am new... so... Hello there.
I am not new to dos/cmd, My first pc was a dos machine... but I am no scripting wizard either. I can understand most basic scripts, and I constantly search the internet to find something that looks like it's up my alley, then borrow and modify away until it is mine. Usually I am satisfied to test the code myself, but this one could have dire consequences if I am wrong, so I would like for a few of the more intelligent souls in the computing world to double check my work for me before I destroy all of my data.
Windows XP - sp3
Just REGULAR OLD batch scripting
Have a video "partition" where I have alot of things happening, automatically downloading files, automatically renaming files, automatically converting files, cleaning up old folders and irrelevant files. This is one of the scripts that I have running periodically at the moment:
@echo off
cls
e:
setlocal EnableExtensions EnableDelayedExpansion
::FIND ANY FILES THAT ARE *NOT* IN A FOLDER CALLED SAMPLE, TV SHOWS
::MOVIES OR RECYCLER - AND THAT *ARE* ENDING WITH THE MP4 EXTENSION
::AND RENAME THOSE FILES USING THE FILEBOT PROGRAM WHICH AUTOMATICALLY
::MOVES THEM AS WELL.
for /r %%a in (*) do (
set "P=%%~pa"
if "!P:~-8!" NEQ "\Sample\" (
if "%%~xa" EQU ".mp4" (
if "!P:~0,10!" NEQ "\TV Shows\" (
if "!P:~0,8!" NEQ "\MOVIES\" (
if "!P:~0,10!" NEQ "\RECYCLER\" (
filebot -script fn:sortivo "%%a" -non-strict --output e:/ )))))
::FIND AND DELETE ANY FILE IN A FOLDER CALLED SAMPLE
if "!P:~-8!" EQU "\Sample\" (
if "%%~xa" NEQ "!qb" (
del "%%a" & rmdir /S /Q "%%~pa" ))
::FIND AND DELETE ANY FILE NOT ALREADY PROCESSED
::UNDER THE RULES ABOVE AND THAT ARE NOT CURRENTLY
::BEING DOWNLOADED
if "%%~xa" NEQ ".mp4" (
if "%%~xa" NEQ ".!qB" (
if "!P:~0,10!" NEQ "\TV Shows\" (
if "!P:~0,8!" NEQ "\MOVIES\" (
del "%%a" ))))
)
:: DELETE ANY EMPTY FOLDER LEFT IN THE DRIVE
for /f "delims=" %%d in ('dir * /s /b /a ^| sort /r') do rd "%%d" 2>nul
:::THIS SECTION OF SCRIPT THAT IS DISABLED WILL CHECK THE
:::AMOUNT OF AVAILABLE SPACE IN THE DRIVE, AND IF THE SPACE
:::IS LESS THAN 8GB, IT WILL BEGIN RECURSIVELY DELETING THE
:::OLDEST FILES IN THE DRIVE UNTIL THE AMOUNT OF AVAILABLE
:::SPACE IS GREATER THAN 8GB
::define
:::SETS %bytesfree% AS THE TEXT AT THE END OF THE DIR COMMAND
:for /f "tokens=3" %%a in ('dir e:\') do set bytesfree=%%a
:::SETS TWO TEMP VARIABLES TO BE MODIFIED IN THE NEXT LOOP
:set tempstr=%bytesfree%
:set strlen=0
:
::loop
:::ADDS 1 TO THE VALUE OF %strlen% UNTIL THE VALUE IS THE LENGTH OF THE TOTAL STRING
:::STORED IN %tempstr% (THE AVAILABLE SPACE ON THE DRIVE)
:if defined tempstr (
: set tempstr=%tempstr:~1%
: set /A strlen += 1
: goto loop
:)
:
:::CHECKS TO SEE IF THE LENGTH OF THE STRING IS 13 CHARACTERS (8,000,000,000)
:::OR LESS. IF SO, AND IF THE FIRST NUMBER IS '8' THEN PREFORM A LOOP AND SET THE
:::OUTPUT OF THE *FIRST LINE* OF THE *DIR* COMMAND SORTED FROM OLDEST TO
:::NEWEST TO BE DELETED. THEN GOTO:DEFINE AND COMPLETE THE PROCESS AGAIN UNTIL
:::THE FIRST NUMBER IS MORE THAN 8, OR THE LENGTH IS MORE THAT 13 CHARACTERS
:if %strlen% LEQ 13 (
: if "%bytesfree:~0,1%" LEQ "8" (
: for /f "delims=*" %%i in ('dir * /s /a-d /od /b') do set "workingfile=%%i" & goto endfor )
: :endfor
: echo del %workingfile%
: goto define )
:)
c:
This disabled section of code SHOULD do exactly as I wish -or- it could wipe the entire partition of every file. If someone could give me their thoughts BEFORE I activate this section of code and obliterate my media files in a few months I would greatly appreciate it!
Can someone smarter than me check this code?
Moderator: DosItHelp
Re: Can someone smarter than me check this code?
Ignore the smileys... they are disabled code "::" followed by a close of the for loop ")"
-
- Posts: 82
- Joined: 30 Dec 2013 10:16
- Location: United States by the big waterfall
Re: Can someone smarter than me check this code?
I haven't read the entire script but I have a suggestion. Make a folder in your temp directory, copy a section of your files there and run the script. No harm, No foul. If it works, great. If not, well, you know you need to fix it. Also, Backup your data! external HD's are cheap. get one.
-Matt
-Matt
Re: Can someone smarter than me check this code?
zthaynes wrote:Ignore the smileys... they are disabled code "::" followed by a close of the for loop ")"
That is why there is options to disable smilies in your posts and using the BB CODE tags will also prevent the smilies.
Re: Can someone smarter than me check this code?
Thought about that, but the code is dependent on the output from the dir command which would show the size of the root directory if I am not mistaken. I could alter the code to make that work, but then I might not get it out back together correctly.
I have already tested snippets of code and they seem to work correctly. I'm just uneasy about the finished product. I've Done it before... one piece in the wrong spot and - boom - not backng up data bites you I'm the rear.
I have already tested snippets of code and they seem to work correctly. I'm just uneasy about the finished product. I've Done it before... one piece in the wrong spot and - boom - not backng up data bites you I'm the rear.
Re: Can someone smarter than me check this code?
You may want to use an isolated virtual machine to protect you from that scenario, virtual PC is free. Saved me a lot of headaches while creating function 'dirDelete_'. Or in cases where a program would crash my machine.