Batch file: filepath manipulation - string

I am trying to create a little script to convert video on windows using the 'send to' menu.
For this I want to create a new file name from the one in input.
But I fail to concatenate strings.
Following the syntax found here, I wrote this piece of code:
#echo Input:
#echo %1
set "outputfile=%1%.MP4"
#echo %outputfile%
But I have an issue with the quotes in the outputfile:
Input:
"D:\this is a test\MVI_7754.AVI"
D:\this is a test>set "outputfile="D:\this is a test\MVI_7754.AVI".MP4"
"D:\this is a test\MVI_7754.AVI".MP4
I would expect the extension inside the quotes not outside!
Could someone tell me how I can concatenate the file name and the extension?
Thanks!

#echo Input:
#echo %1
set outputfile="%~1.MP4"
#echo %outputfile%
to remove previous extension
use
set outputfile="%~n1.MP4"
if you don't use path names or
set outputfile="%~dpn1.MP4"
(name will be converted to full path)

Related

Import title of files containing string into a text document using batch

I am trying to create a batch script that imports the titles of text files in a folder that contain a specific string into a new test file.
This is what I have achieved so far. It imports every text file into the document and if it contains the string, it will put the contents of that file under the title. I just want the title of the file to be imported. Thank you
find "test" dir *.txt > output.txt
OUTPUT:
---------- ALSO_CONTAINS.TXT
test.
---------- CONTAINS.TXT
This is a test
---------- CONTAINS_ASWELL.TXT
This is also a test
---------- SHOULD_FAIL.TXT
Instead of using find, use findstr instead, which if you read the usage information from findstr /? at the Command Prompt, will show you a /M option, (which prints only the filename if it contains a match)
FindStr /IM "test" *.txt>output.txt
Just in case there's an issue with your %PATH% and/or %PATHEXT% environment variables, you could also use:
"%__APPDIR__%FindStr.exe" /IM "test" "*.txt">"output.txt"

BASH: How to copy the name of the file and insert it into the text using script?

So I need to create a lot of text files.
Each of the text file is named AAAAA.txt, BBBBB.txt, CCCCC.txt and etc etc.
Within each text file, all the content is as follows:
1.Copy "to-be-replaced".txt into the folder EXCLUSIVE.
2.Copy the gs file to replace the existing gs file.
3.The .projectdata should also be copied to the correct path.
So, I need to write a script, that copies the name of the file (AAAAA, BBBBB, and so on) and then place it in the "to-be-replaced" within its content.
How can I do that? need some idea please.
Thank you~~
MT32
Use a HERE document which expands variables if the delimiter isn't quoted:
#!/bin/bash
for char in {A..Z} ; do
filename=$char$char$char$char$char.txt
cat <<EOF > $filename
1.Copy $filename into the folder EXCLUSIVE.
2.Copy the gs file to replace the existing gs file.
3.The .projectdata should also be copied to the correct path.
EOF
done

I would like a script to search within text files a certain pattern of strings and move those files to another directory

I need to know how to perform an advanced search on a text file using tools like notepad++, freecommander and windows if possible. A bat script file will be great.
The problem is that i need to search through about 1000 txt files in a directory. I need to know form those 1000 txt files which have a string in the form of for example "SYR_SHA/245/4". I just want it search for the pattern for example *****_******/*****/****** where * are characters that can change in number.
There must be an _ between the first and second set of characters as seen above in the example.
the script should go through the entire txt file and search for the above pattern. The script should then Move all the results in to a seperate directory
Many Thanks
Create this batch file and copy it into your folder where you have all the 1000 .txt files
Change the yourdestinationdirectory directory and run the batch file
#Echo Off
FindStr /M /R "[a-zA-Z]*_[a-zA-Z]*\/[0-9]*\/[0-9]*" *.Txt > findstr.out
For /F "tokens=*" %%a In (FindStr.out) Do call :move_Rtns %%a
del FindStr.out
Exit /B
:move_rtns
copy %1 yourdestinationdirectory\*
del /Q %1
Exit /B
Please change the Regex according to your requirement, for eg. If you are expecting number and letter together you can replace [a-zA-z] with [a-zA-Z0-9]
Good luck

Extract portion of a file name in shell

I am a newbie to Linux and am trying to extract portion of a filename from the absolute path in a bash script. For example, if the path is /opt/data/filename-attribute.dat, I am able to get the path of the directory and the file as follows:
sourcedir=`dirname $path`
name=`basename $path`
I would like to extract attribute from the file and was wondering if there was a quick way in shell script to perform this. I can get filename-attribute by
f=${name%%[.]*}
and would like to extract just the attribute.
The easiest way is just to do it in two steps:
f="${name%.*}" # strip everything from the last dot onward
f="${f##*-}" # strip everything up through the last hyphen
If I understand you right
a="${f#*-}"

Batch file string manipulation

This is a very specific question, however;
Say I have a batch file running from\located in the directory c:\data\src\branch_1
How do I set my environment variable %buildpath% to c:\build\bin\branch_1 in a batch file?
(To be extra clear, if the same batch file is located in c:\foo\bar\branch_2 I want it to set %buildpath% to c:\build\bin\branch_2)
You should be able to use the environment variable %~dp0 to get you the drive and path of the batch file currently running. From there, it's a not-very-efficient method of stripping off the end of that string character by character and building a new string.
For example, the batch file:
#setlocal enableextensions enabledelayedexpansion
#echo off
set olddir=%~dp0
echo Current directory is: !olddir!
if "!olddir:~-1!" == "\" (
set olddir=!olddir:~0,-1!
)
set lastbit=
:loop
if not "!olddir:~-1!" == "\" (
set lastbit=!olddir:~-1!!lastbit!
set olddir=!olddir:~0,-1!
goto :loop
)
set newdir=c:\build\bin\!lastbit!
echo New directory is: !newdir!
endlocal
running as c:\data\src\branch1\qq.cmd returns the following:
Current directory is: C:\data\src\branch1\
New directory is: c:\build\bin\branch1
As to how it works, you can use !xyz:~n,m! for doing a substring of an environment variable, and negative m or n means from the end rather than the beginning. So the first if block strips off the trailing \ if it's there.
The loop is similar but it transfers characters from the end of the path to a new variable, up until the point where you find the \. So then you have the last bit of the path, and it's a simple matter to append that to your fixed new path.
Old case, but still...
easy way of setting current directory into variable.
#echo off
cd > dir.tmp
set /p directory= <dir.tmp
echo %directory% <-- do whatever you want to the variable. I just did a echo..
del dir.tmp > nul

Resources