Convert the filename into project requirement pattern - batch-file

I want to convert this GIF file in same folder
muz-171669-JA_fig_10.gif
muz-171669-JA_fig_11a.gif
In this pattern:
muz is convert to muz12345
Here in numeric value "171669" only last 4 digit must be taken means:
"171669" is convert to 1669
fig_10 is convert to this value f010.
Sometime name is like this fig_11a so in that case it should be convert into this way:
f11a
There are hundreds of gif files with such filename
with suffix fig_01 till fig_999.
And it should be convert like this:
suffix f001 till f999
Sometimes fig_11a suffix occur in that case it should be convert like this f011a.
The output of the following :
muz-171669-JA_fig_10.gif
muz-171669-JA_fig_11a.gif
must be like this:
muz123451669f010.gif
muz123451669f11a.gif
I am doing this manually and it takes lots of my time.My friend told me that this is possible using batch extension. but i dont have must knowledge in batch command.
Can anyone help me out? its project requirement.

#ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
SET "sourcedir=U:\sourcedir\t w o"
FOR /f "tokens=1-7delims=-_." %%a IN (
'dir /b /a-d "%sourcedir%\muz-*-*_*_*.gif" '
) DO (
IF "%%g"=="" (
SET "num2=%%b"
SET "fig=%%d"
SET "num3=00%%e"
ECHO(REN "%sourcedir%\%%a-%%b-%%c_%%d_%%e.%%f" "muz12345!num2:~-4!!fig:~0,1!!num3:~-3!.%%f"
)
)
GOTO :EOF
This should process the files appropriately. You would need to change the setting of sourcedir to suit your circumstances.
The required REN commands are merely ECHOed for testing purposes. After you've verified that the commands are correct, change ECHO(REN to REN to actually rename the files.

Related

Batch file - output csv to different folders

everything in my code is working fine except the last part.
I am wanting to output each text file to the folder with the same name. It is outputing the three text files into the one folder PentahoOutputs. However I am wanting to output it as the following:
folder system2.object2.assets contains file system2.object2.assets
folder system3.object3.assets contains file system3.object3.assets
folder system4.object4.assets contains file system4.object4.assets
#echo off SetLocal EnableDelayedExpansion
SET DELIMS=,
SET COMMAND=AddChange
SET EN=EN
SET ASSETS=Assets
SET DIREC = C:\Users\user.username\Documents\Training\BatchFiles\PentahoOutputs\
SET DELIMS2=.
FOR /D %%a IN (C:\Users\user.username\Documents\Training\BatchFiles\PentahoOutputs\*) DO ( SET subdirs=%%a
result=!subdirs:~71,7!
result2=!subdirs:~79,7!
set "concats=!result!!delims!!result2!!DELIMS!!COMMAND!!DELIMS!!EN!"
echo !concats!
echo !CONCATS! >>C:\Users\user.username\Documents\Training\BatchFiles\PentahoOutputs\!result!!delims2!!result2!!delims2!!assets!.CSV
)
PAUSE>NUL
edit ********** below
changing the problem code to the following puts each of the three files in each of the three folders... however i want one file in each folder
for /d %%b in (C:\Users\usre.username\Documents\Training\BatchFiles\PentahoOutputs\*) DO ( echo !CONCATS! >>%%b\!result!!delims2!!result2!!delims2!!assets!.csv )
From your posted code - given aschipfl's change as noted (although you don't attempt to use direc)
Your posted code has been mangled in an attempt, I assume, to disguise usernames. It also appears that you've cut down the actual code to show only the relevant section. This is good and understandable (but your edit has a glaring typo in the code - which is why you should cut-and-paste as far as possible.)
So - the setlocal following the #echo off must be separated by a & command-concatenator or be (my preference) on a separate line.
Within your for ... %%a ... block, you've removed the required set keyword for result*.
The fixed values you've used for substringing don't suit the changes you've made to the pathname, so the result in result is (eg) "tem3.ob"
If a value does not change within a block (like delims) then it's probably best to use %delims% - result changes, so you'd use !result! not %result%. !delims! also works, of course - but using the delayed-expansion form primes the reader to believe it's going to vary. (opinion)
'tis best with a string assignment to use set "var=value" as the quotes ensure that stray trailing spaces are not included in the value assigned. You only ever need to have that happen once...
OK - here's a revision
#echo OFF
SetLocal
SET DELIMS=,
SET COMMAND=AddChange
SET EN=EN
SET ASSETS=Assets
SET DIREC=U:\Users\user.username\Documents\Training\BatchFiles\PentahoOutputs\
SET DELIMS2=.
FOR /D %%a IN (%direc%*) DO (
FOR /f "tokens=1,2,3 delims=." %%p IN ("%%~nxa") DO IF /i "%%r"=="%assets%" (
echo %%p%delims%%%q%DELIMS%%COMMAND%%DELIMS%%EN%
echo %%p%delims%%%q%DELIMS%%COMMAND%%DELIMS%%EN% >> %%a\%%~na.CSV
)
)
GOTO :EOF
Note that I've used U: for the test directory (it's a ramdrive on my machine)
Given the outer loop, %%a is assigned the full pathname to the directory.
Since you imply that your target directorynames are system2.object2.assets then %%~nxa (the Name and eXtension of %%a) conveniently holds this string. Parsing that using delims of . and selecting the first 3 tokens would assign system2 to %%p, object2 to %%q and assets to %%r This avoids the substringing problem and permits system and object to be any length - not just 7.
The if statement ensures that the main block for for...%%p is only executed for directories found which fit ..asset (/i makes the if case-insensitive)
The required line can then be constructed from the metavariables and constants, as can the destination filename, so the enabledelayedexpansion is not required.

batch - Removing a substring from a path while loop through a certain file type

I'm trying to get only one directory name out of a known path while searching for certain file types. In the example below I am searching for mp4 video files then I want to convert them and move them into a subdirectory of the same name in a different parent directory. The path is known up until the file's direct parent directory, so I was trying to remove the known part of the path. This is giving me a lot of trouble though. In the example below, the variable newDir is empty after the set command and it shows only an input of ~13 with #echo on. Can anyone tell me what I'm doing wrong?
Example:
setlocal enabledelayedexpansion
FOR /R %%X in ("*.mp4") DO (
set currDir="%%~pX" &REM "\test1\test2\dir1"
set newDir=%currDir:~13% &REM dir1
mkdir "C:\new\%newDir%" &REM suppose to be "C:\new\dir1", mine is just "C:\new\"
REM convert mp4 here
cp %%X "C:\new\%newDir%" &REM copies the file
)
#ECHO OFF
SETLOCAL
SET "sourcedir=U:\sourcedir"
FOR /R "%sourcedir%" %%a IN (*.mp4) DO (
FOR %%m IN ("%%~dpa.") DO ECHO(XCOPY "%%a" "c:\new\%%~nm\"
)
GOTO :EOF
You would need to change the setting of sourcedir to suit your circumstances.
The required XCOPY commands are merely ECHOed for testing purposes. After you've verified that the commands are correct, change ECHO(XCOPY to XCOPY to actually copy the files.
I note that you're using a very cygwinnish cp in place of copy. Be careful of nameclashes. I changed the command to xcopy in order that the destination directory can be automatically generated if required. Note that you haven't said what you want to do if the destination file already exists.
Here's a trial run on my test directory:
XCOPY "U:\sourcedir\misc\90s\ACDC - Back in Black.mp4" "c:\new\90s\"
XCOPY "U:\sourcedir\misc\90s\extended\Kelly Rowland - Wor! (Freemasons Arabic Mix).mp4" "c:\new\extended\"
XCOPY "U:\sourcedir\misc\90s\extended\Kelly Rowland - Work (Freemasons Arabic Mix).mp4" "c:\new\extended\"
XCOPY "U:\sourcedir\one\dummyfile1.mp4" "c:\new\one\"
XCOPY "U:\sourcedir\t w o\dum myfile2.mp4" "c:\new\t w o\"
XCOPY "U:\sourcedir\t w o\dum ile2.mp4" "c:\new\t w o\"
With enabledelayedexpansion in for loop, you should use !newDir! instead of %newDir%, and also !currDir:~13! instead of %currDir:~13% to make delayed expansion happen.
[update] There're also small errors. You should be careful about the spaces and quotes when you set to variables. They are actual part of the string to be set. And you have mkdir the same directory several times, and also it's better to user copy or xcopy instead of 'cp' which should be from "Cygwin" or "MinGW32" or something. xcopy will automatically create folders for you if not exist.
To make your codes work, please see below. And just a suggestion that you'd better learn more basic knowledge about bat before starting programming.
#echo off & setlocal enabledelayedexpansion
FOR /R %%X in ("*.mp4") DO (
set currDir=%%~pX
set newDir=!currDir:~13!
REM mkdir "C:\new\!newDir!"
REM convert mp4 here
xcopy "%%X" "C:\new\!newDir!"
)

How can I run an executable on files whose name begin with a specific phrase? (Windows 7)

I'd like to run pdflatex.exe on multiple .tex files, all beginning with a certain phrase I'd like to get a prompt for before the process starts.
How can I do that?
The files I want to process are named like this:
Chap01S01SS01_Description_Name_Whatever.tex
So all in all their names all begin with:
Chap01
Chap02
and so forth...
All the names end on .tex.
Obviously the numbers vary for the chapters and sections. This is why I'd like to make it easy for myself with the prompt: if I'd like to just run the files for section 2 in chapter 3, I run those, at some point I'd like to re-generate all the files for chapter 4 and 5... and so forth.
SOLUTION
#ECHO OFF
SETLOCAL
SET "sourcedir=D:\files\"
SET /p mask="Which .tex-files?"
PUSHD "%sourcedir%"
FOR /f "tokens=1*delims=" %%a IN (
'dir /b /a-d "%mask%*.tex" '
) DO (
pdflatex.exe -interaction=nonstopmode --shell-escape "%%a" any other parameters perhaps containing "%%~na"
)
POPD
GOTO :EOF
Full credit goes to Magoo, I just added the parameters -interaction=nonstopmode --shell-escape for the use with pdflatex. If you know what "externalization" stands for in regards to TikZ/pgfplots, use it, otherwise delete --shell-escape.
BONUS Just copy the file and write lualatex.exe instead of pdflatex.exe for huge graphics. Do note your preamble must be organized accordingly though.
#ECHO OFF
SETLOCAL
SET "sourcedir=U:\sourcedir"
SET /p mask="select which *.tex files ? "
PUSHD "%sourcedir%"
FOR /f "tokens=1*delims=" %%a IN (
'dir /b /a-d "%mask%*.tex" '
) DO (
ECHO(pdflatex.exe "%%a" any other parameters perhaps containing "%%~na"
)
POPD
GOTO :EOF
You would need to change the setting of sourcedir to suit your circumstances, or even use a similar set /p scheme to input the required directory, if you wish.
Since you haven't stated what the pdflatex command line looks like, I can only show pointers.
The required pdflatex commands are merely ECHOed for testing purposes. After you've verified that the commands are correct, change ECHO(pdflatex to pdflatex to actually process the files.
Assuming you have a wildcard specification that matches all the files you want, and only those files, processing them is trivial with something like
FOR %%f IN (Chap01*) DO ECHO Processing file: %%f
So the problem reduces creating a wildcard specification after prompting the user. You can accept user input interactively with SET /P, for example:
SET chapter=
SET /P chapter=Chapter to process (ENTER for everything)?
REM now %chapter% contains the input, or blank if none was given
Putting it all together to get a proof of concept:
#ECHO OFF
SETLOCAL
SET chapter=
SET section=
SET /P chapter=Chapter to process (ENTER for all)?
SET /P section=Section to process (ENTER for all)?
REM We 'll be left-padding the values to two digits with zeroes
SET chapter_spec=??
SET chapter=00%chapter%
SET section=00%section%
IF NOT "%chapter%"=="00" SET chapter_spec=%chapter:~-2%
IF NOT "%section%"=="00" SET section_spec=S%section:~-2%
SET wildcard=Chap%chapter_spec%%section_spec%*.tex
ECHO Files to be processed:
FOR %%f IN (%wildcard%) DO ECHO %%f

Determine filename length from a batch file

I need to work two things into a .bat file I am working on for a little project. First things first, I have to know if any filename contained into the same folder (recursively) I launch my .bat in is any longer than 100 characters. If so, I need to make it 92 characters long and keep the extensions.
For example, I have this filename:
IncrediblyLongFileNameIAmSorryForThisItLooksLikeSomeDamnSpamJesusIAintEvenCloseTo100yetalmostwaitforitYEAH.omg
The above filename is 110 characters. I need to keep the extension, therefore the program should rename the file as this:
IncrediblyLongFileNameIAmSorryForThisItLooksLikeSomeDamnSpamJesusIAintEvenCloseTo100yetalmos.omg
So far, my main problem is that I don't know how to work with filename strings in batch. I used this code:
#echo off & setlocal enableextensions
FOR /R %%i IN (*.*) DO (
ECHO %%~nxi
FOR /f "delims=:" %%a in ('
^(echo."%%~nxi"^& echo.^)^|findstr /o .'
) DO set lenght=%%a-5
echo The length of "%%~nxi" is %lenght%
)
endlocal & goto :EOF
But I can't SET inside a FOR, and it can't do basic math either (i.e. it can't do the -5 operation).
The second thing, which I believe should be easier once the first one is done, is simply to compare all the filenames in the folder (recursive, once again) and make sure no filenames are the same. If the program finds any filenames that are the same, the second occurrence should be renamed to add something like l1l at the end. (I can't use parentheses here, therefore I use two ls instead to cover the number.) The only thing you need to take care of is the file extensions, because I can't add anything after the file extensions, lest they become unusable.
Can anyone offer explanations for how to accomplish this? I would really like to be able to work this out myself, but I simply lack experience in batch programming.
#ECHO OFF
SETLOCAL
SET "sourcedir=c:\sourcedir"
SET "tempfile=%temp%\##fn##.92"
ECHO ::>"%tempfile%"
FOR /f "delims=" %%a IN (
'dir /s /b /a-d "%sourcedir%\*" '
) DO (
SET "fullname=%%a"
SET "name=%%~na"
SET "ext=%%~xa"
CALL :chgname
)
del "%tempfile%"
GOTO :EOF
:chgname
:: Proposed new name part - first 92 characters of existing name
:: also prepare for adding modifier
SET "newname=%name:~0,92%"
SET /a modifier=0
:modl
:: See whether this name has already been found
ECHO %newname%%ext%|FINDSTR /b /e /i /g:"%tempfile%" >NUL
IF ERRORLEVEL 1 GOTO makechange
:: existing name - modify it
SET "newname=%name:~0,92%#%modifier%#"
SET /a modifier+=1
GOTO modl
:makechange
IF "%name%" NEQ "%newname%" ECHO REN "%fullname%" "%newname%%ext%"
>>"%tempfile%" ECHO %newname%%ext%
GOTO :eof
Reasonably simple problem.
Get a directory-list in basic form (full-filename only) and apply the full filename, name part and extension part to appropriately-named variables.
Manipulate the filename to a new name consisting of the first 92 characters of the original name part. Anticipate the need to modify this new name by establishing a modifier to optionally be applied.
See whether the proposed new name already exists in the temporary file of NEW names already processed. If not found on that file, safe to rename (if required) and record name used.
If the filename has already been used, modify it to the original first 92+ "#anumber#", increment the modifier in anticipation and try again.
Only two comments required further - first, I used # rather than ! because ! has a special meaning to batch. Second, writing :: to the tempfile (the name of the tempfile is irrelevant - I chose one that's unlikely to exist...) means that findstr doesn't complain because the file is empty, but :: can't possibly be a real filename.
The /b /e /i options to findstr mean that the name echoed in must exactly match a line (matches both /b - begin and /e - end) but /i - case is irrelevant.

create loop in batch for multiple export with ImageMagick

Im trying to make a batch file that process multiple image files.
The files are named P_1316_0001.png P_1316_0002.png P_1316_0003.png etc..
LETs sqy P_1316 is VARSESSION)
thanks to the ImageMagick program i can convert an image directly with batch.
Basically what i want to do is :
Create a variable that would increment until there is no more file to convert in the folder. Comvert the file from png to jpg (convert %VARSESSION%%i%.png %VARSESSION%%i%.jpg) assuming that i is 0001
Well i hope you can help me.
i thank you
Daniel
You're being a little restrictive with your question, but to suit the precise parameters you've specified,
SETLOCAL ENABLEDELAYEDEXPANSION
for /l %%i in (10001,1,19999) do (
set numb=%%i
ECHO if exist %varsession%_!numb:~1!.png convert %varsession%_!numb:~1!.png %varsession%_!numb:~1!.jpg
)
ENDLOCAL
But there are much better ways, such as
for /f %%i in ('dir /b /a-d %varsession%_*.png') do (
ECHO convert %%i %%~ni.jpg
)
Assuming that all files matching %varsession%_*.png are to be processed.
Note: keyword ECHO inserted to SHOW what the batch proposes to do. Remove the ECHO to actually perform the action

Resources