Parsing filename in Windows CMD Line?

Chapbass

Diamond Member
May 31, 2004
3,147
96
91
Hey all, quick question:

I'm trying to write a batch file to get an automated method to upload some ftp files. We need to put these files into a subfolder daily (with the date as the name of the subfolder). I was thinking that they were always going to use todays date as the folder name, which I figured out no problem.


However, now my users are saying that they want to have it reference the file name in case they don't get to it at night :\.

So for example, they have a collection of files that may look like something like this (using dummy words instead of a real file example):

Bob_Is_a_Real_Life_Person_05-02-2011.txt

In this case I would want to create a directory called 05-02-2011 (or even better would be 20110502) and put any text files related to it in that directory (note, all of the files would start with Bob, so I was planning on just doing copy bob*.txt into 20110502\bob*.txt using a for loop).

The characters leading up to the 05-02-2011.txt part can be any number of characters, so I can't really go by that (thats how I did the date one). Is there a function in the command line to look at a certain character (maybe a -) and go back 2 spaces to parse from there, or something like that?

Sorry if its a little confusing, if you need me to clarify lemme know!

Thanks AT! Learning this stuff is awesome, but I'm a little stumped on how to do this one.
 

MerlinRML

Senior member
Sep 9, 2005
207
0
71
Let me start by saying I'm not totally clear on which piece you're struggling with.

Are you trying to copy files based on two key variables, some leading piece "bob" and a certain date at the end of the file with noncernables in the middle?
You could do:
Code:
copy bob*05-02-2011.txt

It should get every .txt file that starts with bob and ends with the date specified.

Otherwise, you might try:
Code:
dir | find /i "name" | find /i "date"
which would get you a list of files that contain one thing, then further pare that list down with the second string and then process the output one by one.
 

BoberFett

Lifer
Oct 9, 1999
37,562
9
81
Assuming you're stuck on Windows where the included command line utilities are fairly weak, you might be better off using a VBScript and it's string handling for this sort of thing.
 

Chapbass

Diamond Member
May 31, 2004
3,147
96
91
Let me start by saying I'm not totally clear on which piece you're struggling with.

Are you trying to copy files based on two key variables, some leading piece "bob" and a certain date at the end of the file with noncernables in the middle?
You could do:
Code:
copy bob*05-02-2011.txt

It should get every .txt file that starts with bob and ends with the date specified.

Otherwise, you might try:
Code:
dir | find /i "name" | find /i "date"
which would get you a list of files that contain one thing, then further pare that list down with the second string and then process the output one by one.

Sorry, I guess I wasn't super clear. I want to take that file name, create a directory based on the date that is listed in the file (at the end of a random number of characters) and put those files into that said directory.

EDIT: and by super clear I mean clear at all. Definitely not trying to insult you :D
 
Last edited:

MerlinRML

Senior member
Sep 9, 2005
207
0
71
Sorry, I guess I wasn't super clear. I want to take that file name, create a directory based on the date that is listed in the file (at the end of a random number of characters) and put those files into that said directory.

EDIT: and by super clear I mean clear at all. Definitely not trying to insult you :D

Ah, I think that helps a lot.

So it sounds like you have a bunch of files bob*.txt, sam*.txt, etc and the last characters are the filename are a date. You would like to sort those groups of named files by the date at the end of the file and you are struggling with how to extract the date?

As long as the date is always at the end of the file and always the same format (i.e. MM-DD-YYYY) you can try something along the lines of:
Code:
FOR loop to display filenames do (
REM take the final 14 characters of the filename
SET lastchars=%%A:~-14%
REM trim out any .txt strings in the filename
SET lastchars=%lastchars:.txt=%
)

You'll have to modify that code a bit, as it won't work without disablingexpansion and probably a few other tweaks. But it essentially takes the last 14 digits of the filename, which should be the date and .txt extension. Then just trim off the .txt extension, and you should have the date extracted from the filename. You might also want to build in some string validation to make sure you really do have a date, but that's up to you.

Hope that is more along the lines of answering your question.
 

Chapbass

Diamond Member
May 31, 2004
3,147
96
91
Oh thats just awesome. Didn't know you could go backwards from the end. Okay, let me play with that a little bit and see how it goes.
 

takeru

Golden Member
Jan 1, 2002
1,206
8
81
mind telling a bit more as to what methods are available to you? what operating system is this running from? can you use powershell? vbscript? batch only? windows xp and up supports ps and vbs afterall, making it much easier.
 

Chapbass

Diamond Member
May 31, 2004
3,147
96
91
You know honestly I'm not positive. I know its going to be used by an end user of mine, but I didn't take a peek at what OS theyre on. I would think PS would make it easy (look at the creation date of the file? that would match up with the name of it). Honestly I'm not sure though.

I'll let you know tomorrow.