I want my code to read every .txt file and add the first 6 numbers of the file name to the front of the line... I have the first part of the code set up and I know how the second part needs to look I just don`t know how to merge them..
#echooff
del Build_count.csv
for /f %%a in ('dir /b *.txt') do for /f "tokens=*" %%b in (%%a) do echo %%a,%%b >> Build_count.csv
#echo off
setlocal enabledelayedexpansion
if exist List.cvs del List.cvs
for %%a in (*.txt) do (
set fn=%%~na
set fn=!fn:~0,6!
)
Just appending one code to the other doesn't help you. You need to insert the second snippet into the first loop:
#echo off
setlocal enabledelayedexpansion
(for /f %%a in ('dir /b *.txt') do (
set "fn=%%~na"
for /f "tokens=*" %%b in (%%a) do (
echo !fn:~0,6!,%%b
)
))>Build_count.csv
For every matching file extract the desired string, then in another loop read every line and write the string plus the line. Redirect the whole thing to the destination file in one go (that's much faster than opening the file, finding the end, adding a line and closing the file again for each and every line). Deleting it beforehand isn't necessary, because the code overwrites the file anyway.
Note: the for /f loop as it stands now ignores empty lines and lines starting with a ;. Adaptions are necessary if that's a problem.
I have a directory with a file in it (actually it has a lot of files, but I figured getting one to work was the first step before building the loop to hit each file), that needs to be edited and saved with a similar filename. Instead of manually typing in the filename, I'd like to use a variable containing the filename.
Sample Input Data
FileX.txt
Text line
Text line
Text line
Desired Output Data
FileX2.txt
1 Text line
2 Text line
3 text line
I can manage this with one file at a time, I'm struggling to write one script to look through the contents of a folder and do this to each text file within the folder.
I'm on windows 7 and this is what I have so far:
#echo off
setLocal EnableDelayedExpansion
Set N=0
REM THE BELOW LINE IS THE VARIABLE I'M TRYING TO SET AND THEN HAVE PASSED AS THE FOR LOOP PARAMETER
Set F="FILENAME"
REM IF I SKIP A VARIABLE AND HARDCODE THE FILENAME IN THE BELOW LINE, IT WORKS FOR THE ONE FILE
for /f "tokens=* delims= " %%a in (FILENAMEVARIABLE.txt) do (
Set /a N=!N!+1
echo !N! %%a, >> !F!.txt
)
The opening statement of the for loop, is where I can't get the variable to take. In FILENAMEVARIABLE.txt I have tried %%F, %F, !F!, and %%~nxf none of which manage to call the correct file to start the loop. Any ideas what I'm doing wrong here?
Here's what I think you were trying to do:
#Echo Off
SetLocal EnableDelayedExpansion
For %%A In (*.txt) Do (Set "N=0"
(For /F "UseBackQ Tokens=*" %%B In ("%%A") Do (Set /A N +=1
Echo !N! %%B))>"%%~nA2%%~xA")
You could also use FindStr:
#Echo Off
For /F "Tokens=1-2* Delims=:" %%A In ('FindStr /N "^" *.txt 2^Nul'
) Do >>"%%~nA2%%~xA" Echo %%B %%C
In both of the above examples I have assumed that the source directory and script directory are the same.
folks!
I'm not sure if this is a very simple task or a very complicated one, but either way I'm struggling with it. Suppose I have a text file like this:
11111FOO
11111BAR
22222ZOOM
33333FOO
11111CAR
I want to figure out a command line in windows that I can plop into a batch file that will pull out text strings from this file and push them to a new file. I would pass in the leading string to search for, and it would take everything from the end of that string to the next new line.
So using the above example, if I said the leading string was 11111, I would get a new text file that looked like this:
FOO
BAR
CAR
Everything else would be ignored.
Thanks!
If there are no potential poison characters in the input file then perhaps this would suffice:
#(For /F "UseBackQ Delims=" %%A In ("input.txt") Do #(Set "an=%%A"
For /L %%B In (%%A 1 %%A) Do #Call Echo %%an:*%%B=%%))>"output.txt"
Magoo's additions:
#Echo Off
(For /F "UseBackQ Delims=" %%A In ("q46858215.txt") Do (Set "an=%%A"
For /L %%B In (%%A 1 %%A) Do Call Echo %%an:*%%B=%%))>"Output1.txt"
If "%~1"=="" GoTo :Next
(For /F "Delims=" %%A In ('FindStr/BLC:"%~1" "q46858215.txt"') Do (
Set "an=%%A"
For /L %%B In (%%A 1 %%A) Do Call Echo %%an:*%%B=%%))>"Output2.txt"
:Next
(For /F "UseBackQ Delims=" %%A In ("q46858215.txt") Do (Set "an=%%A"
For /L %%B In (%%A 1 %%A) Do Echo %%B))>"Output3.txt"
I used a file named q46858215.txt containing OP's data for my testing.
Produces three output files:
Output1.txt : Compo's original
Output2.txt : filtered to isolate lines beginning with (first parameter to routine)
Output3.txt : Compo's original in reverse, showing the numbers isolated
I'm trying to make a .bat file which iterates through every file in the directory which the .bat file is in, and renames all the files which follow the naming convention of MM_DD_YY*.* to YY_MM_DD*.*.
For example, 05.20.16 MyFile.txt would become 16.05.20 MyFile.txt.
I've managed to iterate through the filenames, but I'm having trouble with making a substring of the beginning 8 characters and comparing them to the format of ##.##.##.
#echo off
setlocal EnableDelayedExpression
for /f %%i in ('dir /b') do (
set fn=%%i
:: Need to compare first 8 chars to ##.##.##
if %fn:~0,8%==??.??.?? echo FollowsFormat
)
Any help is appreciated!
Provided:
the date in the file name is always followed by a space.
You can use delimiters . and spaceto seperate date elements and remaining file name into for variables %%A - %%D and rearrange them in the desired order.
To better distinguish old from new format I suggest to change the delimiter (at least temporarily) to a - and extend the year to 4 digits.
#echo off & setlocal EnableDelayedExpression
for /f "tokens=1-3* delims=. " %%A in (
'dir /b /A-D "??.??.?? *.txt" ^| findstr "^[01][0-9]\.[0-3][0-9]\.[0-9][0-9].*"'
) do Echo Ren "%%A.%%B.%%C %%D" "20%%C-%%A-%%B %%D"
For security reason the batch only echoes what it would rename
until you remove the echo in front of ren if the output looks OK.
I am looking to create a .cmd file which filters all the .csv files in a folder and then creates a set of .csv files with the results.
Here is the scenario.
There are many .csv files in a folder
The first column of data in the csv file contains a code HH??????-XX,
where ?????? could be any set of numbers and XX ranges from 01 to 48
I would like the batch file to start XX at 01 and where HH??????-01 is found it takes the row and puts the results in an output file called -01.csv
The code HH at the beginning could be different so it would have to also filter for HH
This is then repeated for each of the csv files in the folder.
The whole process is then repeated for HH??????-02 and outputs the file to a csv called -02.csv and so on up upto -48.csv
I dont have very much - i am not an expert in this field at all, only in VBA
I obtained this template from the web which i thought i could improve on but i don't really understand how i can adapt this. I am not familiar with FOR loops
#echo off
del c:\Total.csv
Del c:\Final.csv
Type c:\BATTEST\*.CSV > c:\Total.csv
FOR /F "eol=- delims=~ tokens=1-5,6,7*" %%1 IN ( c:\Total.CSV ) DO IF %%4 EQ 456 ECHO %%1 %%2 %%3 %%4 %%5 %%6 %%7 %%8 >> c:\Final.csv
Example rows in the .csv files are: (remembering that HH could be something different)
HH777777-01,A,500,200,10,11,12,13,15.2,17.9,26,B
HH236748-02,C,300,100,11,14,17,19,21.2,18.9,14,B
Each file has a header which is the same throughout and it would be good to have this header in the final output file.
The header is
Code,type,head,file,make,run,style,line,edge,model,letter,status
Edit: Added a target folder and a cd command for a source folder.
This works here - test it on some sample files.
#echo off
setlocal enabledelayedexpansion
set "target=d:\target\folder"
cd /d "c:\source\folder"
for /L %%a in (101,1,148) do (
set num=%%a
del "%target%\-!num:~-2!.csv" 2>nul
>"%target%\-!num:~-2!.csv.txt" echo Code,type,head,file,make,run,style,line,edge,model,letter,status
)
for %%a in (*.csv) do (
for /f "skip=1 usebackq delims=" %%b in ("%%a") do (
for /f "tokens=1,2 delims=-," %%c in ("%%b") do (
set "line=%%c"
if /i "!line:~0,2!"=="HH" >> "%target%\-%%d.csv.txt" echo %%b
)
)
)
ren "%target%\*.csv.txt" *.
pause
#ECHO OFF
SETLOCAL
FOR /L %%a IN (101,1,148) DO CALL :header %%a
FOR %%f IN (q21956251*.csv) DO (
FOR /f "usebackqskip=1delims=" %%a IN ("%%f") DO (
FOR /f "tokens=2delims=-," %%d IN ("%%a") DO (
FOR /L %%h IN (101,1,148) DO IF "1%%d"=="%%h" >>"-%%d.new" ECHO(%%a
)
)
)
FOR /L %%a IN (101,1,148) DO CALL :rennew %%a
GOTO :EOF
:header
SET "destfile=%1"
>"-%destfile:~-2%.new" ECHO(Code,type,head,file,make,run,style,line,edge,model,letter,status
DEL "-%destfile:~-2%.csv" 2>NUL
GOTO :eof
:rennew
SET "destfile=%1"
REN "-%destfile:~-2%.new" "-%destfile:~-2%.csv"
GOTO :eof
You don't explain what you mean by The code HH at the beginning could be different so it would have to also filter for HH - without an explicit specification, we start a guessing game.
This routine will first create a new set of files called -nn.new for n=01 to 48, write the header line to each and then delete any existing -nn.csv
The next step is to select all the .csv files - I selected q21956251*.csv for my testing. Each filename is assigned to %%f in turn.
Then %%a receives each line from the file %%f. The "usebackqis required because%%fis quoted (in case of spaces in filenames). Theskip=1means skip the first line (because it's a header) and thedelims=meansassign the entire line`.
The next for looks at the line in %%a and selects the second (tokens=2) group of characters where - and , are both delimiters. This should select the 01 and '02' from your data. The numbers "101" to "148" are then compared with the concatenation of 1 with the number retrieved into %%d. When there's a match, the line %%a that was read from the file will be appended to -%%d.new
When all of the .csv files have been processed, the :rennew routine renames all of the -nn.new to -nn.csv.
It would be possible to process this sequence without the rename cycle. That's just the way I used.
Filtering CSVs with various conditions can be done with EasyMorph (it has a free version). You might need to use parameters in EasyMorph to pass the XX code into a project.