How do I combine files in order - batch-file

I have/found a windows batch script that will combine csv files from all sub-directories. It works great in Windows 10 but when I run the script in Windows 7, all the files are out of order. How do I force the order in which to combine the csv files?
echo #off
for /r %%i in (*.csv) do (
if not %%~nxi == output.csv (
echo %%~nxi >> output.csv
echo %%i
echo %%~nxi
type "%%i" >> output.csv
echo. >> output.csv
echo. >> output.csv
)
)

Just a little change to your code will do the job:
#echo off
FOR /F "usebackq delims=" %%i IN (`dir /s /b /O:N *.csv`) do (
if not "%%~nxi" == "output.csv" (
echo %%~nxi >> output.csv
echo %%i
echo %%~nxi
type "%%i" >> output.csv
echo. >> output.csv
echo. >> output.csv
)
)
Point is to use another command's out -- use dir to control the sorting order.
/b for clean output by dir
/O for ordering, N -- name.
check FOR /? and dir /? for more details.
PS: you may wanna use:
echo %%~dpnxi >> output.csv
instead of line 4's echo %%~nxi >> output.csv , to show full path of each file in your output.csv.

This is just a slight modification to POW's answer that should give you better performance. This technique keeps the file open for writing. When you use the append multiple times, it is opening and closing the output file. So the file pointer has to be reset every time it outputs to the file.
#echo off
(FOR /F "usebackq delims=" %%i IN (`dir /s /b /O:N *.csv`) do (
echo %%~nxi
echo %%i >con
type "%%i"
echo.
echo.
)
)>output.tmp
rename output.tmp output.csv

Here is another possible solution:
#echo off
for /R %%A IN (*.csv) do (
if not "%%~nxA"=="output.csv" (
(echo %%~fA && type "%%~fA" && echo. && echo.)>>output.csv
echo Processed: %%A
echo %%~nxA
)
)
with much less code.
Using /R option with for to loop through subfolders in %cd%.
We want to parse ALL csv files so, we specify it in parenthesis with (*.csv).
Then, we check if csv file currently processed is output.csv.
If not, then we append full path of the csv ("%%~fA") file, its content (type "...") and two newlines (echo.) to output.csv. This condition is not really required, but added to be on-topic with the question. Also, if you don't want to append full path to output.csv, but filename and extension only just replace %%~fA with %%~nxA.
After that, we echo current file processed and its filename and extension.
If file currently processed is output.csv, then we repeat the loop.
Now, output.csv has the contents of all csv files in all subdirectories.
So, now open a new cmd and type the following commands. Read the output carefully:
for /?
if /?
echo /?
type /?
Some suggestions for further reading:
https://ss64.com/nt/for_cmd.html
https://ss64.com/nt/for_r.html
https://ss64.com/nt/syntax-redirection.html
What does "&&" in this batch file?

Thanks to all of you for your help. After reading all the responses, I realized that I could get around the windows sorting problem by sorting into a second file and then reading it from there.
dir /b /s /O:N *.csv | sort > file.txt
for /f %%A IN (file.txt) do (
if not %%~nxA == output.csv (
echo %%~dpnxA
echo %%~nxA >> output.csv
type %%A >> output.csv
echo. >> output.csv
echo. >> output.csv
)
)
Again, thank you for all your help.

Related

How can I output a filename without extension in batch?

I'm a real "newbe" in batch and wrote the following script:
chdir C:\Users\oRookie\Desktop\batch
dir /b *txt > out.txt
The file out.txt is created and contains out.txt .
So far so good let's try to get more advancaded. Now I want it's content to be just out´. So I need some way to delete the fileextension in the Output, but without deleting the fileextension itself. How can I do it?
Thanks in advance.
Give a try for this code and let me know if this what are looking for or not ?
#echo off
Set Location=%userprofile%\Desktop\
Set Log=out.txt
if exist %Log% del %Log%
for /f %%i in ('dir /b /a-d %Location%*.txt') do #echo %%~ni >> %Log%
start "" %Log%
No idea why you would want to do this but you can do something like this from command line:
type nul > out.txt | for /r %i in (*.txt) do #echo %~ni >> out.txt
Or in a script:
type nul > out.txt | for /r %%i in (*.txt) do #echo %%~ni >> out.txt
This should do it:
for /f "tokens=1 delims=." %%g in ('dir /b C:\Users\oRookie\Desktop\batch') do echo %%g >> out.txt
outputs a txt named out.txt containg only the text "out"

Recursive search by file list

I have a TXT file with a list of names like:
Test
Word
etc.
I need to search ( recursive, so also in subfolder ) if one of that name match with a file. This is my attempt:
#echo off
Set MyPath=C:\Folder
for /f %%i in (list.txt) do (
echo File with word %%i: >> result.txt
echo. >> result.txt
findstr /M /C:%%i /S "%MyPath%\*.*" >> result.txt
echo. >> result.txt
)
The result.txt i want is:
File with word test:
C:\Folder\test.exe
C:\Folder\my test.txt
C:\Folder\another test.doc
C:\Folder\tatest.bat
File with word Word:
C:\Folder\This is my word.exe
C:\Folder\CoolWord.txt
C:\Folder\hello word.bat
So how to improve that batch and make it recursive also for the subfolder of the initial dir?
Thanks
Test this: It will write result.txt with the results for each word.
#echo off
(
for /f "usebackq delims=" %%a in ("list.txt") do (
echo "File with word %%a:"
echo.
set "flag="
for /r "c:\folder" %%b in ("*%%a*") do (echo %%b&set flag=1)
if not defined flag echo No Matches
echo.
)
)>"result.txt"
dir /s /b C:\Folder\*.* >allfiles.txt
for /f %%i in (list.txt) do (
echo File with word %%i: >> result.txt
echo. >> result.txt
findstr /M /C:%%i /S "allfiles.txt" >> result.txt
echo. >> result.txt
)
#echo off
Set MyPath=C:\Folder
(for /f "delims=" %%i in (list.txt) do (
echo File with word %%i:
echo/
dir /B /S "%MyPath%\*%%i*"
echo/
)) > result.txt

Need a batch file that can find folders based on filenames

I have a need for a batch file or utility that would be able to find any "un-compressed archive folders" that are no longer needed and can now be deleted because the original archive file is still present.
The key is that the "un-compressed folder" and the "original archive file" always have the same name except for the file extension of the archive file. I do not want to automatically delete anything; I just want to create a list of folders that I can manually check out. So the sequence would be a 4 step process:
1) Search for all archive files using wildcards such as *.zip, *.rar, *.iso
2) Create a list of all of the filenames that are found - minus the file extensions
3) Use the list created in step two to search for any folders with those names
4) Create a text file with any folders found in step three.
I tried modifying a batch file that I found in these posts but it didn't work and I would like to start from scratch. Any help would be greatly appreciated.
Thanks
Ok, I'll do this step by step:
Step 1:
set dir="C:\...[path to root directory]"
where /r %dir% *.zip *.iso *.rar >> log.txt
Note the where utility should be on your computer if using windows 7.
Step 2:
ren log.txt log.tmp
for /f "delims=." %%a in (log.tmp) do (Echo %%a >> log.txt)
del log.tmp
The above code will not handle files names with periods in it
Step 3:
for /f "tokens=*" %%a in (log.txt) do (
where /r %dir% %%a* >> files.txt
)
Not 100% sure if above will work, tell me if it doesn't.
Step 4:
Rem This code will handle file paths to directories
Ren files.txt files.tmp
for /f "tokens=*" %%a in (files.tmp) do (
Echo %%~pa >> files.txt
)
del files.tmp
Rem The below code will ged rid of repeated direcotries
ren files.txt files.tmp
Echo. > files.txt
setlocal enabledelayedexpansion
for /f "tokens=*" %%a in (files.tmp) do (
set var=1
for /f "tokens=*" %%b in (files.txt) do (
if "%%~a" equ "%%~b" set var=0
)
if !var!==1 Echo %%a >> files.txt
)
del files.tmp
And I'm rather confident that should work. Of course I haven't tested this, but run all of this with #Echo on and a pause command between each sect (or as seperate batch files) so that if an eror does occur I can try helping you.
Hope this was helpful, Mona.
#echo off
setlocal enableextensions
set "rootDir=d:\_data_"
set "fileList=%~dp0\%~n0.files.list"
set "folderList=%~dp0\%~n0.folders.list"
rem generate list of compressed files names
break > "%fileList%"
for /F "tokens=*" %%f in ('where /r "%rootDir%" *.zip *.rar *.iso *.7z 2^>nul') do (
>> "%fileList%" echo %%~nf
)
rem check compressed file list against directory list
break > "%folderList%"
for /F "tokens=*" %%f in ('dir "%rootDir%" /s /b /ad ^| findstr /e /g:"%fileList%" ') do (
>> "%folderList%" echo %%f
)
type "%folderList%"
endlocal

How can i copy a name from a file with spaces in and create a new file with this name

I am after a batch file to convert two files with the same name but different extensions
ie
test file.dat
test file.txt
I want to combine these files with a seperator (I have used ~)
I want the new file to be named a new extension but keep the original name, I have managed this but the majority of the files I use have spaces in them and I can get it to work with the spaces
The code I have so far:
type *.dat > new.andy
echo. >> new.andy
echo ~ >> new.andy
type *.txt >> new.andy
for /F %%a in ('dir /b *.dat') do set FileName=%%~na
REN new.andy %FileName%.andy
So far I have got the script to work apart from to rename the file with its full name including spaces, up to now it only goes till first space ie test.andy
Test the following
type "*.dat" > new.andy
echo. >> new.andy
echo ~ >> new.andy
type "*.txt" >> new.andy
for /F %%a in ('dir /b *.dat') do set FileName=%%~na
REN new.andy %FileName%.andy
You could start with something like this:
#echo off
for /f "tokens=*" %%a IN ('dir /b *.dat') do (
type "%%a" > "%%~na.andy"
echo. >> "%%~na.andy"
echo ~ >> "%%~na.andy"
)
for /f "tokens=*" %%a IN ('dir /b *.txt') do (
type "%%a" >> "%%~na.andy"
)
This will combine all *.dat files with all *.txt files, and the name of the combined file will be .andy.
Depending on what you are after and how your files are organized, this can be improved.

Batch file to add character

if I have a .txt that has the following:
2005050 "2/19/2005"
2005060 "3/1/2005"
2005070 "3/11/2005"
2005080 "3/21/2005"
2005090 "3/31/2005"
Is there a way for the batch file to read and always add .png in the end of a character of 7.
For example.
2005050.png "2/19/2005"
2005060.png "3/1/2005"
2005070.png "3/11/2005"
2005080.png "3/21/2005"
2005090.png "3/31/2005"
This batch file will split each line at the first space, and append .png to the string before the split. The script reads lines from infile.txt and outputs to outfile.txt.
#echo off
echo. > outfile.txt
for /f "tokens=1*" %%i in (infile.txt) do echo %%i.png %%j >> outfile.txt
Update
Or to delete the outfile.txt first....
#echo off
del /q /f outfile.txt
for /f "tokens=1*" %%i in (infile.txt) do echo %%i.png %%j >> outfile.txt
Another Update
To just add new records to outfile.txt do something like....
#echo off
for /f "tokens=1*" %%i in (infile.txt) do (
find "%%i.png %%j" outfile.txt > nul
if errorlevel 1 then (
echo %%i.png %%j >> outfile.txt
)
)
Sed for windows
sed -r "s/^(.......)(.*)/\1.png\2/" file
My Answer failed: I did try: but I couldn't get the SET to WORK within the FOR
#echo off
set str1=ooo
set str2=ppp
for /f "tokens=*" %%a in ('type testprog.txt') do (
set str=!%%a!
echo %%str:~0,7%%.png %%str:~-5,14%% >> tempprog.txt
)
move tempprog.txt testprog.txt
start testprog.txt
Maybe someone can edit a working version as I would like to see what
I did wrong...

Resources