exclude *.lyx~ files in batch (windows) - windows

I just created the following batch file which saves all my lyx documents as tex files:
cd /d "D:\"
:: if the "for"-command is executed from the command line, just use "%" rather than "%%"
for /R %%g in (*.lyx) do "C:\Program Files (x86)\LyX 2.1\bin\lyx.exe" --force-overwrite --export pdflatex "%%g"
The problem is now that instead of the *.lyx files the batch uses the *.lyx~ files which are as far as I know some kind of backup files and don't contain the latest content.
How can I modify the batch file such that it takes the *lyx files rather than the *.lyx~ files?
Any suggestion would be great help.
Best :-)
PS: If I type this code in the command line (without using the batch), everything is fine.

#Edit1: added tokens option. Now it works in subdirs with spaces.
Modify your for loop to:
for /F "tokens=*" %%g in ('dir /b /s *.lyx') do if "%%~xg" equ ".lyx" (
"C:\Program Files (x86)\LyX 2.1\bin\lyx.exe" --force-overwrite --export pdflatex "%%g"
)
Another solution would be to use forfiles.
As for the 3rd comment, I think it does both but it does the .lyx~ at the end.
To see the output of a batch file simply launch it from a cmd window.

Related

Read a Folder's subfolders and files in these subfolders using Windows cmd

I am working on a batch script which reads a file and "find and replace" this character "╡" to "µ"
So far I was able to write following code:
#echo off
Setlocal enabledelayedexpansion
chcp 65001
Set "Pattern=╡"
Set "Replace=µ"
For %%# in ("E:\CopiedVault\*") Do (
Set "File=%%~nx#"
Ren "%%#" "!File:%Pattern%=%Replace%!"
)
The problem I am facing is how I don't know how this script can be modified ,by which it can read subfolders of folder "CopiedVault", and then can find and replace files and their names.
This script works perfect if it is able to find the file.
Try
dir /s /b E:\CopiedVault
inside a forloop and for each occurance rename the file.

Zip files with same name on Windows using batch script

Let's say we have:
somedir
file1.eps
file1.jpg
file2.eps
file2.jpg
What I want to do is, to get zips of every eps, jpg couple with the same names like:
somedir
file1.zip
file2.zip
I can't figure out how to do this. Any suggestions?
You can do it easily with help of WinRAR:
#setlocal
#echo off
set path="C:\Program Files\WinRAR\";%path%
forfiles /s /m *.jpg /C "cmd /c winrar.exe a -afzip "#fname.zip" "#fname.eps" "#fname.jpg"
pause
Here is my example with windows native zip.exe. It looks for only *.eps files inside of current directory (because you have pair of files with the same name and different extensions), then it extracts filename, adds .eps and .jpg extensions and zip them with same name.
for /r %i in (*.eps) do zip %~ni.zip %~ni.eps %~ni.jpg
If you write inside of script then white with %%i
for /r %%i in (*.eps) do zip %%~ni.zip %%~ni.eps %%~ni.jpg

Batch file to copy/rename directory recursively and find/replace file contents

A windows batch file is needed to make a copy of a directory tree and rename occurrences of a given old_name to a given new_name, please see example below.
I looked at robocopy and xcopy for no avail
String old = "old_name";
String new = "new_name";
current directory:
C:\old_name
C:\old_name\table
C:\old_name\garage\old_name\chair\a.file (contains text I am OLD_NAME)
C:\old_name\garage\old_name\b.file (contains text I am old_name)
desired outcome:
C:\new_name
C:\new_name\table
C:\new_name\garage\new_name\chair\a.file (contains text I am NEW_NAME)
C:\new_name\garage\new_name\b.file (contains text I am new_name)
edit:
showing one node in the path which is not being changed to the new name.
old_name=twintyone
new_name=one
C:\Users\fredJ\AndroidStudioProjects\one\app\src\main\java\mx\com\businessman\twintyone
I'm not aware of any single utility that will do everything you want, but if you deploy my JREPL.BAT and JREN.BAT utilities, then a simple batch script can easily and efficiently accomplish your goal.
Both JREPL.BAT and JREN.BAT are hybrid JScript/batch scripts that run natively on any Windows machine from XP onward - no executables need to be copied or installed.
The batch script has 3 simple steps:
Use XCOPY to copy your directory tree
Use JREN.BAT to appropriately rename OLD_NAME folders to NEW_NAME
Use FINDSTR to identify files that contain OLD_NAME, iterate the result with FOR /F, and edit each found file with JREPL.BAT
I also add a few ECHO statements so you can follow the progress
Note - the following is untested. If there is a bug, there should be a simple fix - the basic design is sound
#echo off
set "old=OLD_NAME"
set "new=NEW_NAME"
xcopy "c:\%old%\*" "c:\%new%" /i /s
call jren "^.+" "%new%" /d /s /p "c:\%new%" /fm "%old%"
for /f "delims=" %%F in ('findstr /mspc:"%old%" "c:\%new%\*"') do (
echo Editing "%%F"
call jrepl "%old%" "%new" /l /f "%%F" /o -
)
echo(
echo Done!

Command line to change file extension of all files under a folders and its subfolders

I am looking for a command line which can change the file extension of all the files that are under a folder and its subfolders. Is there any way to do this?
I tried with ren *.js *.txt but this only changes the file extension of the files under one folder.
I assume by ms-dos you really mean the command prompt on Windows. Not many people still use ms-dos.
The following will run your REN command on each folder within the hierarchy that contains .js files. It is probably a bit more efficient then running REN for each file individually.
for /r %F in (.) do #if exist "%F\*.js" ren "%F\*.js" "*.txt"
Double up the percents (%F becomes %%F) if run within a batch script.
You could try this:
For /R %G in (*.js) do REN "%G" "%~nG.txt"
Note that you'll need to use %% instead of % if running from a batch file.

DOS command to replace all instances of <filename>.config

i have an edited version of a config file specific for my machine.
i have the same config file in multiple different directories in my development folder.
i want to, in a single bat file, replace all instances of this file with my edited one.
So in pusedo code:
Take C:\edited.config and copy to C:\Projects\ /s wherever original.config is found
i want the final file to have the name of original.config, not edited.config
so i am guessing i need some combination of a FOR, a rename and copy or something like that
is this easier to do in Powershell?
can anybody help?
Thanks
I blogged about this a little bit ago at http://jamesewelch.com/2008/05/01/how-to-write-a-dos-batch-file-to-loop-through-files/
I think your solution will look something similar to (below is untested but used to show general idea)
for /f %%a IN ('dir /b *.config') do copy c:\master.config %%a
There's probably a switch there on the copy to suppress file overwrite warnings, but I don't remember what the switch is. This will copy your master.config and overwrite your local file (variable of %%a).
I'm amazed what DOS batch file experts make work. Since I'm not one of them, I take an approach that's pragmatic for me. It might work for you as well.
Get a list of destination folders
C:
Cd\
Dir original.config /s > original.bat
Edit original.bat in your favorite text editor (I like Notepad++)
Search for "original.config" and replace with "" (empty string)
Insert the text "Xcopy C:\edited.config " at the front of each line
Proof-read the result to be sure it's what you want. If you're not sure put an "Echo " in front of each line for a dry run.
Run the batch file.
#echo off
C:
cd \Projects
FOR /F "tokens=*" %%G IN ('DIR /B /S original.config') DO xcopy /y c:\edited.config %%G

Resources