Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6833927
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T23:02:45+00:00 2026-05-26T23:02:45+00:00

I have as command-line parameters to my batch script a list of filenames and

  • 0

I have as command-line parameters to my batch script a list of filenames and a folder. For each filename, I need to print all subfolders of the folder where the file is found (the path of that file). The subfolder names should be sorted in descending order of the file sizes (the file can have various sizes in different subfolders).

I have done this so far, but it doesn’t work:

::verify if the first parameter is the directory

@echo off
REM check the numbers of parameters
if "%2"=="" goto err1
REM check: is first parameter a directory?
if NOT EXIST %1\NUL goto err2
set d=%1
shift
REM iterate the rest of the parameters


for %%i in %dir  do (
find %dir /name %i > temp

if EXIST du /b temp | cut /f 1 goto err3 
 myvar=TYPE temp
echo "file " %i "is in: "

for %%j in %myvar do
 echo %j

 echo after sort
du /b %myvar | sort /nr       
)

:err1
echo Two parameters are necessary 
goto end

:err2
echo First parameter must be a directory.
goto end

:err3 
echo file does not exist.
goto end

:end
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T23:02:46+00:00Added an answer on May 26, 2026 at 11:02 pm

    I don’t feel guilty answering this homework question now that the semester is long past. Print folders and files recursively using Windows Batch is a closed duplicate question that discusses the assignment.

    My initial solution is fairly straight forward. There are a few tricks to make sure it properly handles paths with special characters in them, but nothing too fancy. The only other trick is left padding the file size with spaces so that SORT works properly.

    Just as in the original question, the 1st parameter should be a folder path (.\ works just fine), and subsequent arguments represent file names (wildcards are OK).

    @echo off
    setlocal disableDelayedExpansion
    set tempfile="%temp%\_mysort%random%.txt"
    
    set "root="
    for %%F in (%*) do (
      if not defined root (
        pushd %%F || exit /b
        set root=1
      ) else (
        echo(
        echo %%~nxF
        echo --------------------------------------------
        (
          @echo off
          for /f "eol=: delims=" %%A in ('dir /s /b "%%~nxF"') do (
            set "mypath=%%~dpA"
            set "size=            %%~zA"
            setlocal enableDelayedExpansion
            set "size=!size:~-12!"
            echo !size! !mypath!
            endlocal
          )
        ) >%tempfile%
            sort /r %tempfile%
      )
    )
    if exist %tempfile% del %tempfile%
    if defined root popd
    

    I had hoped to avoid creation of a temporary file by replacing the redirect and subsequent sort with a pipe directly to sort. But this does not work. (see my related question: Why does delayed expansion fail when inside a piped block of code?)

    My first solution works well, except there is the potential for duplicate output depending on what input is provided. I decided I would write a version that weeds out duplicate file reports.

    The basic premise was simple – save all output to one temp file with the file name added to the front of the sorted strings. Then I need to loop through the results and only print information when the file and/or the path changes.

    The last loop is the tricky part, because file names can contain special characters like ! ^ & and % that can cause problems depending on what type of expansion is used. I need to set and compare variables within a loop, which usually requires delayed expansion. But delayed expansion causes problems with FOR variable expansion when ! is found. I can avoid delayed expansion by calling outside the loop, but then the FOR variables become unavailable. I can pass the variables as arguments to a CALLed routine without delayed expansion, but then I run into problems with % ^ and &. I can play games with SETLOCAL/ENDLOCAL, but then I need to worry about passing values across the ENDLOCAL barrier, which requires a fairly complex escape process. The problem becomes a big vicious circle.

    One other self imposed constraint is I don’t want to enclose the file and path output in quotes, so that means I must use delayed expansion, FOR variables, or escaped values.

    I found an interesting solution that exploits an odd feature of FOR variables.

    Normally the scope of FOR variables is strictly within the loop. If you CALL outside the loop, then the FOR variable values are no longer available. But if you then issue a FOR statement in the called procedure – the caller FOR variables become visible again! Problem solved!

    @echo off
    setlocal disableDelayedExpansion
    set tempfile="%temp%\_mysort%random%.txt"
    if exist %tempfile% del %tempfile%
    
    set "root="
    (
      for %%F in (%*) do (
        if not defined root (
          pushd %%F || exit /b
          set root=1
        ) else (
          set "file=%%~nxF"
          for /f "eol=: delims=" %%A in ('dir /s /b "%%~nxF"') do (
            set "mypath=%%~dpA"
            set "size=            %%~zA"
            setlocal enableDelayedExpansion
            set "size=!size:~-12!"
            echo(!file!/!size!/!mypath!
            endlocal
          )
        )
      )
    )>%tempfile%
    
    set "file="
    set "mypath="
    for /f "tokens=1-3 eol=/ delims=/" %%A in ('sort /r %tempfile%') do call :proc
    
    if exist %tempfile% del %tempfile%
    if defined root popd
    exit /b
    
    :proc
      for %%Z in (1) do (
        if "%file%" neq "%%A" (
          set "file=%%A"
          set "mypath="
          echo(
          echo %%A
           echo --------------------------------------------
        )
      )
      for %%Z in (1) do (
        if "%mypath%" neq "%%C" (
          set "mypath=%%C"
          echo %%B %%C
        )
      )
    exit /b
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an executeable (Command line which requires arguments/parameters) i need to run on
Background In a C# command-line app I'm writing, several of the parameters have yes
I have a command line script that uses the Django ORM and MySQL backend.
The script I'm writing will require me to pass some command line parameters. I
I have a single string that contains the command-line parameters to be passed to
I have a validation check when I pass the command line parameters to the
I have run program with command-line parameters. How can i wait for it to
I have an application that takes various command line parameters. This works fine but
InnoSetup setup executables have command line options to permit unattended or batch file operation
I have a VB6 console app and it uses command line parameters. For debugging,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.