Batch to get file name

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
niobe
Posts: 6
Joined: 09 Aug 2010 01:42

Batch to get file name

#1 Post by niobe » 09 Aug 2010 01:45

Hi everyone.
Firstable I'll like to say sorry by my awful English.
Involved elements:
External USB hard disk
Windows 2003 Server R2 standar edition
Windows XP Professional SP3
Filezilla Server 0.9.33 beta
Blat 2.6.2
Directory monitor 1.0.1.23
What I do:
I have external hard disk connected to W2003 a shared. From XP I have a mapped this hard disk using UNC and this unit is for store files that people upload to FTP server.
I need make a bat that when new file start upload make these actions:
1.-Get file name
2.-Get folder name
3.-Send a email with this data (file and folder name) using Blat
4.-When upload finished send email using Blat to inform that this file is ready to start download
I've done so far:
Using Directory monitor detect that a new file is start upload and call a bat that send me an email using Blat with Folder name in a subject.
This code works:
@echo off
cd folderexample
for %%* in (.) do set var=%%~n*
for /f %%a in ('time/t') do set vartime=%%a
set body=-body "Test|Hello World"
set from=i@example.com
set to=example@example.com
set subj=-s "Test. FTP.%var%. New Files. %vartime%"
set server=-server smtp.example.com
set user=-u userexample
set pass= -pw passwordexample
C:\Bats\Blat\full\blat %0 -f %from% -b %to% %subj% %body% %server% %user% %pass%
Thanks in advance.

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Batch to get file name

#2 Post by aGerman » 09 Aug 2010 11:50

I have no idea about this stuff. You wrote "Directory Monitor" is watching the folder for new files. OK I don't know this software. What is the interface that you could use between your batch process and the Directory Monitor process?

Regards
aGerman

niobe
Posts: 6
Joined: 09 Aug 2010 01:42

Re: Batch to get file name

#3 Post by niobe » 09 Aug 2010 13:11

Thanks for your reply aGerman.

No i didn't wrote "Directory Monitor". I just a normal user. I found this software in the web. This software has a option that when new file arrive to monitored folder you can launch a program in this case a ".bat".

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Batch to get file name

#4 Post by aGerman » 09 Aug 2010 14:43

I'm sure batch is not the best for such things, but you could play with that.

First you should connect a volume (Z: for my example) with the server share where the files should be searched.

Code: Select all

@echo off &setlocal enabledelayedexpansion

for /f "tokens=1* delims=:" %%a in ('dir /a-d /b Z:\^|findstr /n .') do set /a n=%%a &set "old%%a=%%b"

:loop
for /f "delims=" %%a in ('dir /a-d /b Z:\') do (
  set "newName=%%a"
  call :procFileName
)
goto :loop

:procFileName
set "found=0"
for /l %%a in (1,1,%n%) do (
  set "oldName=!old%%a!"
  if "!oldName!"=="!newName!" set "found=1"
)
if %found%==1 goto:eof
set /a n+=1
set "old!n!=!newName!"
call :message
goto :eof

:message
:: *** your blat stuff here, file name is in !newName!
echo !newName!

goto :eof



In subroutine :message you can do your blat stuff.

Regards
aGerman

niobe
Posts: 6
Joined: 09 Aug 2010 01:42

Re: Batch to get file name

#5 Post by niobe » 09 Aug 2010 15:36

Ufff!!!

Just incredible!!

I see the code and i can't understand nothing but i try now and tell.

Thanks for your help aGerman.

The world make sense with people like you.

Thanks.

niobe
Posts: 6
Joined: 09 Aug 2010 01:42

Re: Batch to get file name

#6 Post by niobe » 09 Aug 2010 15:52

I try you code and open a new command window black and nothing happens.

I think that the code come in to a loop.

I have Z mapped to the folders where i need search new files and i try with the bat in this location and the desktop but the result is the same black window.

Thanks again for your help aGerman.

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Batch to get file name

#7 Post by aGerman » 09 Aug 2010 16:06

Of course you will see a black window. :wink:
First it saves all found files to variables.
After that it loops again and again to compare each found file if it existed before.
Only if a new file is found it will echo the file name.
Note, batch is very slow. If you have hundreds of files into the folder it takes a long time before the new file is found.

Regards
aGerman

niobe
Posts: 6
Joined: 09 Aug 2010 01:42

Re: Batch to get file name

#8 Post by niobe » 09 Aug 2010 16:23

OK.

Now I understand all.

I'll try with a few folders and tell you feedback.

Thanks one more time aGerman.

niobe
Posts: 6
Joined: 09 Aug 2010 01:42

Re: Batch to get file name

#9 Post by niobe » 09 Aug 2010 16:28

Its seems that works properly.

In 4 hours try again a tell.

Im so tired and need sleep al least 4 hours.

Have a nice dreams aGerman.

Post Reply