access second level subfolders in bat-File - batch-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.

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 %

Batch script to find a hidden folder on a remote computer, list the last date modifed, and output to log

I am looking for a Windows batch script that I can run from my admin workstation that will find a specific hidden folder on the C: drive of remote machines, tell me if it exists or not, give me the last date modified, and output the results to a log file. The folder path is something like, "C:/ProgramData/Folder1/Folder2". The "ProgramData" folder is hidden and "Folder2" is the folder that I want all the details from (if it exists, last date modified). I would like to incorporate this with the use of PSEXEC if possible.
I have looked all around for something that will work and found some scripts that tell me if the folder exists or not; but I cannot seem to find my exact scenario. I am familiar with scripts but am no expert by any stretch of the imagination. Any help would be greatly appreciated!
Cheers
EDIT:
Okay here is what I have so far. I was able to use a command prompt to list the subdirectory that I was looking for on a specific computer by using the DIR command. It was something like this:
dir C:\ParentFolder\ChildFolder /ad /o-d /b
This shows a list of directories within the "ChildFolder" directory. The output would be something like:
dir C:\ParentFolder\ChildFolder /ad /o-d /b
folder1
folder2
folder3
folder4
So what I need now is to incorporate a way to just show the folder in this group that had the most recent modification. For example if "folder2" was the most recently modified folder in the group, I would like my command line to just display "folder2 04/08/14 04:13 PM
Take a look at dir /? then specifically /T. Also if /?, You need if exist, to check if a file or directory exists. The following will grab the date and time of a folder's last write, if it exists.
#echo off
set folder=C:\ProgramData\Folder1\Folder2
if exist %folder% (
for /f "skip=5 tokens=1,2" %%a in ('dir %folder%') do set writetime=%%a %%b & goto :break
) else echo %folder% Does Not Exist & goto :EOF
:break
echo %folder% %writetime%
To write the output to a file you can use the > (write) or >> (append) operators. echo Mate >> logfile.txt.

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

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.

Batch file to recursively move a single subdirectory from multiple parent directories TO another single, defined directory

If that title wasn't confusing enough.. hopefully what I am trying to do is a lot simpler to understand.
Windows 7 just in case it needs to be said.
I have multiple directories within the folder I am working in;
C:\WorkingDir\1
C:\WorkingDir\2
C:\WorkingDir\3
and so on
Within each of these folders (1,2,3,etc) is a single sub-directory and no other files or folders;
C:\WorkingDir\1\5E04AB
C:\WorkingDir\2\4F07FC
C:\WorkingDir\3\9DA04F
I need to move each of those single subdirectories from within the parent folder to a new folder;
C:\NewFolder\5E04AB
C:\NewFolder\4F07FC
C:\NewFolder\9DA04F
That's it! I thought it might be simple, but I can't wrap my head around the variables or a better resource explaining how to use them. I don't use batch files much if at all, so I am very sorry for this cry for help. Hopefully someone more knowledgeable has a simple explanation that can help me out :-)
I found this: http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/percent.mspx?mfr=true
But can someone link me to a resource where I can learn more about batch variables and parameters for future reference?
Thank you thank you thank you
Update:
#endoro
Thanks for your response. There must have been a user error the first time I tried to run your code. It is working properly and all is well! Thank you so much!
Update 2
After running the code User1 contributed, It will create my NewFolder directory, but will not copy anything to it. It remains empty. Here is some of the repetitive output in DOS when I run it:
C:\WorkingDir>(
set fldr2=C:\WorkingDir\1\5E04AB
move "C:\WorkingDir\\" "C:\NewFolder\"
)
The system cannot find the file specified.
C:\WorkingDir>(
set fldr2=C:\WorkingDir\2\4F07FC
move "C:\WorkingDir\\" "C:\NewFolder\"
)
The system cannot find the file specified.
C:\WorkingDir>(
set fldr2=C:\WorkingDir\3\9DA04F
move "C:\WorkingDir\\" "C:\NewFolder\"
)
The system cannot find the file specified.`
Please look at the output and remove the echo, if it is OK:
#echo off &setlocal
set "workingdir=WorkingDir"
md "C:\NewFolder" 2>nul
pushd "%workingdir%"
cd
for /d %%i in (*) do for /d %%j in ("%%~i\*") do echo move "%%~fj" "C:\NewFolder\%%~nxj"
popd
I haven't been able to test this but:
#echo off
setlocal EnableDelayedExpansion
md "c:\NewFolder\"
cd "C:\WorkingDir\"
for /D /r %%a in ("*") do (
set fld1=%%a
cd "%fld1%"
for /D /r %%b in ("*") do (
set fld2=%%b
move "c:\WorkingDir\%fld1%\%fld2%" "C:\NewFolder\%fld2%"
)
)
And a good resource for batch:
http://ss64.com/nt/

Comparing Folders and Subfolders files using batch command

I have two folders. I want to compare the files inside these folders.There are some subfolders in root folders. These sub folders are also containing some files. I want to compare these files in subfolder also. For that I am trying to write a batch file. Any kind of solution will help me a lot
I would not use a batch file at all to accomplish this task. BeyondCompare is here to do exactly that, and does it seemingly well.
Now on the other hand you really wanna do it via batch file, I'd suggest you install a tool called diff tools, and you'll be able to do something like:
diff.exe <file1> <file2> <htmlfile>
On the command line.
hope it helps
UPDATE
As a follow up to your comment, I write this, which also seems to work for me, and doesn't use any external tool. This is a simple example, but you can make it better.
if exist compare.log del compare.log
if exist missing.log del missing.log
for /f "delims=" %%a in ('dir/b/a-d c:\test\1') do (
if exist "C:\test\2\%%a" (
fc "c:\test\%%a" "C:\test1\%%a" >> compare.log
) else (
echo %%a is missing >> missing.log
)
)
Pause
try dir /b /s
to search for files within subdirectory, instead of dir /b

Resources