Computer Hope
Microsoft => Microsoft DOS => Topic started by: hudsucker on February 20, 2013, 02:33:39 AM
-
Hi All,
I have a folder with multiple txt files in csv format with name convention Something_something_something_XXXXXXXX. txt where XXXXXXXX is date.
Now I need to join those files in one and each row needs to have a the date XXXXXXXX from respective file added as column.
So far I was able to find way to add whole file name to the row. I have found how trim file name to keep only XXXXXXXX.txt
But I am not able either to remove the .txt nor to add the trimed name to rows. Below is the code for adding name file:
for %%A in (*.txt) do (
echo Processing file '%%A'
FOR /F "delims=" %%L in (%%A) do (
ECHO %%L , %%A >> New.txt
)
)
I have found this to trim the file name so it leaves last 11 chararacters so XXXXXXXX.txt
SET VAR=%%A
CALL ECHO/%%VAR:~-11%%&ECHO/
don't know how to use it in ECHO %%L , %%A >> New.txt
I have seen the %~nA supposedly should return only file name so I could get rid of txt but could not find syntax that would work.
This is probably easy but I am sooo... confused with examples I have seen that I can figure out how to join the dots. :-[
-
There are different ways to skin this cat. This splits the filename into tokens divided by _ and . and takes the 4th one, which is XXXXXXXX here.
Test it on copies of your files as it overwrites the original.
@echo off
for %%A in (*.txt) do (
for /f "tokens=4 delims=_." %%Z in ("%%A") do (
echo Processing file '%%A'
FOR /F "delims=" %%L in (%%A) do (
ECHO %%L,"%%Z" >> New.tmp
)
move /y new.tmp "%%A"
)
)
-
Hi foxidrive
Worked perfectly - actually the files were Something-something_something-something_XXXXXXXX. txt but with your explanation I was able to figure out this detail myself.
Thanks a million!!
-
Hi guys
I have came here through another forum in search of same topic. I found following code @echo off
for %%A in (*.txt) do (
echo Processing file '%%A'
FOR /F "delims=" %%L in (%%A) do (
ECHO %%L%%A >> DataFile.txt
)
)
and used it in my batch file. It works perfectly only when the batch file is run directly by clicking file name in any folder. Problem comes in the scene when I call this batch file in MS-Access. I used a button on form in MS-Access and used following code on Click event of the button.
strReportpath = "F:\meter_pictures\"
Shell strReportpath & "Combine.bat"
but it not working.
Is any one have an idea what mistake I'm doing?