I'm trying to get this batch file to work. I have about 200 files in a folder (C:\Users\bloughj\Documents\Fluxes)
I want the batch file to go through each file in that folder and extract a section of data using sed and awk (the binaries reside in a network folder K:\Physics\bin\1112\Tools). Then write that section of data to an individual file named after the original input file and place that file in C:\Users\bloughj\Documents\Fluxes\output
My batch file is:
set bindirectory=K:\Physics\bin\1112\Tools
set outputdirectory=C:\Users\bloughj\Documents\Fluxes\output
for %%f in (C:\Users\bloughj\Documents\Fluxes\) do (
call %bindirectory%\sed.exe -n '/---new---/,/---old---/ { p; }' %%f |
call %bindirectory%\gawk.exe '{ print $3 }' >> %outputdirectory%\%%f.txt
)
But this doesn't seem to work. I keep getting an error 'The syntax of the command is incorrect.'. Am I doing my for loop wrong?
Batch file to append 'Text' from one File to a 'Central Text File'(Not overwrite text on the main file)?
The following code dosnt copy the contents of 'log2.log' instead it just writes the name of directory 'C:....\LogFiles\log2.log' to the log.txt
#echo "C:....\LogFiles\log2.log" >> "\1xx.1xx.1.xx\c$....\log.txt"
In this case you are echoing the name of the file. If you would like the output of a file to be transferred, use the keyword type
type "C:....\LogFiles\log2.log" >> "\1xx.1xx.1.xx\c$....\log.txt
If you want to append a new line afterwards you can do:
echo. >> destination
Where you replace destination with the destination text file.
I have provided all the detail and code below, but, the problem is only in the last line.
Background
I am creating a script named, GetSelectedFiles.cmd, whose shortcut is added to Windows' "Send To" context menu. For example, by copying the shortcut of above file to location: c:\users\user\AppData\Roaming\Microsoft\Windows\SendTo.
Purpose:
The goal of that script file is to get the list of selected file names when the user goes to the context menu and selects Send To > GetSelectedFiles.cmd.
Requirement:
1) The the selected file names will be separated by new lines.
2) The list will only contain the bare file name and extension (no path).
3) The list will be a saved inside the same directory as selected files.
4) The list will will be saved in a file whose file name matches the first selected file and has extension of `.selection`.
Example:
Assume you are in directory c:\users\u\. Say this directory has files named: w.txt, x.txt, y.txt, z.txt, with some other files.
User selects those above named 4 files, right clicks and does Send To > GetSelectedFiles.cmd.
After completing the above steps, the directory should have a new file named w.selection and it should contain following lines
w.txt
x.txt
y.txt
z.txt
It is a basic task, but, where I am having problem is the last line, specifically %firstFile% just returns empty. What am I missing?
::::::::::::::::::::::::::::::::::::
:::: Here is the complete code :::::
setlocal EnableDelayedExpansion
:: For new line const
set LF=^
for %%A in (%*) do (
IF NOT DEFINED firstFile (SET firstFile=%%~nxA)
::: This last line is the problem!
echo %%~nxA ^%LF% >> %%~dpA%firstFile%.selection
)
I need to create a script, which concatenates multiple text files into one.
I know it's simple to use
type *.txt > merged.txt
But the requirement is to "concatenate files from same day into file day_YYYY-DD-MM.txt" I am a Linux user and Windows batch is hell for me. It's Windows XP.
Windows type command works similarly to UNIX cat.
Example 1: Merge with file names (This will merge file1.csv & file2.csv to create concat.csv)
type file1.csv file2.csv > concat.csv
Example 2: Merge files with pattern (This will merge all files with csv extension and create concat.csv)
When using asterisk(*) to concatenate all files. Please DON'T use same extension for target file(Eg. .csv). There should be some difference in pattern else target file will also be considered in concatenation
type *.csv > concat_csv.txt
At its most basic, concatenating files from a batch file is done with 'copy'.
copy file1.txt + file2.txt + file3.txt concattedfile.txt
In Win 7, navigate to the directory where your text files are. On the command prompt use:
copy *.txt combined.txt
Where combined.txt is the name of the newly created text file.
Place all files need to copied in a separate folder, for ease place them in c drive.
Open Command Prompt - windows>type cmd>select command prompt.
You can see the default directory pointing - Ex : C:[Folder_Name]>.
Change the directory to point to the folder which you have placed files to be copied, using ' cd [Folder_Name] ' command.
After pointing to directory - type 'dir' which shows all the files present in folder, just to make sure everything at place.
Now type : 'copy *.txt [newfile_name].txt' and press enter.
Done!
All the text in individual files will be copied to [newfile_name].txt
I am reiterating some of the other points already made, but including a 3rd example that helps when you have files across folders that you want to concatenate.
Example 1 (files in the same folder):
copy file1.txt+file2.txt+file3.txt file123.txt
Example 2 (files in same folder):
type *.txt > combined.txt
Example 3 (files exist across multiple folders, assumes newfileoutput.txt doesn't exist):
for /D %f in (folderName) DO type %f/filename.txt >> .\newfileoutput.txt
We can use normal CAT command to merge files..
D:> cat *.csv > outputs.csv
cat "input files" > "output files"
This works in PowerShell, which is the Windows preferred shell in current Windows versions, therefore it works. It is also the only version of the answers above to work with large files, where 'type' or 'copy' fails.
Try this:
#echo off
set yyyy=%date:~6,4%
set mm=%date:~3,2%
set dd=%date:~0,2%
set /p temp= "Enter the name of text file: "
FOR /F "tokens=* delims=" %%x in (texto1.txt, texto2.txt, texto3.txt) DO echo %%x >> day_%temp%.txt
This code ask you to set the name of the file after "day_" where you can input the date.
If you want to name your file like the actual date you can do this:
FOR /F "tokens=* delims=" %%x in (texto1.txt, texto2.txt, texto3.txt) DO echo %%x >> day_%yyyy%-%mm%-%dd%.txt
You can do it using type:
type"C:\<Directory containing files>\*.txt"> merged.txt
all the files in the directory will be appendeded to the file merged.txt.
copy is definitely much faster than type - but it sometimes (with large files?) adds a SUB character at the end of the file. So, strictly speaking, it does not simply concatenate the files in the same way as cat in Unix.
So, the correct answer is to use cat - either in something like Git Bash (where it has the same syntax as in Unix), or PowerShell (where it does not).
I would like create a file from a batch file. I can use the echo. > donald.txt, but it creates it with an initial blank line. What else I can use to a file starting from the first line?
You just want to create a completely empty file? This will do it:
copy /y nul donald.txt
If you're looking to write something to first line, you want something like this:
echo Hello, World > donald.txt
One of the options is
#echo off
rem Don't destroy an existing file
if exist testfile goto _nocreate
:: Create the zero-byte file
type nul>testfile
:_nocreate
Originally from http://www.netikka.net/tsneti/info/tscmd041.htm which contains some additional methods.
In old MS-DOS days I used this method to create an empty file:
rem > empty.txt
In new Windows this no longer works, but rem may be replaced by any command that show nothing. For example:
cd . > empty.txt
If you're using PowerShell:
function touch {set-content -Path ($args[0]) -Value ($null) }
touch file-name
Source: http://blog.lab49.com/archives/249.
That's because echo. inserts a blank line in your file.
Try using the same code, but without the period appended to the end of the echo statement:
echo "Test string" > donald.txt