How to bulk rename files within subfolders - using CMD command prompt - file

I am quite new to Command Prompt (Windows), however I have used it to do some file extensions changing and it is extremely helpful. I know nothing on coding, and so i am simply going off what I have read about but I am looking for a command line that I cant seem to find anywhere. I have folder, and inside that folder are 70 sub folders each labelled by a number from 1-70. Inside these subfolders are roughly 20 png picture files, that are currently numbered by a number from 1-20 in png format. I am looking for a command line to rename each file from its original name to "folder name (page number).png"
For example, I have folder called '68' and inside that folder is 1.png, 2.png, 3.png. I want the command line to change that 1.png and 2.png etc... to 68 (1).png and 68 (2). png, noticing the space between the bracket and the folder name. Sorry if i have made it confusing but I would really appreciate it and I have got some very helpful and quick answers from StackOverflow before
Thankyou if you are able to help me, as i am completely hopeless on this matter.

Only run this once - launch it from the folder containing the 70 folders.
#echo off
for /f "delims=" %%a in ('dir /ad /b') do (
pushd "%%a"
for /f "delims=" %%b in ('dir /a-d /b') do ren "%%b" "%%a (%%~nb)%%~xb"
popd
)

I am not a very experienced bash scriptor, but I guess this should do the task for you. I am assuming that you are using Linux operating system. So open a text editor and copy the following:
#!/bin/bash
NumberOfFolders=70
for((a=1; a <= NumberOfFolders ; a++))
do
cd ./$a
b=1
while [ -f $b.png ]
do
mv "$b.png" "$a($b).png"
let b=b+1
done
cd ..
done
save this script where you have you folders 1-70 (and call it whatever.ssh). Then open a terminal and write down chmod a+x whatever.ssh and after that ./whatever.ssh. This should do the job as you presented in your question.

A slight modification of the approach #foxidrive suggested, so the script can be run from anywhere and won't have to parse dir output:
#echo off
for /r "C:\root\folder" %%d in (.) do (
pushd "%%~fd"
for %%f in (*) do ren "%%~ff" "%%~nd (%%~nf)%%~xf"
popd
)
Note that this will not work in Windows XP (not sure about Windows Vista), due to a bug with the expansion of * in the inner for loop that would cause the files to be renamed multiple times.

Related

7zip creating empty archives with batch file

I want to zip everything in a folder, EVERYTHING, but into individually named archives. For some reason every solution on the internet only zips folders, or fails to work at all.
Currently, I have
for /d %%X in (*) do "c:\Program Files\7-Zip\7z.exe" a -mx "%%X.zip" "%%X\"
Which I interpret to mean
for = initiate a loop
/d = I don't know what this means
%%X = I don't know what this means
in = not sure, I think it means current directory
(*) = I don't know what this means
do = execute the next thing in "..."
"C:\Program Files\7-Zip\7z.exe" = the thing I want done.
a = add to archive
-m9 = max compression
"%%X.zip" = make it a zip file, though I still don't know what %%X is.
"%%X\" = even if I knew what "%%X\" meant I don't know why it's here.
I have figured out replacing %%X gives the archive a name, so it seems to copy the name of the thing being targeted.
So if I guess, I think /d is "target folders" and %%X is the name.
So
for /d %%X in (*) do "c:\Program Files\7-Zip\7z.exe" a -mx "%%X.zip" "%%X\"
Says in English as I understand it: for every folder name in the current directory, use 7z to max compress it into a zip of the same name... except I don't know what to replace /d with to make it target files instead of folders. And targeting specific extensions would be even better.
I tried googling what does "/d in cmd mean," "what does %%X mean", etc. I don't seem to be getting correct results in the search, I think I'm being too vague for google.
UPDATE:
for %%i in (*) do "c:\Program Files\7-Zip\7z.exe" a -mx "%%i.zip" "%%i\*.*"
seems to make a zip file NAMED each item in the folder, but does not actually add any files to them. I tried adding /f, but it didn't work at all when I did that.
Additionally, the first time I posted this it was closed as a duplicate of Batch script loop which has almost nothing to do with my problem. Yes, I have a loop, yes that addresses batch loops, but no, it does not come close to solving my problem since my problem isn't with the loop itself., or if it does I have absolutely no idea why or how. So please, explain it to me. I did see the section where it says %%X is the variable, but that just means I suppose X could be anything I want it to be, and since in my update I indicated a secondary issue, I think the problem I'm having is with 7z and not the bat file.
The for command can be very confusing at first, however there is many videos and sites that show examples of how to use it.
To start with try for /? to see the built in help information.
The below code should be what you are after if i understand your requirements correctly.. if not then this should help you understand the principles and adjust for your needs.
for /F "tokens=*" %%a in ('DIR /A-D /B /ON "C:\Test Folder\"') do (
echo Processing File: C:\Test Folder\%%a
"C:\Program Files\7-Zip\7z.exe" a -mx1 -tzip "C:\Test Folder\%%a.zip" "C:\Test Folder\%%a"
echo.
)
pause
Now lets go through it line by line...
for /F "tokens=*" %%a in ('DIR /A-D /B /ON "C:\Test Folder\"') do (
/F Tells it to work with files instead of directories(/D)
in (' ') Contains the command that finds the filenames. Note the ' marks at each end of the command...they are important.
%%a Represents each value of a filename that the in() command passes it.
do ( ) What it should do with each %%a value it receives.
Now we get to the command that is finding the filenames and passing to the do () section as %%a
('DIR /A-D /B /ON "C:\Test Folder\"')
You can find more info on the this by running dir /?
/A-D Tells it to ignore Directories... only list files.
/B Output just the file names and not anything else like sizes or it will confuse the for command.
/ON Feed the filenames to the do() command in alphabetical order
"C:\Test Folder\" The folder that contains all the files we want to list/perform actions on. Make sure you include the trailing slash. Also use the "" marks or it wont work if a folder has a space in its name.
Write(echo) to the screen a message showing which file it is about to do.
echo Processing File: C:\Test Folder\%%a
You can find more information on how to use the 7zip command line functions at https://sevenzip.osdn.jp/chm/cmdline/syntax.htm
"C:\Program Files\7-Zip\7z.exe" a -mx1 -tzip "C:\Test Folder\%%a.zip" "C:\Test Folder\%%a"
"C:\Program Files\7-Zip\7z.exe" Run this program.... and pass it the following information (arguments/switches)
a Add files rather than extract, etc.
-mx1 Use the fastest compression method...but less space efficient.
-tzip Use the ZIP compression format
"C:\Test Folder\%%a.zip" Save the new "zip" file as this. (%%a will be replaced with the name of the current file... aka ChuckNorris.mpg which would end up as ChuckNorris.mpg.zip)
"C:\Test Folder\%%a" This is the actual file you want to zip.

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 %

Search for filenames

So I am trying to code an "A.I." through batch. The user will be able to enter in information, and the program will track keywords, and compile all data onto one text file in a specific folder. I have almost everything but a line that searches for all the filenames with a certain keyword. Here is an example. User: "Tell me EVERYTHING you know about GEORGE WASHINGTON" Program: Searches through memory for folder "George Washington" then gathers all the .txt files from that folder. It then types everything from those files to the user. Pls help, thanks :-)
you can do:
#echo off
set "foldertosearch=George Washington"
cd C:\
for /d /r %%a in ("%fodertosearch%") do (
echo found folder: %%a
cd %%a
)
for /f %%b in (*.txt) do (
echo found file: %%b
type "%%b"
)
pause
i recommend you to change the C:\ of the 3rd line to a specific folder because i am not shure the for /d loop can handle a whole drive. use the where command instead.

.bat file to move multiple files from one directory to another and rename instead of overwriting

Forgive me if this is a simple question, but I've been out of practice for a few years!
I am creating a .bat file at work that will require me to move all files from one directory to another and if any conflicts arise, to add the date to the filename instead of overwriting it. All in all, I want to keep every file, even if they are duplicates. TIA for any help!
MD "C:\NewFolder"
CD "%userprofile%\desktop"
For /f "delims=" %%A in ('dir /a-d /b "%userprofile%\desktop"') do If not exist C:\NewFolder\%%A (copy "%%A" "C:\NewFolder\%%A") Else copy "%%A" "C:\NewFolder\%%A%date:/=%"
It ugly but it works and should get you started. See dir /?, for /?, and if /?. Note you have to change directories for it to work.

access second level subfolders in bat-File

I already looked at this site and others for an answer, but I could not find any, so here is my simple question. I have a dvd which has several folders (named after the respective years) in it. Inside each of those there are subfolders, which then again have subfolders. So it basically looks like this:
2001/Projectabc/sub1
/sub2
/sub3
2002/Projectdef/sub1
/sub2
.. and so on.
I now need a batchfile that listst me all the Projectnames without anything else and writes them inside the textfile, so that in the end I would have a fiule lookin like this:
Projectabc
Projectdef
So basically what I don't know is how do write the batch, so that it only lists the second level Foldernames.
I started like this:
dir [Directory of dvd]:\..\ /b /a:d >> C:\folderlist.txt, saved this as a bat file, executed it, opened my folderlist.txt and have
_01.2003
_01.2004
_01.2005
_01.2008
_01.2009
_01.2010
_01.2011
_01.2013
_01.2014
I really am stuck, since I am a newbie. Thanks very much everyone, who might help.
Let's assume your DVD is in drive G: (change the code to fit your scenario)
The following one liner will work from the command line:
>c:\folderlist.txt (for /d %A in (g:\*) do #for /d %B in ("%A\*") do #echo %~nxB)
Or using a batch file:
#echo off
>c:\folderlist.txt (
for /d %%A in (g:\*) do (
for /d %%B in ("%%A\*") do echo %%~nxB
)
)
On my machine I cannot write to the system root, so I would have to change the destination folder for the output.

Resources