Congratulations to AcaCandy on her 100,000th post!
There's no such thing as a stupid question, but they're the easiest to answer.
JoinTour
Login
 
Tag Cloud
acer black screen blue screen boot bsod computer connection crash css dell display driver drivers email error ethernet excel firefox firefox 3 hard drive internet internet explorer itunes laptop linux malware monitor network networking nvidia outlook outlook 2003 outlook 2007 outlook express partition problem router security slow software sound startup trojan usb video virus vista windows windows xp wireless
DOS/PDA/Other
Search
Search in:
 
Advanced Search
Tech Support Guy Forums > Operating Systems > DOS/PDA/Other >
Append to end of line


HELLO AND WELCOME! Before you can post your question, you'll have to register -- it's completely free! Click here to join today! We highly recommend that you print a copy of our Guide for New Members. Enjoy!

Closed Thread
 
Thread Tools
TheOutcaste's Avatar
Computer Specs
Senior Member with 1,852 posts.
 
Join Date: Aug 2007
Location: Oregon, USA
Experience: Intermediate
19-May-2008, 01:59 PM #46
Would be nice if it was that easy, but it only works with For variables or the batch parameters %0-%9. A For statement is probably easiest, but you can also use call:

Code:
set _filename=c:\test1\file.txt
call :_getfnext %_filename%
call :_getpath %_filename%
echo.Filename and extension is %_fn%
echo.Path to %_fn% is %_pn%
set var=C:\ABC\DEF\something.pdf
call :_getfnext %var%
echo.Filename and extension is now %_fn%
goto:eof
:_getfnext
set _fn=%~nx1
goto:eof
:_getpath
set _pn=%~dp1
goto:EOF
A little bug is you don't need the surrounding % symbols to get the path name; call :_getpn _filename works the same as call :_getpn %_filename%

I haven't tried all the modifiers to see which ones will work that way, but best use the % symbols properly in case other versions of cmd don't have that bug.

EDIT: Just to avoid confusion I've highlighted the batch variable number in red -- those are the number one, not a lower case "L"
__________________
Of course I know all the answers ; I just don't always match the answers to the right questions

Warning -- Windows spoken here. (Rated R for Strong Language and Violence -- When your Windows PC flies through a window, that's violent, right?)
Registry Cleaners are known to cause the above...

Last edited by TheOutcaste : 19-May-2008 03:40 PM. Reason: Clarification
devil_himself's Avatar
Distinguished Member with 4,844 posts.
 
Join Date: Apr 2007
Location: India
Experience: Advanced
19-May-2008, 02:01 PM #47
you can do it this way

Code:
@echo off
set var=%1
set new_var=%~dp1
echo %new_var%
edit - i'm a slow typer
Squashman's Avatar
Distinguished Member with 12,688 posts.
 
Join Date: Apr 2003
Location: 1265 Lombardi Ave
19-May-2008, 02:07 PM #48
You both sure are schooling me on batch files. Thanks for all the help.
Squashman's Avatar
Distinguished Member with 12,688 posts.
 
Join Date: Apr 2003
Location: 1265 Lombardi Ave
23-May-2008, 10:29 PM #49
Quote:
Originally Posted by devil_himself View Post
if you want the output to be like this

Code:
"Name"         :"Address"      :"City"         :"State"        :"ZipCode"      :"Filename"      
"John Doe"     :"503 Grand Ave":"San Jose"     :"CA"           :"93456"        :"file1.txt"     
"Jim Andersen" :"512 Grand Ave":"San Diego"    :"WA"           :"93456"        :"file2.txt"
then this should do it
Code:
@echo off & setlocal enableextensions enabledelayedexpansion
  for /f "tokens=1-6 delims=:" %%a in ('type Myfile.txt') do (
   set l1=%%a               .
   set l2=%%b               .
   set l3=%%c               .
   set l4=%%d               .
   set l5=%%e               .
   set l6=%%f               .
   echo !l1:~0,15!:!l2:~0,15!:!l3:~0,15!:!l4:~0,15!:!l5:~0,15!:!l6:~0,15! >>newfile.txt
   )
  endlocal & goto :EOF
As I was saying earlier, I think I could use this example with a different order I work on. Again, I want to append the filename to the end of each line but this file is already in a fixed format. The filenames are anywhere from 5 to 8 positions long. But I want to maintain the same line length for all records. So I am think I could integrate your example above with my current batch file. I am thinking I would just expand it out to 10 positions. Just add the appropriate amount of blanks to the end. I kind of see that your set statement adds 16 positions to the variable. Then I assume that echo statement outputs the first 15 positions of the variable. I am thinking I might be able to put that into my SED statement to do the replace. Not sure. Will have to test.
__________________
I hate asking the same question twice!
How to ask questions the smart way!
Microsoft MVP - Windows Shell/User

Last edited by Squashman : 23-May-2008 10:42 PM.
Squashman's Avatar
Distinguished Member with 12,688 posts.
 
Join Date: Apr 2003
Location: 1265 Lombardi Ave
03-Jun-2008, 01:18 AM #50
found this neat little utility called Swiss File Knife.

Has a nice option called addtail. Works like a charm.

Code:
FOR /F "tokens=1* delims=." %%A IN ('dir /b /a-d *.TXT') DO echo "Working on file: %%A" && type "%%A.TXT" | sfk addtail %%A >> FILES_COMBINED.txt




.
Squashman's Avatar
Distinguished Member with 12,688 posts.
 
Join Date: Apr 2003
Location: 1265 Lombardi Ave
03-Jun-2008, 02:49 AM #51
Not quite sure why this isn't working the way I want it to. It doesn't seem to add the spaces to the end of the filename. It only adds the filename. My filenames are 6 to 8 characters long but I want to make the filenames 10 characters so I add some spaces. The reason I am doing that is so the line lengths are the same for all lines on the output.

Code:
:: Append the Filename to the end of each line of every file and combine all files together
FOR /F "tokens=1* delims=." %%A IN ('dir /b /a-d *.TXT') DO (
     set _Tfile=%%A               .
     set _Filename=!_Tfile:~0,10!
     echo "Working on file: %%A"
     type "%%A.TXT"|sfk addtail !_Filename! >> FILES_COMBINED.tmp
     )
__________________
I hate asking the same question twice!
How to ask questions the smart way!
Microsoft MVP - Windows Shell/User
Squashman's Avatar
Distinguished Member with 12,688 posts.
 
Join Date: Apr 2003
Location: 1265 Lombardi Ave
03-Jun-2008, 02:53 AM #52
Ignore the stupid person posting. Why do I always forget quotes. Devil, Thanks alot for that tip on substrings.
TheOutcaste's Avatar
Computer Specs
Senior Member with 1,852 posts.
 
Join Date: Aug 2007
Location: Oregon, USA
Experience: Intermediate
03-Jun-2008, 04:49 AM #53
Just a suggestion:
With the FOR /F "tokens=1* delims=." you can't use names with a period. Example, my.file.txt will end up with _Filename being "my " (my followed by 8 spaces) and the type statement will be type my.txt which may not exist, and the File not found error may go unnoticed. Worse, it may actually exist, so it will get processed twice while my.file.txt gets skipped. You may not be using filenames with that format now, but who knows what someone will try 6 months from now.

The following will get the complete filename into the %%A variable, then uses %%~nA to specify the name portion only, so names with a period will be preserved. I haven't had a chance to download and play with sfk, so I'm guessing the missing quotes should be around !_Filename!.
Looks like a very useful tool. Thanks for finding that!


Code:
:: Append the Filename to the end of each line of every file and combine all files together
FOR /F "tokens=*" %%A IN ('dir /b /a-d *.TXT') DO (
     set _Tfile=%%~nA               .
     set _Filename=!_Tfile:~0,10!
     echo "Working on file: %%~nA"
     type "%%A"|sfk addtail "!_Filename!" >> FILES_COMBINED.tmp
     )
HTH

Jerry
__________________
Of course I know all the answers ; I just don't always match the answers to the right questions

Warning -- Windows spoken here. (Rated R for Strong Language and Violence -- When your Windows PC flies through a window, that's violent, right?)
Registry Cleaners are known to cause the above...
Squashman's Avatar
Distinguished Member with 12,688 posts.
 
Join Date: Apr 2003
Location: 1265 Lombardi Ave
03-Jun-2008, 11:03 AM #54
I like how you think. That should make it just about foolproof.

Still wondering if I could do my file counts differently. Would be nice if I could count the records as I am processing the files, instead of counting them before and after. But I think if I do that I will start running into memory issues like we did with the profanity batch. I just do that to verify my input and output file counts.

Wasn't sure if I could put something in there that would increment a counter for each filename as it is processed. I assume this wouldn't work because I am echoing(typing) the file to another command and then redirecting output. I assume I couldn't put a counter in there because it would increment the counter until it was done typing the file. I thought maybe I could use the filename variable as part of the counter variable and then echo that variable to my Log file at the end of the batch in another loop. This would only work I assume if the loop was parsing one line of the file at a time. Instead the loop is controlled by the filename being processed.
set /a !_Filename!_count+=1

I suppose I could create another For Loop inside the first For Loop that types the file but again if it tries to load the entire file into memory, I think that it will choke on memory again.

Code:
FOR /F "tokens=*" %%A IN ('dir /b /a-d *.TXT') DO (
     set _Tfile=%%~nA               .
     set _Filename=!_Tfile:~0,10!
     echo "Working on file: %%~nA"
     FOR /F %%B IN ('type %%A') DO (
     set /a !_Filename!_count+=1
     echo.%%B|sfk addtail "!_Filename!" >> FILES_COMBINED.tmp
     )
     echo.!_Filename!_count >>Appendlog.log
     )
Not even sure if that will work. I am at home right now. Would have to test it on a large file later on went I get to work tonight.
__________________
I hate asking the same question twice!
How to ask questions the smart way!
Microsoft MVP - Windows Shell/User
TheOutcaste's Avatar
Computer Specs
Senior Member with 1,852 posts.
 
Join Date: Aug 2007
Location: Oregon, USA
Experience: Intermediate
03-Jun-2008, 05:42 PM #55
There ya go forgetting those quotes again. Also, need to specify the "tokens=*" or remove delimiters with "delimns=", or any line containing a space or tab will be truncated.

FOR /F "tokens=*" %%B IN ('type "%%A"') DO (

Not sure about the memory issue. Testing will tell. The extra FOR loop will likely slow it down quite a bit though.

Find can be used to count lines, but its not real fast. Didn't time it counting my file with 28,799,994 records (about 80 bytes each), but seems like about 10-20 minutes. Had two counts running, and the PC was recording TV at the same time though. I just typed the commands; should have run a batch and logged the times.
Find was faster than findstr though, and didn't hang like findstr did when it finished.
You might try starting a separate batch file to do the count, and let it run in another process. Haven't tested this, might have to use start so that it doesn't wait for it to finish, but that would open another command prompt window.

Code:
 FOR /F "tokens=*" %%A IN ('dir /b /a-d *.TXT') DO (cmd /c count.bat %%A  
     set _Tfile=%%~nA               .
     set _Filename=!_Tfile:~0,10!
     echo "Working on file: %%~nA"
     FOR /F %%B IN ('type %%A') DO (
     set /a !_Filename!_count+=1
     echo.%%B|sfk addtail "!_Filename!" >> FILES_COMBINED.tmp
     )
     echo.!_Filename!_count >>Appendlog.log
     )
count.bat would contain:
Code:
>>appendlog.log find /v /c "@@this#won't#be#found@@" %1
Or whatever method you are using to count records. Find outputs the file name, if your counting some other way just add
Code:
>>appendlog.log echo.File %1 count is:
to the start of count.bat

Jerry
__________________
Of course I know all the answers ; I just don't always match the answers to the right questions

Warning -- Windows spoken here. (Rated R for Strong Language and Violence -- When your Windows PC flies through a window, that's violent, right?)
Registry Cleaners are known to cause the above...
Closed Thread

THIS THREAD HAS EXPIRED.
Are you having the same problem? We have volunteers ready to answer your question, but first you'll have to join for free. Need help getting started? Check out our Welcome Guide.


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
WELCOME TO TECH SUPPORT GUY! Are you looking for the solution to your computer problem? Join our site today to ask your question -- for free! Our site is run completely by volunteers who help people like you solve computer problems. See our Welcome Guide to get started.



Thread Tools


You Are Using:
Server ID
Advertisements do not imply our endorsement of that product or service.
All times are GMT -4. The time now is 01:05 PM.
Copyright © 1996 - 2008 TechGuy, Inc. All rights reserved.
Powered by vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Powered by Cermak Technologies, Inc.