Computing.Net > Forums > Programming > Read and Write in Batch file

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Click here to start participating now! Also, check out the New User Guide.

Read and Write in Batch file

Reply to Message Icon

Name: kaps
Date: July 3, 2008 at 09:48:18 Pacific
OS: Win XP
CPU/Ram: 1 GB
Product: Dell
Comment:

Hi

I am rookie ot batch.
I have a requirememnt where, I have to read from a text file which looks like this

a,test.txt
s,Files.txt
a,ForCommand.txt

a is for application file and s is for system file

I need to find all the attributes,creation date,modified date of these files and
write these details into a new file with name of the file.

So I achieve so far is , I am able to get some of the parameters of the files .

1)
My loop is running one time extra which returens blank value for last time.

2) I need to get parameters like creation date, last accessed and owner of the file ?

I am pasting my code below

set varText=complist.txt
set varText=%varText%

for /f "tokens=1-2 delims=," %%T in (%varText%) do (

if %%T equ a Call :app %%U
if %%T equ s Call :sys %%U
)

:app

echo " I am in App"

echo FileName = %~nx1
echo FileSize = %~z1
echo TimeStamp = %~t1
echo FullPathofFile = %~f1
echo ShortPathFile = %~s1
echo Attribute = %~a1

goto end

:sys

echo " I am in sys"
echo FileName = %~nx1
echo FileSize = %~z1
echo TimeStamp = %~t1

goto end

:end

:: Done

kaps




Response Number 1
Name: klint
Date: July 3, 2008 at 11:10:38 Pacific
+1
Reply:

1. It's not your loop running one extra time. It's because at the end of your loop you fall through to the :app section because you've forgotten to put "goto end".

By the way, you don't need an explicit end label: if you say goto :eof it will go to the end, as there was an :eof label at the end of your file.

Also, what's the purpose of this line:
set varText=%varText% ? It doesn't do anything.

2. You'll need to call an external utility for that (which you can then use with the for /f command). I'm not sure if there is one that comes with Windows. Try Googling.



Response Number 2
Name: kaps
Date: July 6, 2008 at 14:27:05 Pacific
+1
Reply:

Hi
Thanks klint for quick reply.

I have put gottoend before :app and it stopped and it's coming out of loop as required.

In vartext I am storing my file name.

Now please advise
1) This is working fine
echo FileName = %~nx1

But I want to store/set this into a variable filename as I need to use it later on with my external command.

echo FileName = %~nx1
echo FileSize = %~z1
echo TimeStamp = %~t1
echo FullPathofFile = %~f1
echo ShortPathFile = %~s1
echo Attribute = %~a1

How I can store these results in a variabls.

2)

Also, I found one utility from microsoft for checking checksum of a file

For /F "Tokens=1" %%I in ('FCIV %1%') Do Set checksum=%%I

I am using like above and storing value in checksum.

Thanks for all the help, Since I am writing a big batch file, I may ask some questions on this forum, I need to throw all the result in a file at some partiular location each time it ran. I am doing googling to read and find more before posting here.

thanks

rgds
kaps

kaps



Response Number 3
Name: klint
Date: July 6, 2008 at 14:45:41 Pacific
+1
Reply:

You can do this:

set FileName=%~nx1
set FileSize=%~z1

etc. Don't put spaces around the = sign, or else the variable name will include the trailing space.



Response Number 4
Name: kaps
Date: July 8, 2008 at 07:24:49 Pacific
+1
Reply:

Hi

I want to echo text in a file.
I am doing like this

echo Start Component >Kapstestnew.txt
echo Name = "KapstestnewReport" >>Kapstestnew.txt
echo Description = "Kapstestnew for Ross Stores" >>Kapstestnew.txt

I have a huge text to echo in a file.

Is it possible to echo the whole text line by line without writing echo in starting and/or file name >>Kapstestnew.txt at the end of each line.

Basically echo a chunk of data staright into file like

echo blah blah.....
blah blah
> file.txt

please let me know.

Rgds
kaps

kaps



Response Number 5
Name: klint
Date: July 8, 2008 at 08:10:56 Pacific
+1
Reply:

I don't think so. Well, not unless you use another language or utility - I think you can only do it line-by-line in native batch. But if anyone knows a way, I would be interested to know how to do it too.

For example, in many Unix shells you can do this:

cat <<end >file.txt
your text goes here
in multiple lines
end



Related Posts

See More



Reply to Message Icon

change file name (extensi... Move files over 850kb



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


Google Ads



Results for: Read and Write in Batch file

Progress bar in batch file www.computing.net/answers/programming/progress-bar-in-batch-file/14919.html

How to write in a batch file using c#? www.computing.net/answers/programming/how-to-write-in-a-batch-file-using-c/19065.html

vb binary file readin and writing www.computing.net/answers/programming/vb-binary-file-readin-and-writing/4232.html