Computing.Net > Forums > Programming > Inserting File name into mult. 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.

Inserting File name into mult. file

Reply to Message Icon

Name: wow
Date: October 23, 2008 at 05:13:47 Pacific
OS: Xp
CPU/Ram: 2.66 GHz, 4 GB RAM
Product: Dell
Comment:

I need to go thru a folder and insert the first 10 letters of the text file name into the first line of those individual files. Im not experienced at programing but know how to execute the bat file from send-to.An example file would be named 0321456875_dirsvy.txt. I need to take the 1st 10 numbers and post them into the 1st line of that text file. I have a folder of about 10K I need to do this to.
Thanks for any help!



Sponsored Link
Ads by Google

Response Number 1
Name: pball
Date: October 23, 2008 at 06:09:50 Pacific
Reply:

Here is a batch script that can do that
----------------
@echo off
setlocal enabledelayedexpansion

FOR /f %%A IN ('dir /b *.txt') DO (
set num=%%A
set temp=!num:~0,-4!.temp
echo !num:~0,10! >> !temp!
for /f "tokens=*" %%a in (%%A) do (
echo %%a >> !temp!
)
del "!num!"
ren "!temp!" "!num!"
)
pause
----------------

This will write a temp file with the first 10 characters of the file name in the first line, then copy the rest of the file over, delete the original and rename the temp file.

I'd test it with a few sample files first.


0

Response Number 2
Name: Razor2.3
Date: October 23, 2008 at 06:44:47 Pacific
Reply:

FOR /f %%A IN ('dir /b *.txt') DO (
Suggestion:

FOR %%A IN (*.txt) DO

for /f "tokens=*" %%a in (%%A) do (
echo %%a >> !temp!
)

Suggestion:

echo %%a > #
copy # +%%A !temp!


0

Response Number 3
Name: klint
Date: October 23, 2008 at 07:29:06 Pacific
Reply:

echo %%a > #
copy # +%%A !temp!

Suggestion:
echo %%A > !temp!
copy /b !temp!+%%A

del "!num!"
ren "!temp!" "!num!"

Suggestion:
move /y !temp! !num!


0

Response Number 4
Name: pball
Date: October 23, 2008 at 07:32:13 Pacific
Reply:

Thanks for pointing some things out Razor.

I need to stop copying and pasting stuff and learn to write from scratch, that is why the first for loop has all that extra stuff in it.

Two questions for you though.
I don't get your second suggestion. Are you saying replace the echo or the whole for loop there and what does that part do and how does it work?

Also the way I have script now it removes blank lines, how can that be stopped.

New slightly better script
(old one also added space to end of each line)

@echo off
setlocal enabledelayedexpansion

FOR %%A IN (*.txt) DO (
set num=%%A
set temp=!num:~0,-4!.temp
echo !num:~0,10!>> !temp!
for /f "tokens=*" %%a in (%%A) do (
echo %%a>> !temp!
)
del "!num!"
ren "!temp!" "!num!"
)
pause


0

Response Number 5
Name: wow
Date: October 23, 2008 at 07:48:23 Pacific
Reply:

Thanks for all the comments all!


0

Related Posts

See More



Response Number 6
Name: wow
Date: October 23, 2008 at 08:03:56 Pacific
Reply:

Im new to all this so please forgive my dumb questions. So what I want to do is open notepad type in the code-

@echo off
setlocal enabledelayedexpansion

FOR %%A IN (*.txt) DO (
set num=%%A
set temp=!num:~0,-4!.temp
echo !num:~0,10!>> !temp!
for /f "tokens=*" %%a in (%%A) do (
echo %%a>> !temp!
)
del "!num!"
ren "!temp!" "!num!"
)
pause

-with th4e expension of .bat, then save in the sendto folder on my computer. Next I right click the folder and send to that .bat that was made right?
does it create all the new temps in the same file? then I rename them.


0

Response Number 7
Name: wow
Date: October 23, 2008 at 09:07:56 Pacific
Reply:

ok I got it working however in each .txt file there are thousands of data points so for one file it takes a minute or two (and there are 12,000 of these files in one folder)
This way it would take days.

Is there any other way to do this?


0

Response Number 8
Name: Razor2.3
Date: October 23, 2008 at 09:19:47 Pacific
Reply:

klint: copy /b !temp!+%%A
I don't think that's in every version of COPY, however. (At the very least, I distinctly remember being annoyed that I couldn't do that at some point in the distant past.) But it's a moot point because it does work in WinXP. Good call.

pball: I don't get your second suggestion. Are you saying replace the echo or the whole for loop there and what does that part do and how does it work?
Yes, replace the entire inner FOR loop. It uses copy to combine both files. Much (much) faster than using a FOR loop.

Also the way I have script now it removes blank lines, how can that be stopped.
Use the "COPY +" command.

wow: Is there any other way to do this?
Be patent; we're discussing that now.


0

Response Number 9
Name: klint
Date: October 23, 2008 at 09:54:29 Pacific
Reply:

For what it's worth, and apologies for the slight digression, I've just added a HowTo in answer to pball's question on how to avoid skipping blank lines when using FOR/F.

http://www.computing.net/howtos/sho...

However, I don't recommend using a FOR loop in the application we're discussing here. Use the command

COPY /B tempfile+existingfile

to append the contents of existingfile to tempfile, and then overwrite the existing file with the temp file. As Razor says, it's much faster.


0

Response Number 10
Name: wow
Date: October 23, 2008 at 10:17:56 Pacific
Reply:

Sorry for giving the impression Im in a hurry, Im not.
So with all the sugguestions how would the best fastest code look like?
Thanks to everyone for you help and input. This will make my job ALOT easier.


0

Response Number 11
Name: pball
Date: October 23, 2008 at 10:41:07 Pacific
Reply:

Thank you klint and Razor for the suggestions and hints and tips.

I kinda though about the for loop taking a while but didn't know another way.

so here is the latest script

@echo off
setlocal enabledelayedexpansion

FOR %%A IN (*.txt) DO (
set num=%%A
set temp=!num:~0,-4!.temp
echo !num:~0,10!>> !temp!
copy /b !temp!+%%A
move /y !temp! !num!
)
pause

wow: this should be much faster but with thousands of data points and 12,000 files it will still take a while.


0

Response Number 12
Name: Razor2.3
Date: October 23, 2008 at 10:49:08 Pacific
Reply:

echo !num:~0,10!>> !temp!

should read:
echo !num:~0,10!> !temp!

EDIT:
And while we're at it, let's do away with !temp! (and its logic) altogether, and replace it with a simple #.

echo !num:~0,10!> #


0

Response Number 13
Name: klint
Date: October 23, 2008 at 11:15:52 Pacific
Reply:

You also forgot that the batch file is required to be used in a SentTo folder. So it needs to work on a specified folder. Here's the latest full version:

@echo off
setlocal enabledelayedexpansion
cd %1

FOR %%A IN (*.txt) DO (
set num=%%A
echo !num:~0,10!> #
copy /b #+%%A
move /y # !num!
)
pause


0

Response Number 14
Name: pball
Date: October 23, 2008 at 11:28:47 Pacific
Reply:

so what is the #?

(i like long, interesting, and helpful threads even when i'm not the one with the question)


0

Response Number 15
Name: Mechanix2Go
Date: October 23, 2008 at 11:37:09 Pacific
Reply:

@echo off
setLocal EnableDelayedExpansion

for /f "tokens=* delims= " %%a in ('dir/b *.txt') do (
set f=%%a
set f=!f:~0,10!
echo !f! > #
type %%a >> #
copy # %%a > nul
)
del #


=====================================
If at first you don't succeed, you're about average.

M2


0

Response Number 16
Name: pball
Date: October 23, 2008 at 12:20:41 Pacific
Reply:

man i'm getting confused by the myriad of versions you two are thinking up


0

Response Number 17
Name: Razor2.3
Date: October 23, 2008 at 14:07:22 Pacific
Reply:

pball: man i'm getting confused by the myriad of versions you two are thinking up

setlocal enabledelayedexpansion
cd %1

FOR %%A IN (*.txt) DO (
set A=%%A
echo !A:~0,10!> A
copy /b A+%%A
move /y A %%A
)


Confused yet? ;)

Actually, the '#' we're apparently in love with is just a (hopefully) very unique file name. We could have chosen "tempFile.tmp", but '#' is easier to type.

klint: You also forgot that the batch file is required to be used in a SentTo folder.
I thought that comment in the original post was a qualification of the original poster's scripting proficiency, not a requirement of the script. But your version works in either case.


0

Response Number 18
Name: klint
Date: October 23, 2008 at 14:22:24 Pacific
Reply:

Ok just to answer pball's questions, "#" is just a temporary file name, and M2's version is very similar to the one the rest of us have collaboratively produced. Which one you choose is a matter of personal preference, But I would say that M2's copy command will be a little more time-consuming than the move command.


0

Response Number 19
Name: pball
Date: October 24, 2008 at 04:30:11 Pacific
Reply:

ok thanks guys, I hope wow gets something outta this thread cause I know I have.


0

Response Number 20
Name: wow
Date: October 28, 2008 at 04:17:14 Pacific
Reply:

I've learned alot. Thanks to everyone for your help!


0

Response Number 21
Name: wow
Date: October 28, 2008 at 04:25:34 Pacific
Reply:

When I run
@echo off
setLocal EnableDelayedExpansion

for /f "tokens=* delims= " %%a in ('dir/b *.txt') do (
set f=%%a
set f=!f:~0,10!
echo !f! > #
type %%a >> #
copy # %%a > nul
)
del #
when I run this Version I get an "unknown error while accessing file 'H:\....\locals~1\temp\_Getnames.txt"
Im doing this at work and dont have admin rights....can this be the problem?
p.s. doesnt have to be executable by send-to thats just how I've done it before.



0

Response Number 22
Name: wow
Date: October 28, 2008 at 04:29:02 Pacific
Reply:

When I run
@echo off
setLocal EnableDelayedExpansion

for /f "tokens=* delims= " %%a in ('dir/b *.txt') do (
set f=%%a
set f=!f:~0,10!
echo !f! > #
type %%a >> #
copy # %%a > nul
)
del #
when I run this Version I get an "unknown error while accessing file 'H:\....\locals~1\temp\_Getnames.txt"
Im doing this at work and dont have admin rights....can this be the problem?
p.s. doesnt have to be executable by send-to thats just how I've done it before.Also yes the file names are unique per each file.



0

Response Number 23
Name: wow
Date: October 28, 2008 at 04:34:46 Pacific
Reply:

Ok I have
@echo off
setlocal enabledelayedexpansion
cd %1
FOR %%A IN (*.txt) DO (
set A=%%A
echo !A:~0,10!> A
copy /b A+%%A
move /y A %%A
)
working. thanks!
now another addon: what if I wanted to add this to say the 15th line? also what if I need to add a string that gives the number and adds "API Serial Number"?
Thanks again


0

Response Number 24
Name: pball
Date: October 28, 2008 at 09:05:35 Pacific
Reply:

Well I would guess you'd have to switch back to my original idea of copying it line by line to get it added to the 15th line.

I'm just gonna sit this one out and see what the masters can do for you.


0

Response Number 25
Name: Mechanix2Go
Date: October 29, 2008 at 03:57:45 Pacific
Reply:

"I'm just gonna sit this one out"

LOL

=========================================
@echo off > newfile
setLocal EnableDelayedExpansion

for /f "tokens=* delims= " %%a in (myfile) do (
set /a N+=1
if !N! equ 17 echo my string >> newfile
echo %%a >> newfile
)


=====================================
If at first you don't succeed, you're about average.

M2


0

Sponsored Link
Ads by Google
Reply to Message Icon

VB:write hyperlinks to ex... Remove Administrator: fro...



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: Inserting File name into mult. file

Make directories from file name and move file www.computing.net/answers/programming/make-directories-from-file-name-and-move-file/19988.html

VBS script to shorten file name www.computing.net/answers/programming/vbs-script-to-shorten-file-name/19993.html

batch file help www.computing.net/answers/programming/batch-file-help/14032.html