Run script through all sub folders

Please check the FAQ (https://www.xyplorer.com/faq.php) before posting a question...
Post Reply
jdev21
Posts: 45
Joined: 08 Oct 2014 22:13

Run script through all sub folders

Post by jdev21 »

Does anyone know of a way to run a script through all subfolders of currently selected folder?
I currently have a batch script thats called that will convert image files using imagemagick. I would also like the ability to select for it to run through all subfolders but each prompt that opens needs to wait for the last to close otherwise it will not be able to convert.

highend
Posts: 14603
Joined: 06 Feb 2011 00:33
Location: Win Server 2022 @100%

Re: Run script through all sub folders

Post by highend »

There are more than one way to run through all files that are inside subfolders of the current folder.

Take a look at folderreport() to get a list of all files in subfolders.

Regarding the wait until something is finished:

The script command run has a wait parameter.
One of my scripts helped you out? Please donate via Paypal

bdeshi
Posts: 4256
Joined: 12 Mar 2014 17:27
Location: Asteroid B-612
Contact:

Re: Run script through all sub folders

Post by bdeshi »

Code: Select all

 $parentDir = <xynewitems>;
 $subDirs = folderreport("dirs", r, $parentDir, r);
Now $subDirs will contain a list of all subfolders of $parentDir, one on each line.
You'll then exec your converter script inside a foreach loop:

Code: Select all

 foreach ($sub, $subDirs, <crlf>){
  //foreach (image files in $sub){
   //run "conversion command";
  }
 }

waiting for a (cmd) prompt to close before continuing the script:

Code: Select all

 run "%ComSpec% /k echo Hello, Goodbye",,1 /*or 2*/,1; echo "next script command";
Icon Names | Onyx | Undocumented Commands | xypcre
[ this user is asleep ]

jdev21
Posts: 45
Joined: 08 Oct 2014 22:13

Re: Run script through all sub folders

Post by jdev21 »

so i created:

Code: Select all

"  Generate Sheet";
"-"
Run "C:\Sheet.bat";
"-"
"All Dir Test";
 $parentDir = <xynewitems>;
 $subDirs = folderreport("dirs", r, $parentDir, r);
  foreach ($sub, $subDirs, <crlf>){
  //foreach (image files in $sub){
   run "C:\Sheet.bat";
  wait 2000;
  }
 }
It runs the batch files twice in the same parent folder. I expect it to run 4 times - once for the parent, 2 for subdirectories, 1 for subsubdirectory.
I first tried without the wait but it was the same result.

highend
Posts: 14603
Joined: 06 Feb 2011 00:33
Location: Win Server 2022 @100%

Re: Run script through all sub folders

Post by highend »

Show us the content of the $subDirs variable.
One of my scripts helped you out? Please donate via Paypal

bdeshi
Posts: 4256
Joined: 12 Mar 2014 17:27
Location: Asteroid B-612
Contact:

Re: Run script through all sub folders

Post by bdeshi »

And $subDirs excludes the parent folder itself...
Icon Names | Onyx | Undocumented Commands | xypcre
[ this user is asleep ]

TheQwerty
Posts: 4373
Joined: 03 Aug 2007 22:30

Re: Run script through all sub folders

Post by TheQwerty »

Your run command does not specify the working directory so it will always run in the current path.
Try this instead:

Code: Select all

"All Dir Test"
  $parentDir = <xynewitems>;
  $subDirs = folderreport("dirs", r, $parentDir, r);
  foreach ($sub, $subDirs, <crlf>){
    run "C:\Sheet.bat", $sub;
    wait 2000;
  }
Other notes:
  1. The script you posted actually contains 3 scripts:
    1. Generate Sheet
    2. Run "C:\Sheet.bat";
    3. All Dir Test
    Remember that the indentation is what XY uses to tell scripts apart.
  2. There's no need to put a semi-colon after the caption/label of a script.
  3. Instead of using the wait command you can have the Run command wait until the batch file completes before continuing, by using its wait parameter.
You likely wanted some more like:

Code: Select all

"Generate Sheet"
  Run "C:\Sheet.bat";

"-"

"All Dir Test";
  $parentDir = <xynewitems>;
  $subDirs = folderreport("dirs", r, $parentDir, r);
  foreach ($sub, $subDirs, <crlf>){
    // Run C:\Sheet.bat in $sub and show a message box while we wait for it to complete.
    // Change 1 to 2 in the next line to wait without the message box.
    run "C:\Sheet.bat", $sub, 1;
  }
Hope that helps!

jdev21
Posts: 45
Joined: 08 Oct 2014 22:13

Re: Run script through all sub folders

Post by jdev21 »

Thank you for this. I will test it as soon as I get home.
After this properly runs through sub directories, I'll post the finished results with the batch files so that others can use it as well.

What I am doing:
I found that when an image is named 'folder.jpg' in a folder, that image will show as the folder thumbnail for the directory. I am using imagemagick and montage to create an index sheet containing the first 6 images in the directory, naming the index sheet folder.jpg, and then hiding the file. This produces a nice thumbnail image of photo directories in XYplorer. Eventually I will add options in the script for additional customization through a GUI.

There is probably an easier way to go about batch generating index sheets but I couldn't find anything this customizable. Also, I like being able to fully integrate it with XY.

klownboy
Posts: 4403
Joined: 28 Feb 2012 19:27
Location: Windows 11, 25H2 Build 26200.7171 at 100% 2560x1440

Re: Run script through all sub folders

Post by klownboy »

Hi jdev21, I've been using the folder.jpg trick to display the folder, but your montage file called folder.jpg sounds like a nice idea especially if done on the fly on multiple folders recursively. I certainly would want to do it manually. Please post when you get it done or ask if you need any help. You could probably use XYplorer scripting exclusively to run ImageMagick on those 6 (or whatever) image files to create the montage and eliminate the need for the batch file. There are plenty of command line examples of using IM with XY on the forum.

I have an old script that does nearly the same thing but uses Irfanview in lieu of IM. I searched but couldn't easily find it. I believe it was serendipity who wrote it, but I may be wrong. It uses 12 images, or 4 if 12 aren't available, though 12 may be too many visually as a thumbnail.

Edit: Here's the script I mentioned above (it was posted by serendipity) 5 years ago and it still works. Though it appears some improvements could be made in the scripting based on all the changes in 5 years. http://www.xyplorer.com/xyfc/viewtopic. ... ils#p32267 Essentially the same idea but using Irfanview.

jdev21
Posts: 45
Joined: 08 Oct 2014 22:13

Re: Run script through all sub folders

Post by jdev21 »

Sorry I haven't replied sooner, I got pretty busy.

I did finish up the script and I ended up changing it to produce 15 images instead of just 6 for a sheet. It is a fairly simple script (only about 15-20 lines) but requires manual editing if any changes are needed. I'll post it tonight when I get home but it may be harder to edit for those unfamiliar with command-line. I don't plan on creating any type of GUI for it. I currently have the option available (in XY) to only do the current folder or to do the current folder AND all subfolders.

The script works really well and will go through every sub-folder creating sheets. I found the original problem was using <xynewitems> (not grabbing the right directory). I had to change it to something like <curpath>... I don't remember the exact variable at the moment.
The only problem right now is that XY cannot really be used while its processing (and no easy way to stop it). A command prompt will pop up for each folder and disappear so as soon as a new command window pops up, it takes the focus. The good thing is that each folder only takes a fraction of a second to go through. It can do hundreds of folders in just a couple minutes.

I took a look at the script on the other page. I wish I would have found that sooner but I think I'm happy with what I've produced.

highend
Posts: 14603
Joined: 06 Feb 2011 00:33
Location: Win Server 2022 @100%

Re: Run script through all sub folders

Post by highend »

A command prompt will pop up for each folder and disappear so as soon as a new command window pops up, it takes the focus
run command, [directory], [wait=0], [show=1]

show = 0...
One of my scripts helped you out? Please donate via Paypal

jdev21
Posts: 45
Joined: 08 Oct 2014 22:13

Re: Run script through all sub folders

Post by jdev21 »

highend wrote:
A command prompt will pop up for each folder and disappear so as soon as a new command window pops up, it takes the focus
run command, [directory], [wait=0], [show=1]

show = 0...
Thanks for that highend. My next step is to add a progress bar.


folderjpg.xys

Code: Select all

"  Generate Image Index Sheet  (Current Folder)  |:refresh : THUMB_REFRESH";
  run "<xyscripts>\folderjpg.bat",.,0,0;
"-"
"  Generate Image Index Sheet  (Current Folder and ALL Subs)  |:refresh : THUMB_REFRESH"
  $parentDir = <curpath>;
  $subDirs = folderreport("dirs", r, $parentDir, r);
  foreach ($sub, $subDirs, <crlf>){
	run "<xyscripts>\folderjpg.bat",$sub,1,0;
  }
folderjpg.bat

Code: Select all

@echo off
setlocal
IF EXIST folder.jpg attrib -h folder.jpg && del /F folder.jpg
set /a "n=0, limit=15"
>"filelist.txt" (
  for /f "eol=: delims=" %%F in ('dir /on /b *.jpg *.bmp *.jpeg *.png') do (
    echo '%%F'
    2>nul set /a "n+=1, 1/(limit-n)"||goto :break
  )
)
:break
montage @filelist.txt -geometry "400x500+3+3" -background gray -tile 5x3 folder.jpg
attrib +h folder.jpg
del filelist.txt
exit
You will need to edit the montage line to produce the index sheet that you want. I have a high res monitor so i made mine quite large. Feel free to use the script as you like or offer comments/criticism. I have attached the files in a zip.
Attachments
folderjpg.zip
(962 Bytes) Downloaded 238 times

klownboy
Posts: 4403
Joined: 28 Feb 2012 19:27
Location: Windows 11, 25H2 Build 26200.7171 at 100% 2560x1440

Re: Run script through all sub folders

Post by klownboy »

Hi jdev21, that's an impressive use of DOS command line to obtain the filelist. As far as the output using IM montage though I think I'd rather obtain a folder montage without any gaps or borders. I may toy with the montage parameter's to adjust the output.
Thanks,
Ken

totmad1
Posts: 131
Joined: 24 Jun 2013 12:37

Re: Run script through all sub folders

Post by totmad1 »

Have had great fun creating scriptlets to achieve near same results,without bat file.

http://www.xyplorer.com/xyfc/viewtopic. ... 36#p114036
totmad1 (totally mad one)

jdev21
Posts: 45
Joined: 08 Oct 2014 22:13

Re: Run script through all sub folders

Post by jdev21 »

klownboy wrote:Hi jdev21, that's an impressive use of DOS command line to obtain the filelist. As far as the output using IM montage though I think I'd rather obtain a folder montage without any gaps or borders. I may toy with the montage parameter's to adjust the output.
Thanks,
Ken
To create a sheet without borders or padding, remove the +3+3 from the montage line.

Code: Select all

montage @filelist.txt -geometry "400x500" -background gray -tile 5x3 folder.jpg

Post Reply