How to combine filename and extension in windows batch file? - windows-7

I have managed to extract the filename and the extension and passed that to a .bat file in windows. It's like
%1 = filename
%2 = extension
The problem that I am having is that I can't seem to do %1%2 because the command doesnt seem to like the %1%2 command ? If I do %1<space>%2 it seems happy but then the file is broken because its like file<space>.js
How am I able to get them side by side and still print out ?

#set NAME=somefile
#set SUFFIX=txt
#echo file = %NAME%.%SUFFIX%
OUTPUT:
file = somefile.txt

Related

Windows batch - concatenate multiple text files into one

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).

Hide filenames in type command batch file

I'm creating a batch file and the final part of the batch is to concatenate the text from multiple csv files into one. The csv that is created is correct, however when I run the command it shows the file names on a separate line. Even with echo off it still shows. When I simply copy and paste the line into a blank txt file and use #echo off and that's it.. it still shows the output.
type lak-print01.csv lak-print02.csv lak-print03.csv lak-print04.csv or-sdc-print.csv tac-print01.csv tac-print02.csv tuk-print01.csv wa-02print01.csv wa-110print01.csv wa-61print01.csv > MasterPrinterList.csv
The output shown in cmd line is as follows. I just want it to perform the type command w/o showing the files.
lak-print02.csv
lak-print03.csv
lak-print04.csv
or-sdc-print.csv
tac-print01.csv
tac-print02.csv
tuk-print01.csv
wa-02print01.csv
wa-110print01.csv
wa-61print01.csv
Any help?
Redirect StdErr to nul by appending 2>nul. (type outputs filenames into that.) The command you want is this:
type lak-print01.csv lak-print02.csv lak-print03.csv lak-print04.csv or-sdc-print.csv tac-print01.csv tac-print02.csv tuk-print01.csv wa-02print01.csv wa-110print01.csv wa-61print01.csv > MasterPrinterList.csv 2>nul
type lak-print01.csv lak-print02.csv lak-print03.csv lak-print04.csv or-sdc-print.csv tac-print01.csv tac-print02.csv tuk-print01.csv wa-02print01.csv wa-110print01.csv wa-61print01.csv > MasterPrinterList.csv 2>nul
the file names are printed in error stream so all you need is to add 2>nul at the end.
type also accepts wildcards so you can make your line shorter e.g
type lak*.csv or*.csv tac*.csv wa*.csv > MasterPrinterList.csv 2>nul

Concatenate / Join MP4 files using ffmpeg and windows command line batch NOT LINUX

I've written a batch script that attempts to take a generic introductory title video (MP4) that runs for 12 seconds and attaches it to the beginning of 4 other MP4 videos (same video but each has a different language audio track)
According to ffmpeg syntax here: http://ffmpeg.org/trac/ffmpeg/wiki/How%20to%20concatenate%20%28join,%20merge%29%20media%20files the concat demuxer needs to be run from a text file that looks like this:
# this is a comment
file '/path/to/file1'
file '/path/to/file2'
file '/path/to/file3'
I believe everything in my script up until the point of joining the files appears to be working correctly. But I get this error:
[concat # 04177d00] Line 2: unknown keyword ''C:\Users\Joe\1May\session3\readyforfinalconversion\frenchfile.mp4'
filelistFrench.txt: Invalid data found when processing input
[concat # 03b70a80] Line 2: unknown keyword ''C:\Users\Joe\1May\session3\readyforfinalconversion\spanishfile.mp4'
filelistSpanish.txt: Invalid data found when processing input
[concat # 0211b960] Line 2: unknown keyword ''C:\Users\Joe\1May\session3\readyforfinalconversion\basquefile.mp4'
filelistBasque.txt: Invalid data found when processing input
[concat # 03a20a80] Line 2: unknown keyword ''C:\Users\Joe\1May\session3\readyforfinalconversion\Englishfile.mp4'
filelistEnglish.txt: Invalid data found when processing input
I believe the issue lies in the text file I'm creating. Please excuse my n00b ignorance, but sometimes new script makers like myself get confused about developer jargon and may take things literally.
So when I look at that example text file they gave, am I correct in thinking THIS is what my text file should look like?
# this is a comment
Titlefile.mp4 'C:\Users\Joe\1May\session3\readyforfinalconversion\Titlefile.mp4'
Englishfile.mp4 'C:\Users\Joe\1May\session3\readyforfinalconversion\Englishfile.mp4'
Again, am I being too literal? are the quotations correct? Are the slashes correct? In the example they provide the slashes in the path are / instead of normal windows \ . I'll provide the entire script in case it helps.
#echo off
setlocal EnableDelayedExpansion
rem Create an array of languages
set i=0
for %%a in (French Spanish Basque English) do (
set /A i+=1
set term[!i!]=%%a
)
rem Get the title video file name from user
set /p titlevideofilename=What is the title video file
name?
rem create a path variable for the title video file
set pathtotitlevideo=%~dp0%titlevideofilename%
rem Get the names of the different language video files to append to the title video
rem create a path variable for each different language video files
for /L %%i in (1,1,4) do (
set /p language[%%i]=what is the name of the !term
[%%i]! file you want to append after the title video?
set pathtofile[%%i]=%~dp0!language[%%i]!
)
rem create data file for ffmpeg based on variable data
for /L %%i in (1,1,4) do (
echo # this is a comment>>filelist!term[%
%i]!.txt
echo file '%pathtotitlevideo%'>>filelist!term[%
%i]!.txt
echo file '!pathtofile[%%i]!'>>filelist!term[%
%i]!.txt
)
cls
rem join files using ffmpeg concat option
for /L %%i in (1,1,4) do (
c:\ffmpeg\ffmpeg\bin\ffmpeg.exe -loglevel error -f
concat -i filelist!term[%%i]!.txt -c copy !language[%
%i]!.!term[%%i]!.withtitle.mp4
)
endlocal
:eof
exit
EDIT
Thanks to #foxidrive making me look at the simplicity of it... it occurred to me that Apparently I wasn't being literal enough. I made these 3 changes and script works perfectly now
1: "file" in there example literally meant the word "file"
2: needed the use of single quotes not double quotes as it shows in there example.
3: Used "\" instead of "/" as they have in there example.
So NOW my code to create the text files looks like this:
rem create data file for ffmpeg based on variable data
for /L %%i in (1,1,4) do (
echo # this is a comment>>filelist!term[%
%i]!.txt
echo file '%pathtotitlevideo%'>>filelist!term[%
%i]!.txt
echo file '!pathtofile[%%i]!'>>filelist!term[%
%i]!.txt
)
So NOW my text file looks like this:
# this is a comment
file 'C:\Users\Joe\1May\session3\readyforfinalconversion\Titlefile.mp4'
file 'C:\Users\Joe\1May\session3\readyforfinalconversion\Englishfile.mp4'
From reading your question, I'd suggest that the configuration file might look like this:
# this is a comment
file 'C:\Users\Joe\1May\session3\readyforfinalconversion\Titlefile.mp4'
file 'C:\Users\Joe\1May\session3\readyforfinalconversion\Englishfile.mp4'
It may need double quotes around the path\filenames instead of single quotes, for Windows. Try it both ways.
OS Windows
Step 1: Create a file for example C:\iAmAFile.txt with following contents and save it
file 'C:\video1.flv'
file 'C:\video2.flv'
Step 2: Download FFmpeg http://ffmpeg.zeranoe.com/builds/win32/static/ffmpeg-20140716-git-faafd1e-win32-static.7z . Unzip it, go to the folder and you will find a ff.prompt.bat file which is like command prompt open it.
Step 3: now apply the tutorial command (Concat demuxer is more reliable it doe s not break the encoder/decoder just copy) https://trac.ffmpeg.org/wiki/How%20to%20concatenate%20(join,%20merge)%20media%20files#demuxer
> ffmpeg -f concat -i C:\iAmAFile.txt -c copy C:\output.flv
Done
multiplatform solution, using python:
(incomplete snippet)
combined = 'combined.mp4'
tempf = 'tempfile'
s = ''
for f in sys.argv[1:]:
s += f"file '{f}'\n"
open(tempf, 'w').write(s)
cmd = f'ffmpeg -f concat -safe 0 -i {tempf} -c copy {combined}'
os.system(cmd)

Windows Batch file Dynamic create statements

I need to get a list of file names from a directory using a windows batch program. I would like to take each FILE NAME and combine that with another command line statement.
Note i only need the file name not the contents of the file.
How would this be done?
If i have a 'Data' directory on the D drive with the below files (note there could be many files)
--------------
myFile1.abc
myfile2.abc
------------------
How could i dynamically create something like this using a windows batch program?
move C:\myFile1.abc C:\newdir
move C:\myFile2.abc C:\newdir
note - (i know there is a easier way move files but but i am trying to understand the logic so i can use it in a different command)
You can use a for loop:
for %%X in (D:\*) do (
echo move %%X C:\newdir
)
Try on the command line:
for %X in (D:\DataFiles\*) do echo move "%~fX" C:\newdir>>logfile.txt
It puts all file names from D:\DataFiles in logfile.txt (except hidden files).

Creating a blank file from a batch file

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

Resources