Home > database >  How can I echo subdirectory all the files inside?
How can I echo subdirectory all the files inside?

Time:06-02

Sorry, there is my updated question.

I want to echo all the subfolders and all the files with a specific extension that are inside those subfolders. I would like to echo them like this :

Subfolder 1 :
"C:\Users\546802\Desktop\outil_5s\env_test\Subfolder1\File1.mjb"
"C:\Users\546802\Desktop\outil_5s\env_test\Subfolder1\File2.mjb"
Subfolder 2 :
"C:\Users\546802\Desktop\outil_5s\env_test\Subfolder2\File1.mjb"
"C:\Users\546802\Desktop\outil_5s\env_test\Subfolder2\File2.mjb"
Subfolder 3 :
"C:\Users\546802\Desktop\outil_5s\env_test\Subfolder3\File1.mjb"
"C:\Users\546802\Desktop\outil_5s\env_test\Subfolder3\File2.mjb"

At the moment, I only manage to echo all the files. So there is two things to see :

  1. How can I echo both of this things.
  2. How can I echo specific file only.

There is my actual code :

setlocal enableDelayedExpansion
set ext=".mjb"
for /f "delims=" %%F in ('dir /b /s /a-d "%_FolderName%"^| findstr /vile "!ext!"') do echo "%%F"

And this is my actual result (I'm trying this in a test_env) :

"C:\Users\546802\Desktop\outil_5s\env_test\Plein1\Fichier1.qds"
"C:\Users\546802\Desktop\outil_5s\env_test\Plein1\Fichier2.txt"
"C:\Users\546802\Desktop\outil_5s\env_test\Plein1\Fichier4.iso"
"C:\Users\546802\Desktop\outil_5s\env_test\Plein1\Fichier5.iso"
"C:\Users\546802\Desktop\outil_5s\env_test\Plein1\Fichier6.mjb"
"C:\Users\546802\Desktop\outil_5s\env_test\Plein1\Plein1\Fichier1.iso"
"C:\Users\546802\Desktop\outil_5s\env_test\Plein1\Plein1\Fichier2.mjb"
"C:\Users\546802\Desktop\outil_5s\env_test\Plein1\Plein1\Fichier3.mjb"
"C:\Users\546802\Desktop\outil_5s\env_test\Plein1\Plein1\Fichier4.wmf"
"C:\Users\546802\Desktop\outil_5s\env_test\Plein2\Fichier1.txt"
"C:\Users\546802\Desktop\outil_5s\env_test\Plein2\Fichier4.pdf"
"C:\Users\546802\Desktop\outil_5s\env_test\Plein2\Fichier5.iso"
"C:\Users\546802\Desktop\outil_5s\env_test\Plein2\Fichier6.mjb"

CodePudding user response:

@ECHO Off
setlocal ENABLEDELAYEDEXPANSION
SET "_FolderName=u:\Users\546802\Desktop\outil_5s\env_test"
set "ext=mjb"
SET "currentdir="
for /f "delims=" %%b in ('dir /b /s /a-d "%_FolderName%\*.%ext%"') do (
 IF "!currentdir!" neq "%%~dpb" ( 
  SET "currentdir=%%~dpb"
  FOR %%c IN ("%%~dpb.") DO ECHO %%~nxc :
  SET "files_dir=%%~dpb"
  SET "files_dir=!files_dir:%_FolderName%\=!"
  ECHO !files_dir:~0,-1! :
 )
 echo "%%b"
)
GOTO :EOF

Somewhat unclear what you want to do.

Your code performs a findstr with the /v option, so it will select all lines that do not match the mask provided, but the mask provided is resolved to "".mjb"" because you are assigning the quotes within the set statement, then re-quoting it.

It's not clear what header you are asking for where the filename is ...5s\env_test\Plein1\Fichier2.mjb - which may have the same name as ...5s\env_test\Plein1\Plein1\Fichier2.mjb. Is it the direct parent name (plein1 for both instances) or should there be a distinction?

The dir statement above selects all of the .mjb files which appears to be your goal.

currentdir keeps trac of the last parent directory found. If it changes, I've shown two possibilities. The first potential header shown is simply the parent directory name.

The second is calculated by removing the starting directory name from the absolute pathname of the file.

Note that I changed metavariables because F can be confused with ~f (the full-filename modifier operator) and that my testing was performed on drive u: (my RAMdrive)

CodePudding user response:

Thanks for your answer Magoo. Here is the exact thing of what I wanted exactly.

for /F "Tokens=1 Delims=" %%I in ('cscript //nologo explo_fichier.vbs') do set _FolderName=%%I
setlocal enableDelayedExpansion
set "ext=mjb"
set "currentdir="
for /f "delims=" %%b in ('dir /b /s /a-d "%_FolderName%\*.%ext%"') do (
 if "!currentdir!" neq "%%~dpb" ( 
  set "currentdir=%%~dpb"
  for %%c IN ("%%~dpb.") do echo %%~nxc :
 )
 echo "%%b"
)

I have a script that opens me the file explorer so I can choose which folder I want to test :

explo_fichier.vbs

Const MY_COMPUTER = &H11&
Const WINDOW_HANDLE = 0
Const OPTIONS = 0

Set objShell = CreateObject("Shell.Application")
Set objFolder = objShell.Namespace(MY_COMPUTER)
Set objFolderItem = objFolder.Self
strPath = objFolderItem.Path

Set objShell = CreateObject("Shell.Application")
Set objFolder = objShell.BrowseForFolder _
    (WINDOW_HANDLE, "Select a folder:", OPTIONS, strPath)
     
If objFolder Is Nothing Then
    Wscript.Quit
End If

Set objFolderItem = objFolder.Self
objPath = objFolderItem.Path

Wscript.Echo objPath

The goal of that is to get those informations so I can zip recursively all this files in a folder with the name of the parent folder. So my first step was getting those informations and now I will use a VBS script that zips everything as I want.

  • Related