Use of 7zip "exclude wildcards" in batch file - batch-file

I have a batch file for project backup, that i want to modify to exclude unnecessary files.
My current file uses simple 7z command to pack whole folder into one archive:
!7zipPath! a -t7z "archive_name" "path_to_add".
From what i found in other people questions, to exclude given files, i can use flag, for example -x!.txt, or -xr!.txt if path consist subfolders.
!7zipPath! a -t7z "archive_name" "path_to_add" -xr!*.jt
This should exclude all files with .jt extension (software specific extension), but -x!*.jt returns "incorrect wildcard typemaker *.jt
So from what i understand 7zip does not know this extension so i can not use it (is that correct thinking?)
So instead i tried to use include only. Answers from other question says that it should be simply added like this:
!7zipPath! a -t7z "archive_name" "path_to_add" ./*.xml
In my understanding this should create archive that includes only .xml files while keeping structure, but it just ignores that wildcard and put all the files from the path. It does not return any kind of error.
How can i use 7zip command options in batch file 7z command to get expected results?
PS; Will it work with extension on a folder? So that it would include whole folder.cojt for example?
EDIT: Full code
#echo off
setlocal EnableDelayedExpansion
SETLOCAL ENABLEEXTENSIONS
rem Get the time from WMI - at least that's a format we can work with
set X=
for /f "skip=1 delims=" %%x in ('wmic os get localdatetime') do if not defined X set X=%%x
rem dissect into parts
set DATE.YEAR=%X:~0,4%
set DATE.MONTH=%X:~4,2%
set DATE.DAY=%X:~6,2%
echo Today is: %DATE.YEAR%-%DATE.MONTH%-%DATE.DAY%
if not exist %DATE.YEAR%-%DATE.MONTH%-%DATE.DAY% (
echo Making Backup
For /F "Delims=" %%A In ('where /r "C:\Program Files\7-Zip" /f 7z.exe') do Set zipPath=%%A
IF NOT DEFINED zipPath (
echo Extending search range for 7zip...
For /F "Delims=" %%A In ('where /r C:\ /f 7z.exe') do Set zipPath=%%A
)
IF NOT DEFINED zipPath (
echo Extending search range for 7zip...
For /F "Delims=" %%A In ('where /r D:\ /f 7z.exe') do Set zipPath=%%A
)
IF NOT DEFINED zipPath (
echo 7zip not found on this PC
echo You can close this window now
cmd /k
)
md %DATE.YEAR%-%DATE.MONTH%-%DATE.DAY%
cd %DATE.YEAR%-%DATE.MONTH%-%DATE.DAY%
!zipPath! a -t7z "%DATE.YEAR%-%DATE.MONTH%-%DATE.DAY%_TuneCells" "M:\01\TuneCells\"
)
if exist %DATE.YEAR%-%DATE.MONTH%-%DATE.DAY% (
echo Backup already created %DATE.YEAR%-%DATE.MONTH%-%DATE.DAY%
)
PAUSE
As mentioned in one of commends, the search for 7z is there so that multiple people can easily use the code regardless of their installation path preferences (which are surprisingly wide)

Related

XCOPY copying files based on ID (With network paths)

I have created a batch-file which reads an ID from a CSV file and then searches for files with that ID contained within the title of log files created or modified within a specific date range to move them into a network folder.
At this moment, it seems the code is working (+-) but it is copying more files than it should, without looking at the ID.
It works on my local machine, so it seems the problem is regarding the network paths and it shows the UNC not supported path.
cd \\10.XX.ZZ.YY\ServersFiles
ECHO Will start reading the log files
for /f "usebackq tokens=4 delims=," %%a in ("\\10.XX.ZZ.YY\Folder1\Folder2\Execution_Logs\Tasks_Complete.csv") do (
for /r %%i in (*%%a*.log) do xcopy /Y /D:%date_arg% "%%i" "\\10.XX.ZZ.YY\Logs\"
)
This is what I'm getting right now
If UNC paths aren't allowed, you could try using PushD instead of CD, which will create a temporary drive mapping to the network location. Throughout the rest of your script you could then use relative paths to the required locations.
Example:
#Echo Off
SetLocal EnableExtensions DisableDelayedExpansion
REM Your %date_arg% variable should be defined somewhere around here.
PushD \\10.XX.ZZ.YY\ServersFiles 2>NUL || Exit /B
If Not Exist "..\Folder1\Folder2\Execution_Logs\Tasks_Complete.csv" Exit /B
Echo Will start reading the log files
For /F "UseBackQ Tokens=4 Delims=," %%A In (
"..\Folder1\Folder2\Execution_Logs\Tasks_Complete.csv")Do For /R %%B In (
"*%%A*.log")Do XCopy "%%B" "..\Logs\" /D:%date_arg% /Y
PopD
EndLocal
Exit /B

Tesseract-OCR to batch files in all subfolders, Windows cmd?

I am trying to use Tesseract-OCR to read and OCR all .png files, not only in current folder, (as there is answer for that) but also in all subfolders.
This works for folder:
for %%A in ("C:\Users\x\AppData\Local\Tesseract-OCR\temp\*.png") do C:\Users\x\AppData\Local\Tesseract-OCR\tesseract.exe "%%~fA" "%%~dpnxA"
I tried with this to go through all subfolders that I have in "temp" folder:
(for /r %%a in (*.png) do C:\Users\x\AppData\Local\Tesseract-OCR\tesseract.exe "%%~nxa" "%%~dpnxA")
but I got this errors for every file:
C:\Users\x\AppData\Local\Tesseract-OCR\temp>C:\Users\x\AppData\Local\Tesseract-OCR\tesseract.exe "01.png" "%~dpnxA"
Tesseract Open Source OCR Engine v4.1.0-elag2019 with Leptonica
Error, cannot read input file 01.png: No such file or directory
Error during processing.
It is obvious that the script finds all files in all of the subfolders, but then it cant read then for some reason?
Also, this script works for one folder, but when I try to use with /r it doesnt go through all subfolders:
:Start
#Echo off
Set _SourcePath=C:\Users\x\AppData\Local\Tesseract-OCR\temp\*.png
Set _OutputPath=C:\Users\x\AppData\Local\Tesseract-OCR\temp\
Set _Tesseract="C:\Users\x\AppData\Local\Tesseract-OCR\tesseract.exe"
:Convert
For %%A in (%_SourcePath%) Do Echo Converting %%A...&%_Tesseract% %%A %_OutputPath%%%~nA
:End
Set "_SourcePath="
Set "_OutputPath="
Set "_Tesseract="
Any ideas?
Perhaps this sort of thing is what you're looking for:
#Echo Off
SetLocal DisableDelayedExpansion
Set "_SourcePath=%LocalAppData%\Tesseract-OCR\temp"
Set "_SourceMask=*.png"
Set "_OutputPath=%LocalAppData%\Tesseract-OCR\temp"
Set "_TesserFile=%LocalAppData%\Tesseract-OCR\tesseract.exe"
For /F "Delims=" %%A In (
'""%__AppDir__%where.exe" /R "%_SourcePath%" "%_SourceMask%" 2>Nul"'
) Do Echo Converting %%A...& "%_TesserFile%" "%%A" "%_OutputPath%\%%~nA"
Note, this assumes that tesseract allows for specifying the output directory and accepts doublequoted strings etc. It also assumes that you intend for all output files to be placed in %_OutputPath%.
If you wanted them to be placed along side their respective .png's then perhaps this will do it:
#Echo Off
SetLocal DisableDelayedExpansion
Set "_SourcePath=%LocalAppData%\Tesseract-OCR\temp"
Set "_SourceMask=*.png"
Set "_TesserFile=%LocalAppData%\Tesseract-OCR\tesseract.exe"
For /F "Delims=" %%A In (
'""%__AppDir__%where.exe" /R "%_SourcePath%" "%_SourceMask%" 2>Nul"'
) Do Echo Converting %%A...& "%_TesserFile%" "%%A" "%%~nA"

Find a file within a for loop in command prompt

I am attempting to find files to run commands against in a command prompt (batch) script. So far, so good:
#ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
FOR /R %%I IN ("*.Marker.txt") DO (
SET MARKER=%%I
SET LOG= ???
ECHO MARKER IS !MARKER! AND LOG IS !LOG!
)
Basically, I want to iterate on a pair of files and execute a command on them (for now just echo). The problem I have is that the log file can be in any directory from where the *.Marker.txt file exists. I can't simply just say 100.Marker.log because that's just the filename, not the full path. Some examples:
C:\Temp\Samples\3A5\100.Marker.txt
C:\Temp\Samples\3A5\9\1\100.Marker.log
C:\Temp\Samples\39B\122.Marker.txt
C:\Temp\Samples\39B\2\5\122.Marker.log
So, not really sure what to do here.
You can use PUSHD/POPD to push/pop the working directory during the script and an inner loop.
FOR /R %%I IN ("*.Marker.txt") DO (
SET DIRECTORY=%%~DPI
SET FILENAME=%%~NI
SET MARKER=%%I
PUSHD !DIRECTORY!
FOR /R %%J IN ("!FILENAME!.log") DO (
SET LOGFILE=%%J
ECHO !MARKER!
ECHO !LOGFILE!
)
POPD
)
Not sure how to find a single value, the FOR technically will run on all matches, but if you say you should only have a single value than the inner loop will only run once per marker file. Maybe there's a more efficient way to search for a single file without using FOR?
If all of your files have the same format (text.Marker.ext), then you can use:
#echo off
setlocal EnableDelayedExpansion
for /R %%A IN ("*.Marker.*") DO (
set MARKER=%%A
for /F "tokens=1 delims=." %%B IN ("%%A") do (
set LOG=%%B
)
echo MARKER IS !MARKER! AND LOG IS !LOG!
)
Please note that I don't generally suggest capital letters in batch file.
We make a loop that loops through all subfolders (for /R) trying to find files containing .Marker. (IN ("*.Marker.*")).
We set the output to a variable (MARKER).
Now, we loop through filename found setting delims to .. This means that: token1.token2.token3. . won't be parsed as a token.
Set result to variable.
#echo off
setlocal enabledelayedexpansion
REM following lines to create test environment
md C:\Temp\Samples\3A5\9\1 2>nul
md C:\Temp\Samples\39B\2\5 2>nul
break>C:\Temp\Samples\3A5\100.Marker.txt
break>C:\Temp\Samples\3A5\9\1\100.Marker.log
break>C:\Temp\Samples\39B\122.Marker.txt
break>C:\Temp\Samples\39B\2\5\122.Marker.log
REM end creating test environment
for /R "C:\Temp\Samples\" %%a in ("*.marker.txt") do (
for /f %%b in ('dir /s/b "%%~dpa\%%~na.log"') do (
ECHO Textfile: %%a Logfile: %%b
)
)
The for /R looks recursively for all matching files (*.marker.txt in C:\Temp\Samples\ and subfolders.
The for /f looks for matching log files below the folder, where the marker.txt is.
Output:
Textfile: C:\Temp\Samples\39B\122.Marker.txt Logfile: C:\temp\Samples\39B\2\5\122.Marker.log
Textfile: C:\Temp\Samples\3A5\100.Marker.txt Logfile: C:\temp\Samples\3A5\9\1\100.Marker.log
Note: this assumes, there is only one matching .log for each .txt and it's within the tree below .txt

I need to create a list of folders that contain a specific file type with a batch file

Using a batch file I'm trying to generate a list of only folders within a location that contain a certain file type, let's call it *.abc
at the moment I only know how to echo a DIR command output to a file called folder.lst, I would like to expand on that and try to either
a) echo only folders containing the *.abc file type to folder.lst
b) remove references in folder.lst of folders that do not contain the *.abc file type.
I also tried having a FOR loop check each line to see if a *.abc file existed in that location and skip it, if not, but I just could not get that to work, here is an example of what I had.
setlocal enableextensions enabledelayedexpansion
FOR /F "delims=" %%C in (folder.lst) do (
set temp=%%C
if not exist !temp!\*.abc (goto skip) else (goto resume)
:resume
then my actions live here
:skip
)
but I am aware I am doing something wrong here...I just do not know what.
Maybe the /R form of the for command will help:
for /r "basedir" %%a in (.) do if exist "%%~a\*.abc" (
echo %%a contains .abc file(s)
)
The %%a will be the directories you want (with a trailing \., but you should be able to not care or accommodate this).
There are problems with such of the script as you have posted in that you can'y use labels within a block statement. You've also not provided any examples.
#ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
SET "sourcedir=U:\sourcedir"
SET "lastdir="
FOR /r "%sourcedir%" %%a IN (*.abc) DO IF "!lastdir!" neq "%%~dpa" (
SET "lastdir=%%~dpa"
ECHO %%~dpa
echo "!lastdir:~0,-1!"
)
GOTO :EOF
Each directory found will be echoed twice - one with the trailing \ and once without.
You would need to change the setting of sourcedir to suit your circumstances.
#echo off
setlocal enableextensions disabledelayedexpansion
set "root=%cd%"
for %%a in ("%root%") do for /f "delims=: tokens=2" %%b in ('
dir /a-d /s "%root%\*.abc" ^| find "\"
') do echo(%%~da%%~pnxb
This executes a recursive dir command searching for the indicated file type under the starting point (change root variable to suit your needs). For each found folder we retrieve the folder from the dir header that precedes the file list (the lines that contain a backslash).
To separate the path from the rest of the information in the line, the colon is used as delimiter. As this will leave the drive out of the retrieved information, an aditional for is used to retrieve the drive from the folder reference.
From the command line:
for /f "delims=" %a in ('dir /s /b *.abc') do echo %~dpa >> folders.lst
In a batch file:
for /f "delims=" %%a in ('dir /s /b *.abc') do echo %%~dpa >> folders.lst
The above commands will place only the folder names containing the *.abc files in folders.lst.
Notes:
% should be replaced by %% when the command is used in a batch file.
The ~dp part of %~dpa expands %a to a drive letter and path only. Remove the d if you don't want the drive letter. The p path includes a trailing \ which may be interpreted as an escape character by some commands.
The above commands start the search in the current directory. To search from the root of the current drive you can do cd \ first.
For more information see FOR /F Loop command: against the results of another command and Parameters.

How to rename a set of pdf files using a batch script

I know that there are several posts adressing this issue already. However I can't get my little batch script to work and I am a newbie so I would be very pleased if u could help me to solve that.
I have a bunch of pdf files named with a random number and "_text" e.g. 174098_text.pdf. Now I want to rename the file such that I only have 174098.pdf left (remove _text).
Here is my latest version of my file "Rename.cmd":
#echo off
#setlocal
REM +++++++++++++++++++++++++++++++++++++++++++++++++
REM ++++++++ Umbenennen von Dateien ++++++++++++++++
REM +++++++++++++++++++++++++++++++++++++++++++++++++
REM +++ Dateinamen und Pfad ermitteln
FOR /f "delims=" %%D in ('Dir /b %Path%\*_*.pdf') do (
FOR /f "delims=_ tokens=1-2" %%I in ('%%D') do (
ren %%D %%I.pdf
)
)
Endlocal
I hope you can help me and explain me what i have done wrong. Running the code it opens all the files but dosn't rename a single one of it.
/F parameter is for OPENING commands and/or get the output of a command, so when you use the second for /F you are telling the CMD to execute "%%D" file, so can't work that.
You can use a FOR without parameters, or a /R parameter if you need recursively a folder.
And you don't need to start setlocal for this job
#echo off
FOR %%# in ("C:\Folder\*.pdf") DO (
:: Set the "filename.extension"
Set "File=%%~nx#"
:: Rename it
Call Rename "%%#" "%%FILE:_TEXT=%%"
REM Explanation:
REM Rename "filename_text.pdf" with "Filename(_Text=NOTHING).pdf"
REM (That removes the "_TEXT" pattern in each filename)
)
pause&Exit
Remember, if you need recursive:
FOR /R "C:\Folder\" %%# in (*.pdf)

Resources