Script to batch rename based on first character of original filename? - batch-file

we are OCRing all our sales order in our office. Pdf files being Created based on captured Data. The files looks like these S123456.pdf, S239463.pdf. some times the OCR software "reads" the "S" as a "5". (files end up created as 5123456.pdf.) I am looking for a Batch file for a windows command prompt environment that would rename only the files with the first character that starts with "5", rename it the "S" and leaving the rest intact. preferred to it apply to all sub-folders. I google around tried to modify some examples.. Can't get them working.. Please help!

A simple wildcard rename will actually do exactly what you want:
ren 5*.pdf S*.pdf

This should work - it will only echo the ren commands to the screen so if it looks ok then remove the echo
#echo off
setlocal enabledelayedexpansion
for /r %%a in (5*.pdf) do (
set "name=%%~nxa"
echo ren "%%a" "S!name:~1!
)

Related

Apply batch OCR through command line

I am totally new to batch scripting for cmd (Windows).
I have installed tesseract to work as a command line OCR tool.
Now I would like to run OCR on 100 images that I have stored in a folder.
How can I do it with batch ?
The command to run tesseract on an image and return the OCR text in a text file is:
"C:\OCR\tesseract" "C:\Image_to_OCR.jpg" "C:\out"
More information: http://chillyfacts.com/convert-image-to-text-using-cmd-prompt/
As you can see, I would probably need to make a for loop whith automatically iterates through the number of pictures and changes the name of the picture in the command accordingly and of course also the output name of the text file... but I don't know how to do it.
Any help would be very appreciated !
EDIT:
As suggested in the answer by Stephan, I could write:
for %%A in (C:\*.jpg) do "C:\OCR\tesseract.exe" "%%~fA" "C:\out"
However, the command line (cmd) only apears quickly and closes imidiatley and nothing happens.
My files are not directly in C:\ but in "C:\Users\James\Desktop\", therefore I wrote the command as such:
for %%A in (C:\Users\James\Desktop\*.jpg) do "C:\OCR\tesseract.exe" "%%~fA" "C:\out"
...but as said before, it does not work somehow.
Also, can I change the output txt name to be the same as the input image name, like so ?
for %%A in (C:\Users\James\Desktop\*.jpg) do "C:\OCR\tesseract.exe" "%%~fA" "%%~fA"
This worked :
I got two great answers! Thanks a lot. The final thing that worked was a mix between both answers:
#Echo off
PushD C:\Program Files (x86)\Tesseract-OCR || (Echo couldn't pushd C:\OCR & Exit /B 1)
for %%A in ("C:\Users\EPFL\Google Drive\EDx PDF Maker\Cellular Mechanisms of Brain Functions\Slides\1\*.jpg") do tesseract.exe "%%~fA" "%%~dpnxA"
I don't know your program C:\OCR\tesseract.exe but I assume it needs supporting tools/files present in the C:\OCR folder, so either you have to set that folder as the current one or have it contained in your path variable.
#Echo off
PushD "C:\OCR" || (Echo couldn't pushd C:\OCR & Exit /B 1)
for %%A in ("C:\Users\James\Desktop\*.jpg") do tesseract.exe "%%~fA" "%%~dpnA.txt"
The "%%~dpnA.txt" will save the text with same drive/path/filename and extension .txt
Use a for loop to iterate over the files:
for %%A in (C:\*.jpg) do "C:\OCR\tesseract.exe" "%%~fA" "C:\out"
%%A is the filenames (one at each run of the loop),
%%~fA is the fully qualified filename (just to be sure).
Read the output of for /? to learn more about those modifiers.
Note: this is batchfile syntax. To use it directly on command line, replace every %% with a single %

Recursive rename isn't working at all in batch

So I have been searching in other topics how to rename files and folders in a .bat in a recursive way but it isn't working at all.
My code is:
# echo off
setlocal enabledelayedexpansion
set /p rut="Introduce folder: "
FOR /D /R %%x in ("%rut%"\*) DO (
cd %rut%
echo %cd%
pause
ren .\* "a"
)
exit
But this only renames the files that are on the first folder and not in the rest. Forfile won't work at all with the variables.
What I want to get is how I could rename everything inside the main folder (including subfolders) whatever it is, be it files or folders as "a" to solve the Windows problem of having routes way too long when trying to delete a full structure, that is why I can't use the ".txt" ".whatever" solution.
A powershell solution would be valid too!
Thank you very much
Edit I added a random to see if it was the name that was conflicting but no, it is still not working:
ren .\* "a%RANDOM%"
And renaming them from cmd works the same way, I mean, if I write ren "folder" "whatever" it will change but in the script doesn't work with "*"

SIMPLE :: XCOPY NOT PRODUCING OUTPUT IN .bat

I'm a mortgage loan guy in Seattle, WA and I frequently set up a folder hierarchy into which I save a client's documents and as they come in to me. I've been creating these manually for years and I'd like to save the 3 to 4 minutes it takes to set these up by using a batch file.
So... I have a default set of folders, some of which contain a couple of small Adobe PDFs. What I'd like to do (and cannot make happen) is to run a batch file that would facilitate some custom remarks or input from me during the batch so that with a click and a couple of keystrokes, I have an organized folder setup for a new client within seconds rather than minutes.
I've written the following but it isn't producing any output folders or files.
______not sure character terms show correctly - see linked images below for actual______
#echo off
::Ask
echo Your Source Path:
set INPUT1=
set /P INPUT1=Type input:
echo Your Destination Path:
set INPUT2=
set /P INPUT2=Type input:
C:\WINDOWS\system32\xcopy.exe /e /v %INPUT1% %INPUT2%
My responses were:
to the first prompt "E:\DV8333 MY DOCUMENTS\002 ATLAS\ATLAS RESOURCES\000NEWCLIENTFOLDER2014"
to the second prompt "E:\DV8333 MY DOCUMENTS\001 CLIENTS\"
I have verified that xcopy.exe is in fact located as indicated above.
I'm on XP SP3
My actual paths and .bat file are shown in the linked image for clarity.
http://www.avidrecording.com/images/01.png
Thanks in advance, much appreciated.
#Rem save this as a .bat file and run
#echo off
set /P source=Enter Source Folder:
echo %Source%
set /P destination=Enter Destination Folder:
echo %destination%
xcopy %source% %destination% /v /i /e
Fundamentally, your problem appears to be that the xcopy command can't figure out which of the data it is receiving is parameters and which switches and which superfluous because you have spaces in your directorynames.
Fortunately, the cure is simple. "quote your parameters"
C:\WINDOWS\system32\xcopy.exe /e /v "%INPUT1%" "%INPUT2%"
Now - the path to xcopy.exe is probably superfluous - as is the extension, so
xcopy /e /v "%INPUT1%" "%INPUT2%"
is more than likely all you'd need.
(I'd caution to experiment with a throw-away destination until you've perfected your method. I use a RAMDISK...)
Also, if you're copying a template, then there's no apparent reason for all the folderol about inputting Input1. If you have more than one template set, set up a separate batch and shortcut for each one with a fixed template path, eg
xcopy /e /v "E:\DV8333 MY DOCUMENTS\002 ATLAS\ATLAS RESOURCES\000NEWCLIENTFOLDER2014" "%INPUT2%"
Note the use of quotes to defeat the evil spaces.
Next, your destination could be built, but may contain spaces. For instance, you may have a client to which you wish to refer as "037 - Fred Nurk". Now it's a pain to have to type in the full path, so make it easy. Just type in the 037 - Fred Nurk part and let batch fill in the rest.
xcopy /e /v "E:\DV8333 MY DOCUMENTS\002 ATLAS\ATLAS RESOURCES\000NEWCLIENTFOLDER2014" "E:\DV8333 MY DOCUMENTS\001 CLIENTS"\"%input2%"\
Note that this will append the input as a directory under E...001 clients. Note that the strings are concatenated and the double-quotes are there solely to tell batch "here be a string that may contain spaces."
If this works, and there's no reason why it wouldn't (does for me...) then all you'd need to do is enter the client details and the template would be copied to a new directory. Now actually playing around with the data in the files that are copied so that they are customised - well, at the price, that would be worthy of another question...
#echo off
echo Backing up file
set /P source=Enter source folder:
set /P destination=Enter Destination folder:
set /P Folder name=Enter Folder name
set xcopy=xcopy // Set the switches as per your need
%xcopy% %source% %destination%
pause

Batch file rename reading old file name and new file name from a txt file

I have done some searching and reading but I have not been able to find a solution. I apologize if this issue is already answered on the forum. Here is what I want to do:
I have many 1000's of files that I need to rename. I can easily create a .txt file that lists the old file name and the new file name as follows:
oldfilename1.ext newfilename1.ext
oldfilename2.ext newfilename2.ext
oldfilename3.ext newfilename3.ext
etc......
The actual file names will be as follows:
TKP-RL-MB-00205-001.pdf SDY-352-20009SA10BA-RXW03-001.pdf
TKP-RL-MB-00060-004.pdf SDY-352-20010SA10BA-RXW03-004.pdf
etc....
I would like a batch file that reads the two names from the txt file and renames the file from the old name to the new name. The batch file will be located in the same directory as all the files with the old file name. I don't need functionality to check if the old file exists unless that functionality is essential to the batch file working.
I am working in Microsoft Windows 7 so I have cmd and Powershell at my disposal.
Your help is greatly appreciated.
Thank you
This should do what you have described. Remove the echo after the do when you have tested it - atm it will merely print the commands to the screen.
#echo off
for /f "usebackq tokens=1,*" %%a in ("file.txt") do echo ren "%%a" "%%b"
pause
for /f "tokens=1,2" %%a in (yourtextfile.txt) do ECHO ren "%%a" "%%b"
-assuming that the only spaces in the file are those separating the names.
Reduce %% to % to execute directly from the prompt.
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.
If you have that textfile, you might just put a rename at the beginning of each line and save it as .bat.
(E. g. with Notepad++, Search Mode = Extended, replace \n with \nrename, maybe check first and last lines)

Windows batch way to replace all files in subdirectories with singular file (copy, rename all files)

I have a good command over cmd commands, but this may require a variable or a loop which is where I fail in batch commands. Please help if you can!
-- Have about 100 subdirectories each has 1-20 HTML files in it. There are about 100 HTML files in the root directory too.
-- Need to replace all HTML files in the above directories with the same HTML source (copy over existing file and keep the name the same). Basically trying to replace all existing files with a redirect script to a new server for direct bookmarked people. We are running a plain webserver without access to server-side redirects so trying to do this just by renaming the files (locked down corp environment).
Seems pretty simple. I can't get it to work with wildcards by copying the same file over to replace. I only get the first file replaced, but the rest of the files will fail. Any one with any advice?
This should do it from the command prompt. Replace % with %% for use in a batch file.
for /r "c:\base\folder" %a in (*.html) do copy /y "d:\redirect.html" "%a"
Without knowing more precisely how you want to update the file content I suggest the following rough approach.
To re-create your example, I had to create some folders. Run this command to do that:
for /l %i in (1,1,20) do mkdir fold%i
I then used this script to create some example files:
#echo off
set number=0
for /d %%i in (c:\Logs\htmltest\*) do call :makefiles %%i
goto :EOF
:makefiles
set /a number+=1
touch %1\file%number%.txt
echo %number% >%1\file%number%.txt
I then used this script to append the text changed to the file. Not sure if that is what you wanted - probably you need something more sophisticated.
#echo off
set number=0
for /d %%i in (c:\Logs\htmltest\*) do #for %%f in ("%%i\*.txt") do call :changetext %%f
goto :EOF
:changetext
echo changing file contents to ^"changed^" for file: %1
echo changed>>%1

Resources