Computing.Net > Forums > Programming > extract filename in batch + more

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

extract filename in batch + more

Reply to Message Icon

Name: netroam
Date: October 5, 2004 at 00:06:04 Pacific
OS: Windows XP Pro
CPU/Ram: Intel Pentium 4, 2.4 GHz,
Comment:

Hello all!

I'm having a bit of trouble here at my work. We have a customer that sends us semicolon-separated files through FTP-upload several times a day. The files are used in a PHP-script so they can be added to a database and given an output to be printed out. All this works perfectly - no trouble there.

The problem is though that the filenames can be rather confusing to figure out as they consist of the actual date + timestamp. Examples of files can be as follows:

041005_082204.dat
041005_055354.dat
041004_081423.dat

and so on ... (not taken into consideration whether it's AM or PM though - that's the confusing part).

What I want to do is make a kind of batch-file that can extract the first 6 characters from the filename, check whether the file is from today or tomorrow and move the rest to another specified folder (for example today is October 5th, so the filenames with 041005 and 041004 should remain in the folder and move the rest somewhere else).

Is that possible somehow? If so, I'd appreciate a lot if someone could help me. I'm not at all a DOS programming shark or anything, but I hope you can help me anyway :)

All the best,

/// netroam



Sponsored Link
Ads by Google

Response Number 1
Name: dtech10
Date: October 5, 2004 at 16:15:23 Pacific
Reply:

Hi
Something like this help

@echo off
rem May need to modify set commands as
rem your date format may be different as Im english
for /f "tokens=1-3 delims=/- " %%f in ('date /t') do (
set dd=%%f
set mm=%%g
set yy=%%h
)

rem shorted year to 2 digits
set yy=%yy:~2,2%

rem Set Todays var
set Today=%yy%%mm%%dd%

rem Add 1 to day
set /a dd=%dd%+1

rem if day less than 10 add leading zero
if %dd% LSS 10 set dd=0%dd%

set Tomorrow=%yy%%mm%%dd%

rem display vars just for show
echo Today=%Today%
echo Tomorrow=%Tomorrow%
echo.

rem get files not containing string %Today% and %Tomorrow%
dir /b *.dat | find /v "%Today%" | find /v "%Tomorrow%" > Found.txt

rem move filelist to \Dir\
for /f %%a in (Found.txt) do move %%a \Dir\%%a

rem tidyup
del Found.txt
set Today=
set Tomorrow=
set dd=
set mm=
set yy=



0

Response Number 2
Name: FishMonger
Date: October 5, 2004 at 21:35:30 Pacific
Reply:

>> rem Add 1 to day
>> set /a dd=%dd%+1

There is a slight flaw with that method. How do plan on rolling over to the next month?

using that method
041031

becomes
041032

when it should be
041101

When needing to calculate past and future dates, it's best to use one of the (many) more powerful scripting languages such as vb, or Perl. In fact, you could use your current PHP script to calculate the dates and move the unwanted files prior to the database updates.

I personal prefer to use Perl, so here's a short script that can be called separately or from within your PHP script.


#!perl -w

use strict;
use POSIX;
use File::Copy;

my $today = strftime("%y%m%d", localtime(time));
my $tomarrow = strftime("%y%m%d", localtime(time + 86400));

while (<*.dat>) {
unless (/^$today/ || /^$tomarrow/) {
move($_, "c:/some/dir/$_");
}


0

Response Number 3
Name: dtech10
Date: October 7, 2004 at 05:35:27 Pacific
Reply:

Hi Fishmonger

Thanks I completely forgot about that.
Your right ablut using better scriping language than Batch files.
I could modify the Batch file to do this, as thats's whay Netraam whats.


0

Response Number 4
Name: netroam
Date: October 11, 2004 at 00:55:39 Pacific
Reply:

Hi Fishmonger & dtech10

Thank you both for your replies. It's really great to see. Sorry for my late answer. Haven't checked the forum until now.

First of all - I didn't know it could be done in PHP which is why I asked about it as a batch-file. The thing I thought about PHP is that since it's a server-side script it can't affect anything on the local computer's file system seeing as how it would be an excellent tool for any hacker to use in that case. I tried several times both to affect the file system through PHP and also through Microsoft Outlook as I thought I could send a sort of local URL which would then execute a batch-file with link to the file that needs to be stored somewhere else for example:

execute.bat 041005_082204.dat

which would then move the file to another folder simply by making this:

move c:\some\dir\%1 c:\some\other\dir

But that never worked out.

I haven't tried any of your solutions yet though. To tell you the truth I don't know exactly where to implement the PHP-solution in my PHP seeing as how I kind of took the whole script from PHPMyAdmin and used in my own little administration system (that script puts all semicolon-separated data into the database the way it should, so I thought there was no need to invent the wheel again and go through a long process of programming it all - which I might not even have enough PHP-knowledge to do) :)

Since I wrote the message I've tried to figure out what to do with it exactly. It all basically was for a temporary solution before we got our actual system up and running seeing as how we're going to use all these semicolon-separated files in connection with the implementation of a barcoding system at our company. We will receive the files and the system automatically adds the data to our Microsoft Navision C5 system and C5 will then move the files to another folder. It was a job for our C5-programmer to make this but it has been a bit slow as to implementing it all, so I needed to make a temporary solution until it worked a 100%

Anyway - thank you both for your efforts - I will try to see if I can get it working both with the PHP-solution and the batch-solution.

Fishmonger: If you have an idea as to how and where to implement the PHP-script after the above explanation regarding my current script, I would love to know, since I'm not sure where it should be done. Thanx!

/// NetRoam


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More


Database connection in VB... Crystal Reports.net-Runti...



Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: extract filename in batch + more

Batch - search filename to copy www.computing.net/answers/programming/batch-search-filename-to-copy/15064.html

Extract substring in batch file www.computing.net/answers/programming/extract-substring-in-batch-file/15514.html

Find then compare two #'s in batch www.computing.net/answers/programming/find-then-compare-two-s-in-batch/17217.html