Computer Hope

Microsoft => Microsoft DOS => Topic started by: Maverick27 on March 25, 2017, 06:15:03 AM

Title: DOS Command to capture image names & output to Excel ?
Post by: Maverick27 on March 25, 2017, 06:15:03 AM
Hi - i have 1000's of product images (mostly JPG / PNG) stored in a folder.

Anyone care to share a DOS Command to capture the image file names & output to an Excel ?  ::)

This will save me alot of time in individually copying / pasting the name to Excel Sheet.  :-X

Thanks in Advance.

 
Title: Re: DOS Command to capture image names & output to Excel ?
Post by: strollin on March 25, 2017, 07:03:30 AM
The simplest way to do what you asked for is this:  for %%x in (*.jpg) do echo %%x ,  >> out.csv      (save this command to a batch file, if you want to run it from the command line use only a single % sign in front of the x's).

This creates a CSV file named out.csv which is a text file containing Comma Separated Values (CSV).  Excel can read a CSV file in directly.

This command only reads the files in the current folder and only those with the extension .jpg.  You could run it multiple times with different extensions or change the command to add all the extensions you're looking for such as: for %%x in (*.jpg *.gif *.png) do echo %%x ,  >> out.csv
Title: Re: DOS Command to capture image names & output to Excel ?
Post by: Maverick27 on March 25, 2017, 12:41:29 PM
Thanks..

It seems you've figured out what I want to accomplish.

My DOS skills is completey ZERO, although I am an Oracle Programmer.

Do you mind giving a more concise answer ?

Ie :

1. Goto C: prompt
2. Type: for %%x in (*.jpg) do echo %%x ,  >> out.csv ??!!
3.   Then...

What do you mean by current folder ?

Although , I appreciate your answer,    I have no idea on what to do ?!

Title: Re: DOS Command to capture image names & output to Excel ?
Post by: strollin on March 25, 2017, 06:26:10 PM
The current folder is whichever folder you happen to be in at the time.  So, for instance, you you open a command prompt and type in cd \, you will change to the root folder.  The current folder will be the root folder.

What folder are the images you wish to capture the names of in?  Let's assume the folder is called C:\Images.  Open a command prompt and navigate to C:\Images to make it the current folder, then type in the command: for %x in (*.jpg *.gif *.png) do echo %x ,  >> out.csv    This will create a file in the folder called out.csv.  If you go into Windows Explorer and find that file and double-click on it, it will open in Excel and all of your image files will be listed there.
Title: Re: DOS Command to capture image names & output to Excel ?
Post by: Hackoo on March 26, 2017, 02:31:18 AM
Hi  :)
You can start from this script, just give a try and tell me the results  ;)
ImagesFinder.bat
Code: [Select]
@echo off
mode con cols=75 lines=4 & Color 0A
Title Search for images files
REM We set the variable Folder
Set "Folder=%USERPROFILE%\Pictures"
Rem We set the variable EXT for the extensions images to be searched by the script
Set "EXT=jpg png gif"
REM We initialize the variable Count from zero for counting images files
SET "Count=0"
Rem Set the variable LogFile to save the information after the scanning
Set "LogFile=%~n0.csv"
If exist "%LogFile%" Del "%LogFile%"
Setlocal Enabledelayedexpansion
For %%a in (%EXT%) Do (
Call :Scanning %%a
FOR /f "delims=" %%f IN ('dir /a-d /b /s "%Folder%\*.%%a"') DO (
Rem We increment the variable Count
SET /a "Count+=1"
rem populate the array variable list with the name of images
set "list[!Count!]=%%~nxf"
rem populate the array variable listpath with the whole path of images
set "listpath[!Count!]=%%~dpFf"
)
)
Cls
echo(
echo             Saving information into the file "%LogFile%"
echo             The Total of [%EXT%] images files(s) found is !Count!
rem Display array elements
for /L %%i in (1,1,%Count%) do (
rem save information into the LogFile.csv
echo %%i;!list[%%i]!;!listpath[%%i]!>> "%LogFile%"
)
Start "" "%LogFile%" & exit
::****************************************************************************
:Scanning %1
Cls
echo(
echo           Please Wait a while Scanning for "*.%1" images ...
Timeout /T 1 /nobreak>nul
exit /b
::****************************************************************************
Title: Re: DOS Command to capture image names & output to Excel ?
Post by: Salmon Trout on March 26, 2017, 06:43:15 AM
Let's hope there aren't any bmp images.
Title: Re: DOS Command to capture image names & output to Excel ?
Post by: strollin on March 26, 2017, 07:21:59 AM
Hi  :)
You can start from this script, just give a try and tell me the results  ;)
ImagesFinder.bat...   
Nice little .bat file Hackoo.  I'd recommend changing the line echo %%i;!list[%%i]!;!listpath[%%i]!>> "%LogFile%" to use commas instead of semi-colons, that way Excel will put the count, file name and location in separate columns.

As for Salmon's comment regarding .bmp files, any other type of image files can be added to the line: Set "EXT=jpg png gif bmp tif" or in my original command: for %x in (*.jpg *.gif *.png *.bmp *.tif) do echo %x ,  >> out.csv
Title: Re: DOS Command to capture image names & output to Excel ?
Post by: Hackoo on March 26, 2017, 10:01:22 AM
New script to search for [jpg png gif bmp tif] with ShowBalloonTip function in powershell for fancy  ;D
ImagesFinder.bat
Code: [Select]
@echo off
Title Search for images files
mode con cols=75 lines=5 & Color 0A
REM We set the variable Folder
Set "Folder=%USERPROFILE%\Pictures"
Rem We set the variable EXT for the extensions images to be searched by the script
Set "EXT=jpg png gif bmp tif"
REM We initialize the variable Count from zero for counting images files
SET "Count=0"
Rem Set the variable LogFile to save the information after the scanning
Set "LogFile=%~n0.csv"
If exist "%LogFile%" Del "%LogFile%"
Set "Msg=Please wait a while scanning for"
Setlocal Enabledelayedexpansion
For %%a in (%EXT%) Do (
Call :Scanning %%a
Call :ShowBalloonTip 'Information' 100 '"%~n0 to find images"' '"%Msg% """*.%%a""" images ..."' 'Info' 10
FOR /f "delims=" %%f IN ('dir /a-d /b /s "%Folder%\*.%%a"') DO (
Rem We increment the variable Count
SET /a "Count+=1"
rem populate the array variable list with the name of images
set "list[!Count!]=%%~nxf"
rem populate the array variable listpath with the whole path of images
set "listpath[!Count!]=%%~dpFf"
)
)
Cls
echo(
echo             Saving information into the file "%LogFile%"
echo        The Total of [%EXT%] images files(s) found is !Count!
rem Display array elements
for /L %%i in (1,1,%Count%) do (
rem save information into the LogFile.csv
echo %%i,!list[%%i]!,!listpath[%%i]!>> "%LogFile%"
)
Start "" "%LogFile%" & exit
::****************************************************************************
:Scanning %1
Cls
echo( & echo(
echo             %Msg% "*.%1" images ...
exit /b
::****************************************************************************
:ShowBalloonTip $notifyicon $time $title $text $icon $Timeout
PowerShell ^
[reflection.assembly]::loadwithpartialname('System.Windows.Forms') ^| Out-Null; ^
[reflection.assembly]::loadwithpartialname('System.Drawing') ^| Out-Null; ^
$notify = new-object system.windows.forms.notifyicon; ^
$notify.icon = [System.Drawing.SystemIcons]::%1; ^
$notify.visible = $true; ^
$notify.showballoontip(%2,%3,%4,%5); ^
Start-Sleep -s %6; ^
$notify.Dispose()
%End PowerShell%
exit /B
::*****************************************************************************
Title: Re: DOS Command to capture image names & output to Excel ?
Post by: Blue on March 26, 2017, 02:11:11 PM
Maybe I can contribute something positive..............

If you have multiple folders in different locations and don't want the bother of putting the script in the "current folder" you're welcome to drop this code into your script. This will let you put your script wherever you want - then - at run-time - you can browse your computer and pick the target folder you want to work with.

Obviously, you have to tweak the original script to include this code - and use the variable that returns the folder you picked. And don't forget... make sure the "EXITs" in this script are removed or modified so when this code ends it doesn't terminate the script it was inserted into.

Cheers !!

Code: [Select]
@echo off
setlocal enabledelayedexpansion
::
set SName=%~dpn0
::
call :SavBrowse
::
cls & color 2E
::
:: =====================================================================
::   call the folder-finder
:: =====================================================================
::
set _Repo=
For /f "delims=" %%A in (
 'cscript //nologo "%temp%\900_VBrowse.vbs" "C:\"'
) do set _Repo=%%A
::
echo. & echo.
echo Selected Folder:   %_Repo%
echo. & echo.
echo. & echo.
pause
::
::
:: =====================================================================
::  End-of-Script
:: =====================================================================
::
cls & color 5E
echo. & echo. & echo. & echo.
echo. & echo   * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
echo. & echo   *  %~nx0
echo. & echo   *    Blah, Blah, Blah........
echo. & echo   *   
echo. & echo   * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
echo. & echo.
set /p entered=....Hit any key to continue   
::
EXIT
::
::
:: @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
:: @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
::
:: -_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-
::    Subroutines
:: -_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-
::
:SavBrowse
::
set _aa=Function BrowseForFile()
echo %_aa% > %temp%\900_VBrowse.vbs
set _aa='
echo %_aa% >> %temp%\900_VBrowse.vbs
set _aa=xparm = ""
echo %_aa% >> %temp%\900_VBrowse.vbs
set _aa=xparm = WScript.Arguments.Item(0)
echo %_aa% >> %temp%\900_VBrowse.vbs
set _aa='
echo %_aa% >> %temp%\900_VBrowse.vbs
set _aa=Dim shell : Set shell = CreateObject("Shell.Application")
echo %_aa% >> %temp%\900_VBrowse.vbs
set _aa=Dim file : Set file = shell.BrowseForFolder(0, "Choose a folder:", ^^^&H4000, xparm)
echo %_aa% >> %temp%\900_VBrowse.vbs
set _aa='
echo %_aa% >> %temp%\900_VBrowse.vbs
set _aa=BrowseForFile = file.self.Path
echo %_aa% >> %temp%\900_VBrowse.vbs
set _aa=End Function
echo %_aa% >> %temp%\900_VBrowse.vbs
set _aa='
echo %_aa% >> %temp%\900_VBrowse.vbs
set _aa=WScript.echo(BrowseForFile)
echo %_aa% >> %temp%\900_VBrowse.vbs
goto :EOF

Title: Re: DOS Command to capture image names & output to Excel ?
Post by: Salmon Trout on March 26, 2017, 02:16:27 PM
As someone who has tinkered with batch/vbs hybrid scripts, I have to ask: why all those set _aa and echo %_aa% commands? Why not just echo the lines directly into the vbscript? It makes the batch harder to read and doubles the number of lines in that section.



Title: Re: DOS Command to capture image names & output to Excel ?
Post by: patio on March 26, 2017, 02:28:13 PM
 ;)
Title: Re: DOS Command to capture image names & output to Excel ?
Post by: Salmon Trout on March 26, 2017, 02:37:04 PM
Also, double-colons (which are really broken labels) as comments are severely deprecated in NT family batch scripting; they may look cool but they can break scripts, e.g. in parenthesis blocks.

Not wishing to pour cold water on what is a very neat idea.

Title: Re: DOS Command to capture image names & output to Excel ?
Post by: Hackoo on March 26, 2017, 03:50:51 PM
If you want to use the function Browse4Folder, you can do something like this  ;)  ;D
ImagesFinder.bat
Code: [Select]
@echo off
Title Search for images files
mode con cols=75 lines=5 & Color 0A
REM We set the variable Folder
Call :Browse4Folder "Choose source folder to scan for images files" "c:\scripts
Set "Folder=%Location%"
Rem if the variable %Folder% have a trailing back slash, we remove it !
IF %Folder:~-1%==\ SET Folder=%Folder:~0,-1%
If "%errorlevel%" EQU "0" (
echo( & echo(
echo             You choose this folder "%Folder%"
Timeout /T 2 /nobreak>nul
) else (
echo( & echo(
echo                              "%Folder%"
Timeout /T 2 /nobreak>nul & Exit
)
Rem We set the variable EXT for the extensions images to be searched by the script
Set "EXT=jpg png gif bmp tif"
REM We initialize the variable Count from zero for counting images files
SET "Count=0"
Rem Set the variable LogFile to save the information after the scanning
For %%f in (%Folder%) do set "LogFile=%~n0_%%~nf.csv"
If exist "%LogFile%" Del "%LogFile%"
Set "Msg=Please wait a while scanning for"
Setlocal Enabledelayedexpansion
For %%a in (%EXT%) Do (
Call :Scanning %%a
Call :ShowBalloonTip 'Information' 100 '"%~n0 to find images"' '"%Msg% """*.%%a""" images ..."' 'Info' 10
FOR /f "delims=" %%f IN ('dir /a-d /b /s "%Folder%\*.%%a"') DO (
Rem We increment the variable Count
SET /a "Count+=1"
rem populate the array variable list with the name of images
set "list[!Count!]=%%~nxf"
rem populate the array variable listpath with the whole path of images
set "listpath[!Count!]=%%~dpFf"
)
)
Cls
echo(
echo             Saving information into the file "%LogFile%"
echo        The Total of [%EXT%] images files(s) found is !Count!
rem Display array elements
for /L %%i in (1,1,%Count%) do (
rem save information into the LogFile.csv
echo %%i,!list[%%i]!,!listpath[%%i]!>> "%LogFile%"
)
If exist "%LogFile%" (
Start "" "%LogFile%" & exit
) else (
exit
)
::****************************************************************************
:Scanning %1
Cls
echo( & echo(
echo             %Msg% "*.%1" images ...
exit /b
::****************************************************************************
:ShowBalloonTip $notifyicon $time $title $text $icon $Timeout
PowerShell ^
[reflection.assembly]::loadwithpartialname('System.Windows.Forms') ^| Out-Null; ^
[reflection.assembly]::loadwithpartialname('System.Drawing') ^| Out-Null; ^
$notify = new-object system.windows.forms.notifyicon; ^
$notify.icon = [System.Drawing.SystemIcons]::%1; ^
$notify.visible = $true; ^
$notify.showballoontip(%2,%3,%4,%5); ^
Start-Sleep -s %6; ^
$notify.Dispose()
%End PowerShell%
exit /B
::*****************************************************************************
:Browse4Folder
set Location=
set vbs="%temp%\_.vbs"
set cmd="%temp%\_.cmd"
for %%f in (%vbs% %cmd%) do if exist %%f del %%f
for %%g in ("vbs cmd") do if defined %%g set %%g=
(
    echo set shell=WScript.CreateObject("Shell.Application"^)
    echo set f=shell.BrowseForFolder(0,"%~1",0,"%~2"^)
    echo if typename(f^)="Nothing" Then 
    echo wscript.echo "set Location=Dialog Cancelled"
    echo WScript.Quit(1^)
    echo end if
    echo set fs=f.Items(^):set fi=fs.Item(^)
    echo p=fi.Path:wscript.echo "set Location=" ^& p
)>%vbs%
cscript //nologo %vbs% > %cmd%
for /f "delims=" %%a in (%cmd%) do %%a
for %%f in (%vbs% %cmd%) do if exist %%f del /f /q %%f
for %%g in ("vbs cmd") do if defined %%g set %%g=
goto :eof
::******************************************************************************
Title: Re: DOS Command to capture image names & output to Excel ?
Post by: Blue on March 26, 2017, 05:22:22 PM
I am not a "scriptor" - I just solve problems as I encounter them. Everyone has his/her own style so if it works for me - but you don't like what I do - or the way I do it - I still go to sleep at night knowing that it works for me. I'm not trying to sell you anything - I'm just offering code that might be helpful to somebody. I'm very sorry if that offends you to the point you must criticize my efforts. If you must pick-pick-pick I hope you feel better now.

I do appreciate the help some have given me in other threads.

Blue
Title: Re: DOS Command to capture image names & output to Excel ?
Post by: BC_Programmer on March 26, 2017, 05:31:25 PM
There are two ways to accept criticism, you can take it personally, or you can use it to improve.
Title: Re: DOS Command to capture image names & output to Excel ?
Post by: strollin on March 27, 2017, 09:20:02 AM
It looks like we lost the OP for this thread.
Title: Re: DOS Command to capture image names & output to Excel ?
Post by: patio on March 27, 2017, 09:43:41 AM
Quite awhile ago... 8)
Title: Re: DOS Command to capture image names & output to Excel ?
Post by: Hackoo on March 30, 2017, 05:25:01 PM
The OP is lost ; may be, he solved his issues on otherr side  ::)
Never mind  :P
So, i post my last modification of this batch file : ImagesFinder.bat
Code: [Select]
@echo off
Title Search for images files
mode con cols=75 lines=5 & Color 0A
REM We set the variable Folder with the function Browse4Folder
Call :Browse4Folder "Choose source folder to scan for images files" "c:\scripts"
Set "Folder=%Location%"
Rem if the variable %Folder% have a trailing back slash, we remove it !
IF %Folder:~-1%==\ SET Folder=%Folder:~0,-1%
If "%errorlevel%" EQU "0" (
echo( & echo(
echo             You choose this folder "%Folder%"
Timeout /T 2 /nobreak>nul
) else (
echo( & echo(
echo                              "%Folder%"
Timeout /T 2 /nobreak>nul & Exit
)
Rem We set the variable EXT for the extensions images to be searched by the script
Set "EXT=jpg png gif bmp tif"
REM We initialize the variable Count from zero for counting images files
SET "Count=0"
Rem Set the variable LogFile to save the information after the scanning
For %%f in (%Folder%) do set "LogFile=%~n0_%%~nf.csv"
If exist "%LogFile%" Del "%LogFile%"
Set "Msg=Please wait a while scanning for"
Setlocal Enabledelayedexpansion
For %%a in (%EXT%) Do (
Call :Scanning %%a
Call :ShowBalloonTip 'Information' 100 '"%~n0 to find images"' '"%Msg% """*.%%a""" images ..."' 'Info' 10
FOR /f "delims=" %%f IN ('dir /a-d /b /s "%Folder%\*.%%a"') DO (
Rem We increment the variable Count
SET /a "Count+=1"
rem populate the array variable list with the name of files with their extensions
set "list[!Count!]=%%~nxf"
rem populate the array variable list with the name of files without their extensions
set "listName[!Count!]=%%~nf"
rem populate the array variable listpath with the whole path of files
set "listpath[!Count!]=%%~dpFf"
Call :Scan %%~nxf
)
)
Cls
echo(
echo             Saving information into the file "%LogFile%"
echo           The Total of [%EXT%] images files(s) found is !Count!
rem Display array elements
for /L %%i in (1,1,%Count%) do (
rem save information into the LogFile.csv
echo %%i,!list[%%i]!,!listpath[%%i]!>> "%LogFile%"
)
If exist "%LogFile%" (
Start "" "%LogFile%" & exit
) else (
exit
)
::****************************************************************************
:Scanning %1
Cls
echo( & echo(
echo             %Msg% "*.%1" files ...
exit /b
::****************************************************************************
:Scan %1
Cls
echo( & echo(
echo             %Msg% "%1"
exit /b
::****************************************************************************
:ShowBalloonTip $notifyicon $time $title $text $icon $Timeout
PowerShell ^
[reflection.assembly]::loadwithpartialname('System.Windows.Forms') ^| Out-Null; ^
[reflection.assembly]::loadwithpartialname('System.Drawing') ^| Out-Null; ^
$notify = new-object system.windows.forms.notifyicon; ^
$notify.icon = [System.Drawing.SystemIcons]::%1; ^
$notify.visible = $true; ^
$notify.showballoontip(%2,%3,%4,%5); ^
Start-Sleep -s %6; ^
$notify.Dispose()
%End PowerShell%
exit /B
::*****************************************************************************
:Browse4Folder
set Location=
set vbs="%temp%\_.vbs"
set cmd="%temp%\_.cmd"
for %%f in (%vbs% %cmd%) do if exist %%f del %%f
for %%g in ("vbs cmd") do if defined %%g set %%g=
(
    echo set shell=WScript.CreateObject("Shell.Application"^)
    echo set f=shell.BrowseForFolder(0,"%~1",0,"%~2"^)
    echo if typename(f^)="Nothing" Then 
    echo wscript.echo "set Location=Dialog Cancelled"
    echo WScript.Quit(1^)
    echo end if
    echo set fs=f.Items(^):set fi=fs.Item(^)
    echo p=fi.Path:wscript.echo "set Location=" ^& p
)>%vbs%
cscript //nologo %vbs% > %cmd%
for /f "delims=" %%a in (%cmd%) do %%a
for %%f in (%vbs% %cmd%) do if exist %%f del /f /q %%f
for %%g in ("vbs cmd") do if defined %%g set %%g=
goto :eof
::******************************************************************************