Jump to content

Need tips on batch programming


Recommended Posts

Moderator Note

The OP was looking for a script which would delete the most recent file within a directory, (identified as E:\Fraps)

To further help other readers, CoffeFiend posted some great solutions, thanks!

Link to comment
Share on other sites


Unfortunately from the information you have provided I cannot work out exactly what you require.

Please provide us with a proper example of your directory contents, and explain which file is the 'latest'.

Link to comment
Share on other sites

Open a command prompt.

In it type:

DIR /S E:>C:\mydirlist.txt

(presuming that your "1 Tb" drive is E:\, this will produce a Directory listing on the volume in file C:\mydirlist.txt - change drive letters accrding to your setup)

Compress mydirlist.txt into a .zip file and attach it to your next post.

This way we have an exact view of the files you are using.

Also, post some info on what you mean by "store b". :unsure:

jaclaz

Link to comment
Share on other sites

Open a command prompt.

In it type:

DIR /S E:>C:\mydirlist.txt

(presuming that your "1 Tb" drive is E:\, this will produce a Directory listing on the volume in file C:\mydirlist.txt - change drive letters accrding to your setup)

In addition to that, since we are completely unaware of which Operating System you are using, (important for determining the best solution), I''d suggest changing it to:

DIR/S/A/D/O-G E:>"%USERPROFILE%\Desktop\mydirlist.txt"
(Change the drive letter in red to match your source drive.)

Not only do I find it a better formatted result, you should always have rights to place the output file on your Desktop where you can find it.

Link to comment
Share on other sites

Open a command prompt.

In it type:

DIR /S E:>C:\mydirlist.txt

(presuming that your "1 Tb" drive is E:\, this will produce a Directory listing on the volume in file C:\mydirlist.txt - change drive letters accrding to your setup)

In addition to that, since we are completely unaware of which Operating System you are using, (important for determining the best solution), I''d suggest changing it to:

DIR/S/A/D/O-G E:>"%USERPROFILE%\Desktop\mydirlist.txt"
(Change the drive letter in red to match your source drive.)

Not only do I find it a better formatted result, you should always have rights to place the output file on your Desktop where you can find it.

I'm running Windows 7.

I use fraps to record gameplay video to an external hard drive in E:\fraps\, my operating system is on drive C:\.

The dir /s does appear to list them in the correct order, is there a way to delete the last file on that list?

That is the only functionality that I need, to delete the last created file in a directory.

To clarify the original post,

I don't know batch, but i guess to script it i'd do something like this

//the only programming knowledge I have is Galaxy and Java, so I was thinking using pointers.

Pointer to File %a;

for each File %b in E:\Fraps, {get name/date of creation of file %a, compare to name/doc of %b, then store pointer to %b into %a if %b is newer;}

then Delete file %a;

mydirlist.txt

Edited by xinehp
Link to comment
Share on other sites

There's many ways to skin a cat. Powershell version (just for fun, I'm well aware that's not what you asked for language-wise), assuming I've understood your request properly (deleting the newest file in a directory):

Get-ChildItem E:\fraps | Sort-Object -Property LastWriteTime -Descending | Select-Object -First 1 | Remove-Item

or the shorter version, using built-in aliases and omitting defaults:

gci E:\fraps | sort LastWriteTime -Desc | select -First 1 | rm

It would be trivial to do in vbscript or jscript as well (using a lot more code though -- I like that this was ridiculously fast to write and with very little code). As for batch files, I'll leave that fun to someone else.

Edit: bah, I'm bored. Here's a vbscript quickie that does the same thing too:


Option Explicit
Const path = "E:\fraps"
Dim fso, folder, f, newest
Set fso = CreateObject("Scripting.FileSystemObject")
Set folder = fso.GetFolder(path)
For Each f In folder.Files
If newest = "" Then
Set newest = f
Else
If newest.DateLastModified < f.DateLastModified Then
Set newest = f
End If
End If
Next
fso.DeleteFile (newest.Path)

...and the JScript equivalent:


var fso, folder, files, f, newest, path
path = "E:\\fraps";
fso = new ActiveXObject("Scripting.FileSystemObject");
folder = fso.GetFolder(path);
files = new Enumerator(folder.Files);
for(; !files.atEnd(); files.moveNext() ) {
if (newest == null) {
newest = files.item();
}
else {
if (newest.DateLastModified < files.item().DateLastModified) {
newest = files.item();
}
}
}
fso.DeleteFile (newest.Path);

.. and here's the C# version (accepts the path as an arg; compile with csc.exe):


using System;
using System.IO;

namespace bored
{
class Program
{
static void Main(string[] args)
{
DirectoryInfo di = new DirectoryInfo(args[0]);
FileInfo[] files = di.GetFiles();
Array.Sort(files, (f1, f2) => f2.LastWriteTime.CompareTo(f1.LastWriteTime));
File.Delete(files[0].DirectoryName + "\\" + files[0].Name);
}
}
}

...and the VB version too (path as an arg too; compile with vbc.exe):


Imports System
Imports System.IO

Module bored
Sub Main(ByVal args As String())
Dim files As FileInfo() = New DirectoryInfo(args(0)).GetFiles
Array.Sort(Of FileInfo)(files, Function(f1 As FileInfo, f2 As FileInfo)
Return f2.LastWriteTime.CompareTo(f1.LastWriteTime)
End Function)
File.Delete(files(0).DirectoryName & "\" & files(0).Name)
End Sub
End Module

I'm feeling too lazy for a C++ version (or any other language for that matter). If you want anything else, just ask.

No error handling of any kind in any of them. If there are no files, or the path doesn't exist, or you don't have sufficient permissions, or anything else along those lines then something strange will likely happen.

If you insist on using an antiquitated batch file for some strange reason then someone else will have to take care of it.

Link to comment
Share on other sites

hmm... I guess I do know enough Java to write it, then just write a .cmd to run the java file.. I think if someone know how to do it in batch that would be simpler.

All done, guys. I already downloaded java, set windows 7's environment variables so i can use java through command lines, and wrote the java program. Thanks for attempting to help :P

I thought maybe someone would just know how to do it in batch off the top of their heads, but I guess not.

Edited by xinehp
Link to comment
Share on other sites

hmm... I guess I do know enough Java to write it, then just write a .cmd to run the java file.. I think if someone know how to do it in batch that would be simpler.

All done, guys. I already downloaded java, set windows 7's environment variables so i can use java through command lines, and wrote the java program. Thanks for attempting to help :P

I thought maybe someone would just know how to do it in batch off the top of their heads, but I guess not.

First of all let me say that your Operating System can run all of the first three methods provided by CoffeeFiend directly without the need to download anything!

I would myself have provided and strongly suggested that you run a one line powershell command, (hence the request for your Operating System).

If you really do want an outdated and more long-winded method of achieving the result here's your Windows Command Script, (batch file):

@PUSHD E:\FRAPS 2>NUL||GOTO :EOF
@ECHO OFF & SETLOCAL ENABLEEXTENSIONS DISABLEDELAYEDEXPANSION
SET "LF_=" & IF DEFINED DIRCMD SET "DIRCMD="
FOR /F "TOKENS=*" %%# IN ('DIR/B/OD/A-D') DO SET "LF_=%%#"
IF DEFINED LF_ DEL "%LF_%"
POPD

Please do not remove your opening question from our Forum when you feel the need to do so. It is selfish behaviour which does a dis-service to all readers, especially our Members.

Link to comment
Share on other sites

I thought maybe someone would just know how to do it in batch off the top of their heads, but I guess not.

For the record, your guess is wrong, the problem was that you initially failed to give out the needed details.

And yes, removing the contents of your original post only makes the provided solutions more difficult to be of use to any other member.

The original post was about a way to identify files with names in a format like:

BFBC2Game 2010-07-05 21-14-59-96.avi

BFBC2Game 2010-07-05 21-18-34-41.avi

and delete latest (or the previous one).

jaclaz

Link to comment
Share on other sites

First of all let me say that your Operating System can run all of the first three methods provided by CoffeeFiend directly without the need to download anything!

And the last 2 solutions don't require a download either. They're not interpreted though, so you do have to compile before running them. Win 7 already has all you need to compile, have a peek inside C:\Windows\Microsoft.NET\Framework\v3.5

I don't let anything Java related get near my computer but that's just me.

Link to comment
Share on other sites

hmm... I guess I do know enough Java to write it, then just write a .cmd to run the java file.. I think if someone know how to do it in batch that would be simpler.
@xinehp, welcome to MSFN - there's always going to be multiple ways to write a script, and multiple languages or script environments/parsers to do it in. And it's also worth noting that Java and JavaScript (while both contain the word java) are in no way related. JavaScript, VBScript, PowerShell, and .cmd/batch file execution are all native to Windows 7 / Server 2008 R2. It appears there are multiple scripts in this thread, in multiple parsers and languages which will achieve what you would like to achieve. Let us know which one (or ones) work for you, and remember that using a specific language or parser to write a script is only easy because you know how to use it. I must reiterate that while you can use a .cmd in Windows 7, you should consider learning things like VBscript, Javascript, and PowerShell for administrative scripts simply because they're far more powerful, and they are all (frankly) very easy to use once you learn how to write for them.
Link to comment
Share on other sites

hmm... I guess I do know enough Java to write it, then just write a .cmd to run the java file.. I think if someone know how to do it in batch that would be simpler.
@xinehp, welcome to MSFN - there's always going to be multiple ways to write a script, and multiple languages or script environments/parsers to do it in. And it's also worth noting that Java and JavaScript (while both contain the word java) are in no way related. JavaScript, VBScript, PowerShell, and .cmd/batch file execution are all native to Windows 7 / Server 2008 R2. It appears there are multiple scripts in this thread, in multiple parsers and languages which will achieve what you would like to achieve. Let us know which one (or ones) work for you, and remember that using a specific language or parser to write a script is only easy because you know how to use it. I must reiterate that while you can use a .cmd in Windows 7, you should consider learning things like VBscript, Javascript, and PowerShell for administrative scripts simply because they're far more powerful, and they are all (frankly) very easy to use once you learn how to write for them.

Yeah, I'm beginning to understand that. I'm just victimized by academia's obsession with Java :P Learning another language is definitely on the todo list, but I only have a 1-core brain.

I must apologize to coffefiend for completely ignoring his solutions, I'm incredibly less knowledgeable about Windows than I thought, and his powershell line is freaking elegant. I'm still using my knowledge from 4 years ago writing .cmds to mess with the computers at high school :P I didn't know powershell existed, and I didn't think vbscript came with a native compiler.

Thanks MSFN, I was initially expecting very little replys, from experience on other forums, but you guys are incredibly active (especially mods) and got it in no time, well done.

Edited by xinehp
Link to comment
Share on other sites

I'm just victimized by academia's obsession with Java :P
I feel your pain. As someone who sits in hiring sessions with recent college grads for development positions, the forced usage of Java is really putting these kids at a disadvantage in the working world. Java's good at some things, but it teaches some really bad habits and is poor at getting kids to learn pointers and functional programming, both of which they're going to need to learn to use languages other than Java. There's no way for me to tell if a potential hire is a good candidate or not when they want to do everything in Java, and the fact that we're mostly a Windows shop also means that a lack of knowledge of C, C++, and .NET languages is a drawback as well. Back when I went to school, we had to learn C, pointers, recursive programming, etc - it was difficult on purpose, because it taught you how to be a GOOD developer, not just how to develop. I wish more schools would try harder to make their grads more qualified for what's out there in the real world, but I guess class sizes and budgets are the main concern, not necessarily the education they're giving (or, in fact, NOT giving) those students.

I guess LIT has gone to Java ... boo ;).

/rant I'm off my soapbox... for now.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...