get file name and write it in another file

February 4, 2010 at 08:23:54
Specs: Windows XP
Hi everybody,

Here is my problem:
I have a directory Dir1 in which are going to be saved files on daily basis.
These files will be named as follows: filename_dateSaved.dat (eg. myFile_20100203.dat --- for the file saved on February 03, 2010, and myFile_20100204.dat for the file saved on February 04, 2010)
Now, I want to build a final batch file (MainLoader), which should load the data contained in the files stored in Dir1 into a database, on daily basis. Except other tasks, MainLoader.bat takes as input a file (ControlFile.ctl) which contains a script that points to the latest file saved in Dir1 (filename_dateSaved.dat). The content of ControlFile.ctl is as follows:
load data
infile 'c:\path\ filename_dateSaved.dat
Now, I’m trying to create some transit batch-script, in order to get the name of the last saved file in Dir1, and assign it to the rest of infile 'c:\path\...lastSavedFile_Name.
Can you help me please?
Lots of thnxs
P.S.
I’m also looking about creating as many ControlFile.ctl failes, as saved filename_dateSaved.dat files are going to be stored in Dir1.


See More: get file name and write it in another file

Report •

#1
February 4, 2010 at 08:37:32
@echo off & setLocal EnableDELAYedeXpansion

pushd dir1

for /f "tokens=* delims= " %%a in ('dir/b/a-d/o-d') do (
> ControlFile.ctl echo 'c:\dir1\%%a'
goto :done
)
:done


=====================================
Helping others achieve escape felicity

M2


Report •

#2
February 4, 2010 at 23:19:17
Thnx four your time and asnwer Mechanix2Go,

The problem is that, the batch script will get the name of the last modified file, not of the last saved file.
So, there is the risk, that if none of this file is opened for modification, the batch will return everytime the same file_name. Am I right?

How can we get the property Created: of a file, and compare this, with the other files in the same directory?


Report •

#3
February 4, 2010 at 23:35:34
for /f "tokens=* delims= " %%a in ('dir/b/a-d/o-d/tc') do (


=====================================
Helping others achieve escape felicity

M2


Report •

Related Solutions

#4
February 5, 2010 at 01:48:23
Thnx a lot Mechanix2Go,

Can I ask your help for another step that I have to take?
After the creation of the ControlFile.ctl from the .batScript, I renamed it lastFileName.txt, and moved it to another directory (Dir2) [now we have the file lastFileName.txt in Dir2]

Now, the content of the lastFileName.txt, consists of a single line, (the file_Name of a file). As I wanted to make use of this file_Name in the content of another file, I used this .batScript to get the file_Name, ... but I do not know how to assign this value to a variable, and moreover, I cannot make use of this named variable in the rest of the .batScript.

Here is the .batScript,
@echo off
for /f "tokens=1 delims= " %%a in (Dir2\lastFileName.txt) do (
> Control.ctl echo load data infile %%a append into table TBL_NAME fields terminated by "," optionally enclosed by '"' ( Field1, Field2, Field3, Field4, Field5)
goto :move_file
)
:move_file
move Control.ctl Dir3
sqlldr user/pass@dbpath control=Dir3\Control.ctl log=Dir3\%%a.log data=Dir1\%%a.dat


Report •

#5
February 5, 2010 at 06:14:12
This resolved my problem:

@echo off
for /f "tokens=1 delims= " %%a in (Dir2\lastFileName.txt) do ( set var_fn=%%a
> Control.ctl echo load data infile %var_fn%

Thnx again for your sustain


Report •

Ask Question