I have searched the world wide web high and low, and can't seem to find any solution for my problem! I have multiple text files that I would like to combine.
It's easier to show examples than to explain exactly why I'm trying to do.
The first file looks like this:
John
Paul
Mark
Sam
Herold
This file serves as a "primary key".
The rest of the files contain data for each item like this. A program generates this data into a new file each hour.
4
10
20
5
200
I'm most familiar with windows batch files, so I tried to write something like this:
for /f "tokens=*" %%A in (file1.txt) do
(for /f "tokens=*" %%B in (file2.txt) do (echo %%A,%%B>>combined.txt))
Unfortunately that writes every value to every person. If this would be working as expected, the end result would be like this:
John,4,2,6,9,1,2,5,6,12,51,53,3,6,7,8,1,4,7,2,743,21,4,7,5
Paul,10,5,6,1,7,9,34,56,1,76,48,23,222,12,54,67,23,652,1,6,71,3,6,4
and so on.
The software I am using presents the data in this format, and cannot be changed. I am open to any and all suggestions.
You may read several input files in a Batch program via the standard handles. Remember that 0 is Stdin, 1 is Stdout and 2 is Stderr, but this leaves handles 3 to 9 available! The Batch file below do a file merge with the contents of two files; of course, up to 8 files can be combined with this method.
#echo off
setlocal EnableDelayedExpansion
Rem First file is read with FOR /F command
Rem Second file is read via standard handle 3
3< file2.txt (for /F "delims=" %%a in (file1.txt) do (
Rem Read next line from file2.txt
set /P line2=<&3
Rem Echo lines of both files
echo %%a,!line2!
))
Further details here: http://www.dostips.com/forum/viewtopic.php?f=3&t=3126
The unix command paste maybe what you're looking for. Also see this question on StackOverflow.
You could just do:
paste -d, file*.txt > combined.txt
if you have paste available. You may need to install cygwin, or work on a *nix machine. (You did say you are open to all suggestions!) This relies on the data files being named sequentially. If you want to tweak the order, you can spell it out instead of using the glob.
Related
I didn't find so far a similar question, so I try to explain the problem:
I have large number of files that are in subfolders inside "C:\images"
I have a list of names in two columns, so that 1st column is old filename and 2nd is a new filename. I want to change that list into a batch file.
Names are pretty unique so I want to make batch file - so that will be one command for every file to be renamed.
RENAME "C:\images\768e11ab.jpg" "4ca5d042.jpg"
RENAME "C:\images\5402c708.jpg" "b802820b.jpg"
RENAME "C:\images\1c039e0e.jpg" "80ce9797.jpg"
etc...
It is rather simple, only, files are scattered across subfolders. Is there any way to make a command so it will look for that specific file in all subfolders in "C:\images" and rename it.
Following some similar questions tried this, with no result:
for /r "C:\images\" "%%~G" (768e11ab.jpg) do "4ca5d042.jpg"
Also, tried to use some renaming application for this but they freeze when I try to rename big list of files, so I would avoid them and use batch file. Also, I would like to use this way where there is one line in batch file for every file because it is simpler for me to use it (and change it later). I appreciate your help.
Approach the problem from the other side. Instead of looping over the image files, loop over the text file.
Assuming your textfile to be something like
"768e11ab.jpg" "4ca5d042.jpg"
"5402c708.jpg" "b802820b.jpg"
"1c039e0e.jpg" "80ce9797.jpg"
Then the code could look like:
#echo off
REM read each line; %%A is the first column, %%B is the second one
for /f "tokens=1,2" %%A in (list.txt) do (
REM search the image file %%A
for /R "C:\Images\" %%C in ("%%~A") do (
REM %%C now holds the full path of %%A
ECHO ren "%%~C" "%%B~%%~xC"
)
)
If your list looks different, the tokens and perhaps the delims for the for /f loop have to be adapted.
NOTE: the ren command is just echoed for security reasons. Once you verified it does exactly what you want, remove the ECHO to enable the * ren` command.
I would be really grateful for anyone that could save me some time on this.
I have a Windows batch file that I am using to match two different file types with different extensions and write the results out to a file.
It reads through a table of filenames (called matched_filenames.txt) and, for each line in it, tries to locate the filename in two other tables.
I have this code (which works to an extent).
for /f "tokens=*" %%z in (%Ourhome%\matched_filenames.txt) do (
rem -------
rem ------- now (for each line), find the photo name in both the list of raw and list of jpg files
rem -------
findstr /C:"%%z" raw_photos_directory.txt >>located_matches.txt
findstr /C:"%%z" jpg_photos_directory.txt >>located_matches.txt
)
But it writes separate lines out to the located_matches.txt file.
I would really like to assign the results of the findstr's to variables so that I can then concatenate them and then write the concatenated line as a single line to the file.
Can anyone recommend a more elegant way of doing this?
I have multiple folders with multiple files in each of them. The file names are as following: static-string-1.wav static-string-2.wav .... static-string-10.wav ... static-string-99.wav static-string-100.wav ... and so on. The static string remains same but the number before .wav changes from 1 - 999. Now the problem is if I want to play this on any media player or want to join all the files, windows will sort like 1,10,100,2,20,200 not 1,2,3,4,5,6,7,8,9,10 which messes up the playback. So to fix this, I have to rename each file from static-string-1.wav to static-string-0001.wav and so on.
Currently, I am doing a dir command to an output file and then I copy the file list in excel from where I do some playing around with concatenate and text to columns and come up with two columns of old name and new name which I then again convert to a .bat file with and run it. The bat file has multiple rows with individual rename commands something like this:
#echo off
rename <oldname1> <newname0001>
rename <oldname2> <newname0002>
.
..
exit
It is getting the work done but I know there is and easier and more efficient way to use for loops. I saw few example and previous answers but they dont have a similar requirement as me. Any help will be appreciated.
Leading zeros can be added and (later) truncated if not needed. This question is a possible duplicate, but I don't like how files are sorted like that either.
Delaying the expansion allows b to change within the for loop instead of being static (haha puns) throughout the whole program. Therefore you can increment b each loop and rename the files. This is a simple example:
#echo off
setlocal ENABLEDELAYEDEXPANSION
for /l %%a in (1,1,99) do (
set b=00%%a
rename static-string-%%a.wav static-string-!b:~-2!.wav
)
This should work. Contact me if you need more help
Below is a significant improvement to Clayton's answer
Only numbers less than 100 need be modified
The script automatically works with any static prefix. See How does the Windows RENAME command interpret wildcards? for an explanation of how this works.
The script reports any file names that could not be renamed
#echo off
setlocal enableDelayedExpansion
for /l %%N in (1 1 99) do (
set "n=00%%N"
for %%F in (*-%%N.wav) do ren "%%F" *-!n:~-3!.* || >&2 echo ERROR: Unable to rename "%%F"
)
Or, you could grab my JREPL.BAT regular expression renaming utility and simply use:
jren "-(\d{1,2})(?=\.wav$)" "'-'+lpad($1,'000')" /j
I need to delete the first two lines from a very large amount of txt files (about 10.000). I'm looking for a way to do this from the command line or through another semi-automatic procedure. Every file is different from the other, but they all contain some information on the first two lines that I need to get rid of.
for %%a in (*.txt) do (
more +2 < "%%~fa" > 2linesskipped.txt
move /y 2linesskipped.txt "%%~fa"
)
without external tools.
I think aguslr's answer looks good on a UNIX-derivative system, but I'm guessing since the tags are "cmd" and "batch-file" that you are on Windows. You can get sed for Windows in several places, for example in the UnxUtils ports or more up-to-date ones via Cygwin or GnuWin32. But find is a built-in Windows program that is nothing like the find on UNIX-derivative systems. So you would probably want to replace the second half of the answer with a "for" command in Windows like:
for /r %a in (*.txt) do sed -i '1,2d' %a
Note that if you run this from a batch file you will want to use %%a instead of %a.
I'm writing a batch file that uses an output from a dir command to perform other tasks, I also want to use this same output (stored in dir_output.txt) for another use, but I don't want the file extensions at the end. right now the file looks like this:
barrier_1_post.p3d
barrier_1_section.p3d
but I want it to look like this
barrier_1_post
barrier_1_section
minus the file extensions, but I have no idea how to do this via the batch, I've looked through SO exhaustively but either I'm not finding the solution or I can't see the wood for the trees.
Any help would be amazing, I'm fairly new to batches.
for /f "delims=" %%a in (yourfilename.txt) do echo %%~na
should remove those extensions quite happily.
%~na delivers the name part only of the assumed filename in %a
(see for /? from the prompt for documentation)