Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
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!

Here is a batch script that can do that
----------------
@echo off
setlocal enabledelayedexpansionFOR /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.

FOR /f %%A IN ('dir /b *.txt') DO (
Suggestion:FOR %%A IN (*.txt) DOfor /f "tokens=*" %%a in (%%A) do (
echo %%a >> !temp!
)
Suggestion:echo %%a > #
copy # +%%A !temp!

echo %%a > #
copy # +%%A !temp!Suggestion:
echo %%A > !temp!
copy /b !temp!+%%Adel "!num!"
ren "!temp!" "!num!"Suggestion:
move /y !temp! !num!

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 enabledelayedexpansionFOR %%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

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 enabledelayedexpansionFOR %%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.

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?

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.

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.

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.

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 enabledelayedexpansionFOR %%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!
)
pausewow: this should be much faster but with thousands of data points and 12,000 files it will still take a while.

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!> #

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 %1FOR %%A IN (*.txt) DO (
set num=%%A
echo !num:~0,10!> #
copy /b #+%%A
move /y # !num!
)
pause

so what is the #?
(i like long, interesting, and helpful threads even when i'm not the one with the question)

@echo off
setLocal EnableDelayedExpansionfor /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

pball: man i'm getting confused by the myriad of versions you two are thinking up
setlocal enabledelayedexpansion
cd %1FOR %%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.

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.

When I run
@echo off
setLocal EnableDelayedExpansionfor /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.

When I run
@echo off
setLocal EnableDelayedExpansionfor /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.

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

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.

"I'm just gonna sit this one out"
LOL
=========================================
@echo off > newfile
setLocal EnableDelayedExpansionfor /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

![]() |
VB:write hyperlinks to ex...
|
Remove Administrator: fro...
|

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