Batch rename with outcome of other batch - windows

I'm trying to create a batch file, which scans documents for a barcode, and rename the file to the barcode.
I use two batchfiles for this. One of the batchfiles (test.bat) executes the exe that scans for the barcode:
#echo off
zbarimg.exe --raw -D -q %1
In the second batchfile, I want it to scan every file in the directory with a *.tif extension, scan for the barcode, and rename it to the barcodenumber. The second batch (rename.bat) looks like this:
for /f "tokens=*" %%a in ('dir /b *.tif') do (test.bat %%a)
My main question is, how can I incorporate a rename command, that renames the files to the outcome of test.bat.
In short, this is the proces:
1. there is a file called test.tif
2. the file is being scanned for a barcode (i.e. 123456789)
3. test.tif is renamed to 123456789.tif
I am wondering how I can capture the outcome of test.bat in a variable, so I can call that when I use the rename command?
Thanks!

The way to capture the output from test.bat is the same you used to capture the output from dir command: via a for /F command.
The simplest solution is eliminate the test.bat file and insert the zbarimg.exe program in the same Batch file:
for /f "tokens=*" %%a in ('dir /b *.tif') do (
for /F %%b in ('zbarimg.exe --raw -D -q %%a') do (
ren "%%a" "%%b.tif"
)
)
However, if you want to keep test.bat file, just do this:
for /f "tokens=*" %%a in ('dir /b *.tif') do (
for /F %%b in ('test.bat %%a') do (
ren "%%a" "%%b.tif"
)
)

The awkward way cmd has to get the result into a variable is to use a FOR loop.
SETLOCAL ENABLEDELAYEDEXPANSION
SET "BCNAME="
for /f "tokens=*" %%a in ('dir /b *.bat') do (
FOR /F "usebackq tokens=*" %%b IN (`CALL wc "bc.bat"`) DO (SET "BCNAME=%%~b")
echo REN "%%~a" "!BCNAME!.tif"
)
When it produces the correct REN command, remove echo.
Also, this makes no plan for multiple .tiff files to have the same barcode result.

Related

How to batch rename files in windows removing everything from them but set of listed strings?

I have went through a lot of guides for it, but havent found a way to do something similar to lets say turn all files in folder, like these files:
djhwu4s_cat_ruhg29.png
397y_dog_j0929_ej93.png
8yhh_owl.png
into these:
_cat.png
_dog.png
_owl.png
So basically removing everything from file names but a list of predefined strings i am searching for. In example above i would define list as "_cat", "_dog", "_owl". I know that each file will have only one of these variables, and there will be only one file with each of them in folder.
Will appreciate any tips on how to achieve that. Thanks in advance!
edit:
Here is what i came up with (with stuff i can understund) and what seems to be working fine now.
#echo off
setlocal enabledelayedexpansion
set v1=_cat-cat
set v2=_cat-owl
set v3=_cat
set v4=_dog
set v5=_owl
set v6=_horse
FOR /L %%a IN (1,1,6) DO (
rem echo %%a
rem echo !v%%a!
FOR /f %%f in ('dir /b /a:-D *!v%%a!.*') DO (
REN %%f !v%%a!.*
)
FOR /f %%f in ('dir /b /a:-D *!v%%a!_*.*') DO (
REN %%f !v%%a!.*
)
)
rem using two passes of this simpler code i can grasp and understund with dot and with underscore
rem after constructed variables value i make sure cat-cat is not recognised as and renamed to cat
rem no matter if im getting file with that variable as the last string before extension or another underscore
rem Gonna test it in combat now
For some reason this stuff doesnt work with files containing spaces and characters like:
"ab’c efg_dog.png"
FOR /L %%a IN (1,1,36) DO (
FOR /f %%f in ('dir /b /l /a:-D *!v%%a!.*') DO (
REN "%%f" "!v%%a!.*"
)
FOR /f %%f in ('dir /b /l /a:-D *!v%%a!_*.*') DO (
REN "%%f" "!v%%a!.*"
)
)
After further testing i have realised the problem starts with the %%f, and not the REN function as i thought. echo %%f before ren gives just the first part of the name to the first space, hence the REN function cant find the file. In case of "ab’c efg_dog.png" after finding the file with dir, the %%f becomes just "ab’c".
edit: After more tests and experiments and adding those "delims" to the code, the echo now shows proper, full names to be renamed, but it replaces that weird ’ character with ' for the REN command and thats why it still cant find the file to rename.
FOR /L %%a IN (1,1,36) DO (
FOR /f "delims=" %%f in ('dir /b /l /a:-D *!v%%a!.*') DO (
echo %%f
echo REN "%%f" "!v%%a!.*"
)
FOR /f "delims=" %%f in ('dir /b /l /a:-D *!v%%a!_*.*') DO (
echo %%f
echo REN "%%f" "!v%%a!.*"
)
)
#ECHO OFF
SETLOCAL
rem The following setting for the directory is a name
rem that I use for testing and deliberately includes spaces to make sure
rem that the process works using such names. These will need to be changed to suit your situation.
SET "sourcedir=u:\your files"
PUSHD "%sourcedir%"
FOR /f "delims=" %%e IN ('dir /b /a-d "*_*" 2^nul ^|findstr /v /b "_"') DO (
FOR /f "tokens=2delims=_." %%y IN ("%%e") DO ECHO REN "%%e" "_%%y%%~xe"
)
POPD
GOTO :EOF
For lack of examples, here's a start.
Always verify against a test directory before applying to real data.
Process the list of filenames that match *_*; find those names that do not start _, pick the second string between the delimiters _ and . and rename using that string (in %%y), prefixed by _ and appended with the original extension.
The ren command is simply echoed to the screen for verification. When happy, remove the echo before the ren to actually execute the rename.

Hide file not exist error in loop batch file

i have a batch script which i use to merge all the same name txt files from directories & subdirectories into one, here's my code:
#echo off
for /f "tokens=*" %%a in (textfilenames.txt) do (
for /D /R %%I in (%%a.txt) do (
type "%%I" >> merged.tmp
echo. >> merged.tmp
)
ren merged.tmp All_Combined_%%a.txt
)
)
#pause
and so when the loop doesn't finds the file on some directories then this msg is displayed:
The system cannot find the file specified.
The system cannot find the file specified.
The system cannot find the file specified.
Press any key to continue . . .
and i wanna hide above error and so i used >NUL in file name eg:
#echo off
for /f "tokens=*" %%a in (textfilenames.txt) do (
for /D /R %%I in ('%%a.txt^>NUL') do (
type "%%I" >> merged.tmp
echo. >> merged.tmp
)
ren merged.tmp All_Combined_%%a.txt
)
)
#pause
but i'm still getting error msg, i want to make this script completely silent like no error nothing or if somehow it's not possible then i wann customize the error to something like:
The system cannot find the example.txt specified. in the \Subfolder\Enigma\
etc!
If you do it right, you do not need to hide anything.
In this example we use the dir command with the /s function to search for files. It will not complain about files not found because it does not expect the file exist indefinitely in any given directory, it simply searches for it:
#echo off
for /f "useback delims=" %%a in ("textfilenames.txt") do (
for /f "delims=" %%I in ('dir /b/s/a-d "%%a.txt"') do (
(type "%%I"
echo()>>All_Combined_%%a.txt
)
)
)
#pause
Note, I eliminated the ren part as that is not needed. You can write to the combined file in the loop.
I also use echo( instead of echo. for reasons that can be found on numerous answers in SO.
Lastly, we can eliminate one parenthesized code block, by putting the second for loop inline with the first:
#echo off
for /f "useback delims=" %%a in ("textfilenames.txt") do for /f "delims=" %%I in ('dir /b/s/a-d "%%a.txt"') do (
(type "%%I"
echo()>>All_Combined_%%a.txt
)
)
#pause

Change the names of pictures in a folder using a text file

I have several pictures of my students in a folder, and a list with their names in a text file.
I would like to creat a batch file to rename the pictures using the text file (names.txt) so that every picture has the name of the student.
All the pictures are in .png format. I searched this site and tried the following code :
#ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
rem Load the list of new filenames
set i=0
for /F "delims=" %%a in (names.txt) do (
set /A i+=1
set "newname[!i!]=%%a"
)
rem Do the rename:
set i=0
for /F "delims=" %%a in ('dir /b /o:n *.png') do (
set /A i+=1
for %%i in (!i!) do ren "%%a" "!newname[%%i]!"
)
I creat the batch file in the folder and when I execute it, there is nothing happening.
I think it is not picking the right folder to work into, but I'm not sure.
Example of files:
1.png
2.png
3.png
Example of names.txt
1_john_dalton
2_carol_denvers
3_steve_austin
Based on your comments and the construction of the current code, it seems you want something like this:
#echo off
for /F "delims=" %%a in (names.txt) do (
for /f %%i in ('dir /b /o:n *.png') do echo ren "%%i" "%%a%%~xi"
)
This above will simply echo the result and not do the rename, only if you are happy with the results, can you remove echo from the last line, before ren.
Your example seems to want to resuse the old numeric name as well, so if indeed the case, then this should be it:
#echo off
for /F "delims=" %%a in (names.txt) do (
for /f %%i in ('dir /b /o:n *.png') do echo ren "%%i" "%%a[%%~ni]%%~xi"
)

How to compare file names in directory with file names in a text file?

I try to improve an overview of my search and find memories for batch files in Windows XP command prompt environment.
In order to my previous sentence I am not happy with my search possibilities and have to post a question.
I try to compare the names of some text files and have written words in a text file that are by reading the same. With such a start environment I wrote following batch script to get an echo output.
The aim is
#echo off
setlocal enabledelayedexpansion
for /f "delims=" %%b in ('dir "C:\A Folder"') do set var=%%~nb & echo !var!
rem The output is the name of the files without extension. Now my question:
rem Is it possible to compare the above file names with some input
rem from a text file, for example like:
for /f "delims=" %%b in ('dir "C:\A Folder"') do set var=%%~nb & for /f %%a in (Textfile.txt) do (if !var!==%%a echo good else echo search)
rem That returns no output. I would like to know if there are possibilities
rem to do that? And if it is possible, how to revise this batch file?
endlocal disabledelayedexpansion
pause
Have a nice day, wishes
Stefan
This should work with Latin characters - some foreign characters may not work:
#echo off
for /f "delims=" %%b in ('dir /b /a-d "C:\A Folder\*.*" ') do find /i "%%~nb" < "textfile.txt" >nul && (echo "%%~nb" found) || (echo "%%~nb" not found)
pause
proper formatting you code increases readabilty:
for /f "delims=" %%b in ('dir /b /a-d "C:\A Folder"') do (
for /f %%a in (textfile.txt) do (
if "%%~nb"=="%%a" ( echo good ) else ( echo search )
)
)
I added a /b to the dircommand (show name only, no date/time/attributes) and a /a-d to exclude directorynames.
You don't need to use a variable (!var!) here (but you can, it works fine).

How to batch rename files in a directory from a list of names in a text file

I'd like to use Power Shell or a batch file to rename several files in a folder based on a list in a text file. Essentially I want to append the file names with the author's last names (which I have stored in a separate text file).
E.g. Currently I have:
C:\myfiles
9-ART-2013.pdf
4-EGO-2013.pdf
2-ART-2013.pdf
My text file (in same order as files):
C:\myfiles
_Smith
_Jenkins
_McMaster
I want the files to be renamed as follows:
9-ART-2013_Smith.pdf
4-EGO-2013_Jenkins.pdf
2-ART-2013_McMaster.pdf
I've seen similar problems where people want to recursively rename files but they are always using a generic common appending element like adding an underscore or pre-pending with folder name, etc.
e.g. https://serverfault.com/questions/6268/easy-way-to-rename-all-files-in-a-directory-in-windows
In PowerShell it would be:
$names = Get-Content c\myfiles
Get-ChildItem C:\somedir\*.pdf | Sort -desc |
Foreach {$i=0} {Rename-Item $_ ($_.basename + $names[$i++] + $_.extension) -WhatIf}
If it looks like it will copy correctly, remove the -WhatIf.
Any of the above are likely workable solutions with just a little tweaking but the simplest thing to do was to simply create one text file with each row containing "the current filename"... a TAB... then "the filename I wanted".
9-ART-2013.pdf 9-ART-2013_Smith.pdf
4-EGO-2013.pdf 4-EGO-2013_Jenkins.pdf
2-ART-2013.pdf 2-ART-2013_McMaster.pdf
Then save the file as rename_list.txt and create a batch file with the following code.
for /F "tokens=1,2" %%a in (rename_list.txt) do ren "%%a" "%%b"
pause
You can delete the pause line once you get it tweaked and running correctly. Just copy the rename_list.txt and batch files to the folder with the files you want to rename and run the batch file. If you have a large folder with many files names you can get them in a text file by running a batch file with the following line.
dir /D > filename_scrapper_output.txt
It will create a text file filename_scrapper_output.txt that you can use to start your rename_list.txt file for above.
Another way to achieve the same thing:
#echo off
setlocal EnableDelayedExpansion
rem Load the list of authors:
set i=0
for /F %%a in (myfiles.txt) do (
set /A i+=1
set "author[!i!]=%%a"
)
rem Do the rename:
set i=0
for /F %%a in ('dir /b *.pdf') do (
set /A i+=1
for %%i in (!i!) do ren "%%a" "%%~Na!author[%%i]!%%~Xa"
)
EDIT: New method added
If the list of names file have the "OLD-Name NEW-Name" structure, then the code is much simpler:
for /F "tokens=1,2" %%a in (myfiles.txt) do ren "%%a" "%%b"
Note that the names must be separated by a space.
#ECHO OFF &SETLOCAL ENABLEDELAYEDEXPANSION
<Text.txt (
for /f "tokens=1*delims=_" %%a in ('dir /b /a-d /o-d *-*-*.pdf') do if "%%b"=="" (
set "xand="
set /p "xand="
echo ren "%%~a" "%%~na!xand!%%~xa"
))
Taking Endoro's and simplifying:
#ECHO OFF &SETLOCAL ENABLEDELAYEDEXPANSION
<texts.txt (
for /f "tokens=* delims=* " %%a in ('dir /b /a-d /o-d *-*-*.pdf') do (
set "xand="
set /p "xand="
ren "%%~a" "%%~na!xand!%%~xa"
))

Resources