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.
Related
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 have a file TestProject.dll and it resides in different locations on my computer like D:\Folder1\TestProject.dll, D:\Test\Info\TestProject.dll etc.
I want to find all these locations wherever it is located and prepare a text file (SearchResults.txt) which looks like as shown below:
D:\Folder1\TestProject.dll
D:\Test\Info\TestProject.dll
I want to do this using a batch scripting file. I am new to this scripting.
Please help me here.
use dir /s /b to search in a single drive.
Build a loop around that to check every disk.
put the complete output into a file.
(
for /f %%a in ('wmic logicaldisk where "drivetype=3" get caption^,size^|find ":"') do (
echo now checking drive %%a...
dir /b /s %%a\TestProject.dll
)
)>SearchResults.txt
Note: we don't really need the size here, this is only one of several ways to come around wmic's ugly line endings, that would ruin the rest of the code
where drivetype=3 means "Harddisks only" (drop it, if you want to search in all drivetypes (Thumbdrives, CD, whatever)
Remember: this will search through your whole file system(s), so it will need some time.
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)
How to delete the first line of a file using Batch?
I really need help... I've tried everything but it didn't work. :(
I've tried this :
#echo off
Type test.txt | findstr /I /V /C:"So, lets remove this line" >>Test2.txt
exit
Some tasks are clumsy with pure batch techniques (and mangles characters) - this is a robust solution and uses a helper batch file called findrepl.bat that uses built in jscript-ing from - http://www.dostips.com/forum/viewtopic.php?f=3&t=4697
Place findrepl.bat in the same folder as the batch file.
type "file.txt"|findrepl /v /o:1:1 >"newfile.txt"
Another method is to use more.exe but it has limitations with the number of lines at 64K and it mangles TAB characters.
more +2 <file.txt >newfile.txt
(echo off
for /f "skip=1 tokens=1* delims=" %A in (c:\windows\win.ini) do echo %A
echo on)
You'll need to loop through your file line by line and output each line, except the first one.
From your example it looks like you might be looking for a specific string.
try this question/answer .. it might help you get you on your way.
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.