0

I am trying to export a list of specific file extensions from different folder paths. My code is not working somehow.

@Echo on
SetLocal EnableExtensions DisableDelayedExpansion
Set "SourceDir=C:\Users\Murray\Documents\Mainfile"
Set "Ext1=.ssl"
Set "Ext2=.conf"
for /f "delims=" %%A in ('Dir /B /A:-D "%SourceDir% ^| findstr /ie "%Ext1%" ^|
%SystemRoot%\System32\findstr.exe /ie "%Ext2%" ') do echo %%A  >> C:\Users\Murray\Documents\log.txt
Pause

What I want to overcome is to list all files from different directories and selected file extensions with full path. I would like to do this by setting variables. It is very likely that I am missing a few point. My main goal is to get full paths from a log text then copy them to the several directories depending on their extensions (Destination paths will be several and I will set them as variable similar to the listed code above not to use multiple goto submenus). Thanks in advance.

@Echo on
SetLocal EnableExtensions DisableDelayedExpansion
Set "SourceDir=C:\Users\Murray\Documents\Mainfile"
Set "Ext1=.ssl"
Set "Ext2=.conf"
for /f "delims=" %%A in ('Dir /B /A:-D "%SourceDir%" ^| findstr /ie "%Ext1%" ') do echo %%A  >> C:\Users\Murray\Documents\log.txt
Pause

This code exports the list of all files with extension .ssl to the log.txt from %SourceDir% but I could not manage to add extensions which were defined with %Ext2% so that they can be listed in the same log.txt file. Actually, The real problem here I would like to learn is that how can I set multiple source variables (different folder paths and file extension) as in my example to my code? For instance, how can I use multiple variables with Findstr?

Murray
  • 57
  • 7
  • your posted code does not do anything close to what your are describing as the goal. consider editing your question and replacing the code with a step by step breakdown of what you seek to accomplish. – T3RR0R Sep 18 '21 at 04:55
  • I have just edited my question as requested. – Murray Sep 18 '21 at 08:56
  • 1
    There is no point in using multiple `FINDSTR` commands as it is capable of accepting multiple search terms. And there is no point in using `FINDSTR` at all as the `DIR` command will accept multiple file masks you want to search for. – Squashman Sep 18 '21 at 16:30
  • My problem is to figure out how to use variables in order to shorten processes. Actually, I want to learn how to get things done without using multiple goto submenus. @Squashman – Murray Sep 18 '21 at 17:58

1 Answers1

1

The Dir command accepts only one argument for source destination. if no argument is given, the current directory will be searched.

If you need to search subfolders as well, The dir command accepts the switch /s to search the source directory and all sub directories

To do what you seek, multiple sources can be defined in list form to a variable, then for loops can be used to iterate over each source as appropriate. Pushd can be used to switch the working directory to the current item in the sourcelist.

For info on how to iterate over arrays in batch, see here.

@Echo off

Setlocal
:# define the directories to search
 set sourcelist="%Userprofile%\Desktop" "C:\Users\Murray\Documents\Mainfile"

:# define the extensions to search
 Set extlist=".ss1" ".conf"
 For %%G in (%extlist%)Do Set "[i]%%~G=0"

 For %%G in (%sourcelist%)Do (
  PUSHD %%G
  For %%H in (%extlist%)Do For /f "delims=" %%J in ('^
   dir /b /a:-d "*%%~H" 2^> nul ^| %__APPDIR__%findstr.exe /leic:"%%~H" ^
 ')Do (
   Set /a [i]%%~H+=1
   Setlocal EnableDelayedExpansion
   For %%v in (![i]%%~xH!)Do Endlocal & Set "%%~H[%%v]=%%~fJ"
  )
  POPD
 )

 Set .ss1

 Set .conf
 Pause
 Rem do stuff
Endlocal & Goto :Eof

the pipe through findstr ensures 'long' directories aren't falsely matched IE:".doc" ".docx"

this construct:

   Setlocal EnableDelayedExpansion
   For %%v in (![i]%%~xH!)Do Endlocal & Set "%%~xH[%%v]=%%~fJ"

allows the expansion of the current value of the count to be assigned to a faux array reference variable without consuming any ! characters present in the filename.

  • %%~xH exapands first to the current .ext value of the %%H for loop ie: .ss1, then delayed expansion occurs expanding ![i].ss1! to its value. the for metavariables are assigned to the resulting variable name after the delayed expansion environment is exited via Endlocal IE:
Endlocal & Set ".ss1[#]=C:\Users\Murray\Documents\Mainfile\filename.ss1"
  • where # is the current count

This method of passing variables accross the endlocal is known as tunneling.

T3RR0R
  • 2,747
  • 3
  • 10
  • 25